我正在开发一个由FlatList组成的React Native应用程序。我推荐这篇文章, https://medium.com/react-native-development/how-to-use-the-flatlist-component-react-native-basics-92c482816fe6 将FlatList组件用于我的应用程序。
我想实现搜索栏以搜索该列表的内容(使用每个项目的标题)&根据搜索文本自动呈现内容。如何在不使用任何库的情况下执行此操作?
感谢您的帮助!
答案 0 :(得分:2)
检查以下示例,该示例是我使用 flatlist 和 TextInput 创建的。搜索项目时,项目以下拉列表的形式显示。我认为这会对您有所帮助。
import React, { Component } from 'react';
import { View, Text, FlatList, TextInput, ListItem } from 'react-native';
class SearchFunction extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
value: '',
};
this.arrayNew = [
{ name: 'Robert' },
{ name: 'Bryan' },
{ name: 'Vicente' },
{ name: 'Tristan' },
{ name: 'Marie' },
{ name: 'Onni' },
{ name: 'sophie' },
{ name: 'Brad' },
{ name: 'Samual' },
{ name: 'Omur' },
{ name: 'Ower' },
{ name: 'Awery' },
{ name: 'Ann' },
{ name: 'Jhone' },
{ name: 'z' },
{ name: 'bb' },
{ name: 'cc' },
{ name: 'd' },
{ name: 'e' },
{ name: 'f' },
{ name: 'g' },
{ name: 'h' },
{ name: 'i' },
{ name: 'j' },
{ name: 'k' },
{ name: 'React' },
{ name: 'React native' },
{ name: 'react navigations' },
];
}
renderSeparator = () => {
return (
<View
style={{
height: 1,
width: '100%',
backgroundColor: '#CED0CE',
}}
/>
);
};
searchItems = text => {
let newData = this.arrayNew.filter(item => {
const itemData = `${item.name.toUpperCase()}`;
const textData = text.toUpperCase();
if(text.length >0 ){
return itemData.indexOf(textData) > -1;
}
});
this.setState({
data: newData,
value: text,
});
};
renderHeader = () => {
return (
<TextInput
style={{ height: 60, borderColor: '#000', borderWidth: 1 }}
placeholder=" Type Name..."
onChangeText={text => this.searchItems(text)}
value={this.state.value}
/>
);
};
render() {
return (
<View
style={{
flex: 1,
padding: 25,
width: '98%',
alignSelf: 'center',
justifyContent: 'center',
}}>
<FlatList
data={this.state.data}
renderItem={({ item }) => (
<Text style={{ padding: 10 }}>{item.name} </Text>
)}
keyExtractor={item => item.name}
ItemSeparatorComponent={this.renderSeparator}
ListHeaderComponent={this.renderHeader}
/>
</View>
);
}
}
export default SearchFunction;
答案 1 :(得分:0)
使用npm install --save searchable-flatlist
将库Searchable Flatlist安装在一个空文件夹中
打开在上面的文件夹中创建的node_module文件夹,复制searchable-flatlist文件夹,然后粘贴到项目的node_module文件夹中
然后按照以下代码片段进行操作:
import SearchableFlatlist from "searchable-flatlist";
import {Button, Input, Container, Header, Content, Card, CardItem, Body, Item, Icon} from 'native-base';
export default class App extends Component {
state = { searchTerm: "", data:"empty" };
render() {
if(this.state.data== "empty"){
return (
<View ><ActivityIndicator size="large" color="#0000ff" /> </View>
);
}
else{
return (
<View style={sContainer}>
<Card style={{backgroundColor: "#dddddda1"}}>
<Header searchBar rounded>
<Item>
<Icon name="ios-search" />
<Input placeholder="Search"
onChangeText={searchTerm => this.setState({ searchTerm })}
/>
<Icon name="ios-people" />
</Item>
<Button transparent>
<Text>Search</Text>
</Button>
</Header>
<SearchableFlatlist
searchProperty={"email"}
searchTerm={this.state.searchTerm}
data={this.state.customersBox}
containerStyle={{ flex: 1 }}
renderItem={({ item }) =>
<ListItem
roundAvatar
title={`${item.name.first} ${item.name.last}`}
subtitle={item.email}
avatar={{ uri: item.picture.thumbnail }}
+ containerStyle={{ borderBottomWidth: 0 }}
/>
}
// item.name.first specifies the value to filter with
keyExtractor={item => item.name.first}
ItemSeparatorComponent={Separator}
/>
</Card>
</View>
);
}
}
};
您可能需要稍微修改一下代码。