【发布时间】:2020-04-19 16:46:24
【问题描述】:
我有一个 SearchBar 和一个简单的 FlatList。我的搜索过滤器逻辑工作正常,但对于简单搜索来说没问题。
例子:
如果我的 FlatList 包含三个项目
- 参观时间。
- 他知道参观时间。
- 访问和时间。
如果我在我的 SearchBar 中搜索 "visit h",它只会呈现 item 2 -> 2。他知道参观时间。
我希望它在我搜索 “visit h”时呈现所有 3 个项目 我的搜索过滤器应该查找包括“访问”和“h”的每个项目。 “访问”中还应包括“访问”
如何捕获这种类型的过滤器?有没有可以使用的js库?执行此搜索过滤器的任何有效方法?
我当前的代码如下:
export default class HomeComponent extends Component {
constructor(props) {
super(props)
this.state = {
isLoading: true,
notifications: [],
query: '',
temp: []
}
};
componentDidMount() {
fetch('https://140xya6a67.execute-api.ap-southeast-1.amazonaws.com/dev/', {
method: 'GET',
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
notifications: responseJson,
notificationRead: false,
temp: responseJson,
})
})
};
goToDetails() {
return this.props.navigation.navigate(Details);
}
renderItem({item}) {
return <NotificationItem item={item}
onPress={() => {this.goToDetails()}}/>
}
handleSearch(text) {
const newData = _.filter(this.state.temp, (item) => {
const itemData = item.Desc ? item.Desc.toUpperCase() : ''.toUpperCase();
const textData = text.toUpperCase();
return itemData.indexOf(textData) > -1;
});
this.setState({
notifications: newData,
query: text,
});
}
renderContent() {
let {notifications} = this.state;
return (
<View>
<SearchBar placeholder='type here...' lightTheme round value={this.state.query}
onChangeText={(text) => {this.handleSearch(text)}}/>
<FlatList
keyExtractor={(item, id) => item.id}
data={notifications}
renderItem={this.renderItem.bind(this)}
/>
</View>
);
}
render() {
let {fill, container} = styles;
return (
<View style={[fill, container]}>
{this.renderContent()}
</View>
);
}
}
【问题讨论】:
标签: javascript reactjs react-native search react-native-flatlist