Search data in ListView react native

不打扰是莪最后的温柔 提交于 2019-12-06 11:22:17

In your fetchData method you should probably save responseData to state too. You will then interact with this data each time search field changes.

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

Now in your setSearchText() method you should include some filter function that will find the movies you want from the movies you saved to state in fetchData().

setSearchText(action){
    const searchText = event.nativeEvent.text;
    const filteredMovies = this.state.movies.filter(/* awesome filter function */);

    this.setState({
        searchText,
        dataSource: this.state.dataSource.cloneWithRows(filteredMovies);
    });
},

Each time you want to update ListView you have to update it's dataSource. Only this way ListView component can realize that the data it's displaying has changed.

Hope I helped.

Searching data in the listview is basically just searching for it in a linked list or an array just take the input and search for it in the datasource or data blob. You can use linear search or binary search whichever you prefer.

The UIExplorer example from Facebook shows how to do this with ListView: https://github.com/facebook/react-native/tree/master/Examples/UIExplorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!