[英]How to apply Search functionality to a SectionList with variable child members?
Here I am providing a small list to showcase the type of data I get, some lists are way big with multiple sections and multiple child objects.在这里,我提供了一个小列表来展示我获得的数据类型,有些列表很大,包含多个部分和多个子对象。 I have a search box on top of the SectionList and the list must only display the data according to the Search Query.
我在 SectionList 顶部有一个搜索框,列表必须仅根据搜索查询显示数据。 The List must sort real-time.
列表必须实时排序。 I have tried some solutions but the main problem seems to be the filtering logic.
我尝试了一些解决方案,但主要问题似乎是过滤逻辑。 Any help is welcome.
欢迎任何帮助。 Thanks in advance.
提前致谢。
sectionData =
[
{
"GroupName": "Otopulse",
"data": [
{
"CurrentSpeed": 0.01,
"LastUpdatedDate": "20 Jun, 2021",
"LastUpdatedTime": "08:29 AM",
"VehicleId": 1210,
"VehicleName": "AhmedGMC",
"VehicleStatus": "PARKED",
"VehicleType": "Car"
}
]
},
{
"GroupName": "Unassigned",
"data":
[
{
"CurrentSpeed": 1,
"LastUpdatedDate": "19 Jun, 2021",
"LastUpdatedTime": "03:35 PM",
"VehicleId": 1715,
"VehicleName": "Khalil",
"VehicleStatus": "OFFLINE",
"VehicleType": "Car"
},
{
"CurrentSpeed": 1,
"LastUpdatedDate": "29 Dec, 2020",
"LastUpdatedTime": "09:57 AM",
"VehicleId": 1697,
"VehicleName": "Nazir test",
"VehicleStatus": "OFFLINE",
"VehicleType": "Car"
}
]
}
]
This answer is for anyone who wishes to implement Search/Filter functionality on a SectionList in React Native.这个答案适用于希望在 React Native 中的 SectionList 上实现搜索/过滤功能的任何人。 I used a library: React Native Searchable List, see it here .
我使用了一个库:React Native Searchable List,见这里。 It is a fairly easy library to implement.
这是一个相当容易实现的库。 See my implementation: Please take a note of my section data from the issue statement above
查看我的实现:请从上面的问题声明中记下我的部分数据
const [data, setData] = useState([]);
const [searchTerm, setSearchTerm] = useState('');
const [searchAttribute, setSearchAttribute] = useState('VehicleName');
const [ignoreCase, setIgnoreCase] = useState(true);
<TextInput
style={styles.searchInputBox}
placeholder="Search"
onChangeText={setSearchTerm}
value={searchTerm}
/>
<SearchableSectionList
ItemSeparatorComponent={FlatListItemSeparator}
sections={data}
searchTerm={searchTerm}
searchAttribute={searchAttribute}
searchByTitle={false}
ignoreCase={ignoreCase}
keyExtractor={(item, index) => item + index}
renderItem={({item}) => <VehiclesListItem item={item} />}
renderSectionHeader={({section: {GroupName}}) => (
<View
style={{
backgroundColor: '#dadada',
padding: 10,
}}>
<Text style={{color: '#2A2A2A', fontSize: 15}}>{GroupName}</Text>
</View>
)}
/>
Thanks, take care.谢谢,保重。 Happy Coding!!
快乐编码!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.