P粉6378669312023-08-18 14:28:26
Create a state to hold the search query and another state to store the filtered data:
const [searchQuery, setSearchQuery] = useState(''); const [filteredCustomers, setFilteredCustomers] = useState(customers); // 初始化为完整列表
Now add this function. First update the searchQuery
status, then filter based on the given text
parameter and set the result to the filteredCustomers
status.
const handleSearchInputChange = (text) => { setSearchQuery(text); const filtered = customers.filter( (customer) => customer.customer.toLowerCase().includes(text.toLowerCase()) || customer.address.toLowerCase().includes(text.toLowerCase()) || customer.mobileno.includes(text) || customer.plateno.includes(text) ); setFilteredCustomers(filtered); };
This logic must be executed every time you enter in the search TextInput
.
<TextInput placeholder="按客户搜索..." onChangeText={handleSearchInputChange} value={searchQuery} />
Finally, just map by filtredCustomers
instead of displayedCustomers
:
{filteredCustomers.map((customer, index) => { return ( <TouchableOpacity key={index} > // .... </TouchableOpacity> ) })