gpt4 book ai didi

reactjs - 过滤简单的平面列表

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

我想通过搜索栏过滤这个简单的平面列表。我该如何编码,以便无论我在输入文本上写什么,它都会过滤平面列表?你能帮我完成吗?

import React from 'react';
import { StyleSheet, Text, View, SafeAreaView, TextInput, TouchableOpacity, LayoutAnimation, Image, FlatList, ScrollView } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
import {ListItem, SearchBar} from 'react-native-elements';

export default class HomeScreen extends React.Component{
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerTitle}>Home</Text>
</View>
<View style={styles.container1}>
<Icon name={"ios-search"} style={styles.icon}/>
<TextInput style={styles.inputBox}
underlineColorAndroid='rgba(0,0,0,0)'
placeholder="Procura aqui"
placeholderTextColor = "white"
selectionColor="black"
keyboardType="default"
/>
</View>
<View style={styles.flatlist}>
<FlatList
data = {[
{key:'Tiago'},
{key:'Ricardo'},
{key:'Beatriz'},
{key:'Miguel'},
{key:'Simão'},
{key:'David'}
]}
renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
/>
</View>
</View>
);
}
}

最佳答案

你应该有一个搜索文本的状态值,并根据它过滤数组。该组件应如下所示。

export default class HomeScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
searchText: '',
};
}

render() {
//Data can be coming from props or any other source as well
const data = [
{ key: 'Tiago' },
{ key: 'Ricardo' },
{ key: 'Beatriz' },
{ key: 'Miguel' },
{ key: 'Simão' },
{ key: 'David' },
];

const filteredData = this.state.searchText
? data.filter(x =>
x.key.toLowerCase().includes(this.state.searchText.toLowerCase())
)
: data;
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerTitle}>Home</Text>
</View>
<View style={styles.container1}>
<Icon name={'ios-search'} style={styles.icon} />
<TextInput
style={styles.inputBox}
underlineColorAndroid="rgba(0,0,0,0)"
placeholderTextColor="white"
selectionColor="black"
keyboardType="default"
onChangeText={text => this.setState({ searchText: text })}
value={this.state.searchText}
/>
</View>
<View style={styles.flatlist}>
<FlatList
data={filteredData}
renderItem={({ item }) => (
<Text style={styles.item}>{item.key}</Text>
)}
/>
</View>
</View>
);
}
}

关于reactjs - 过滤简单的平面列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61887810/

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