gpt4 book ai didi

listview - 在 React Native 中过滤 ListView 数据

转载 作者:行者123 更新时间:2023-12-04 15:41:48 25 4
gpt4 key购买 nike

我正在尝试在 React Native 中为我的 ListView 编写一个过滤器。但它不起作用,我不知道为什么。
这是我的类(class) -

import {
AppRegistry,
Image,
ListView,
StyleSheet,
Text,
TextInput,
Navigator,
TouchableHighlight,
TouchableOpacity,
filter,
View
} from 'react-native';
var REQUEST_URL = GLOBAL.STATES_URL + '?apikey=' + GLOBAL.API_KEY;

class MainPage extends Component {

constructor(props) {
super(props);
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
loaded: false,
searchText: '',
state: '',
};
}

componentDidMount() {
this.fetchData();
}

componentDidUpdate() {
if (this.state.searchText === '') {
this.fetchData();
}
}

filterStates(searchText, states) {
var text = searchText.toLowerCase();
var rows = [];

for (var i=0; i < states.length; i++) {
var stateName = states[i].name.toLowerCase();
if(stateName.search(text) !== -1){
rows.push({
name : states[i].name,
});
}
}

this.setState({
dataSource: this.state.dataSource.cloneWithRows(rows),
loaded : true,
});
}

fetchData() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData),
loaded: true,
});
})
.done();
}

setSearchText(event){
var searchText = event.nativeEvent.text;
this.setState({searchText});

fetch(REQUEST_URL)
.then((data) => {
this.filterStates(searchText, data);
})
.done();
}


render() {
return (
<Navigator
renderScene={this.renderScene.bind(this)}
navigator={this.props.navigator}
navigationBar={
<Navigator.NavigationBar style={{backgroundColor: '#246dd5'}}
routeMapper={NavigationBarRouteMapper} />
}
/>
);
}


renderScene(route, navigator) {
if (!this.state.loaded) {
return this.renderLoadingView();
}

return (
<View style={styles.combo}>

<TextInput
style={styles.searchBar}
value={this.state.searchText}
onChange={this.setSearchText.bind(this)}
placeholder='Search' />
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderState.bind(this)}
// renderScrollComponent={props => <RecyclerViewBackedScrollView {...props} />}
renderSeparator={(sectionID, rowID) => <View key={`${sectionID}-${rowID}`} style={styles.separator} />}
style={styles.listView}
/>
</View>

);
}

renderLoadingView() {
return (
<View style={styles.container}>
<Text>
Loading States...
</Text>
</View>
);
}

renderState(state,route,navigator) {
return (
<TouchableHighlight onPress={this.gotoPersonPage.bind(this,state)}>
<View style={styles.row}>
<View style={styles.container}>
<Text style={styles.name}>{state.name}</Text>
</View>
</View>
</TouchableHighlight>

);
}


gotoPersonPage(state) {
this.props.navigator.push({
id: 'PersonPage',
name: 'PersonPage',
data: {name:state.name , abbreviation:state.abbreviation},
});
}
}


任何人都可以告诉上面代码中的问题是什么,以及该问题的解决方案。
提前致谢。

最佳答案

尝试设置 new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 !== row2组件外:

var ds = new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 !== row2    

class MainPage extends Component {
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
loaded: false,
searchText: '',
state: '',
};
}

然后像这样设置状态:
filterStates(searchText, states) {
...

this.setState({
dataSource: ds.cloneWithRows(rows),
loaded : true,
});
}

和这个:
fetchData() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
this.setState({
dataSource: ds.cloneWithRows(responseData),
loaded: true,
});
})
.done();
}

关于listview - 在 React Native 中过滤 ListView 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37587524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com