how to set data A to Z alphabetic order when user select A to Z into drop down in react native?

huangapple go评论104阅读模式
英文:

how to set data A to Z alphabetic order when user select A to Z into drop down in react native?

问题

我正在创建一个屏幕,可以显示来自API的1000条数据。

这是图片:

how to set data A to Z alphabetic order when user select A to Z into drop down in react native?

现在我有一个下拉框,用户可以在下拉框中选择从A到Z的字母,然后在选择A到Z的字母顺序后,数据按照A到Z的字母顺序排列。所有数据按A到Z的顺序设置,那么我该如何在我的代码中执行这个功能?

我尝试了以下代码:

  1. // setData(json.results); <==================it for sorting
  2. // setData((prevData) => [...prevData, ...newData]);
  3. // const sorted = [...sortedData, ...newData].sort((a, b) => {
  4. // const lastNameA = a.name.first.toLowerCase();
  5. // const lastNameB = b.name.first.toLowerCase();
  6. // if (lastNameA < lastNameB) return -1;
  7. // if (lastNameA > lastNameB) return 1;
  8. // return 0;
  9. // });
  10. // setSortedData(sorted);

但是当我在下拉框中选择A到Z顺序列表时,它不起作用。

我该如何做到这一点?

代码:

  1. import {
  2. StyleSheet,
  3. Text,
  4. View,
  5. Image,
  6. TextInput,
  7. FlatList,
  8. ActivityIndicator,
  9. SafeAreaView,
  10. } from 'react-native';
  11. import React, { useEffect, useState } from 'react';
  12. import { scale, verticalScale, moderateScale } from 'react-native-size-matters';
  13. import { RFPercentage } from 'react-native-responsive-fontsize';
  14. import SelectDropdown from 'react-native-select-dropdown';
  15. const sortby = ['A to Z', 'Newawst', 'Oldest'];
  16. const App = () => {
  17. const [loading, setLoading] = useState(true);
  18. const [data, setData] = useState([]);
  19. const [search, setSearch] = useState('');
  20. const [filteredDataSource, setFilteredDataSource] = useState([]);
  21. const [masterDataSource, setMasterDataSource] = useState([]);
  22. const [total, setTotalData] = useState(0);
  23. const [searchCount, setSearchCount] = useState(0);
  24. const [sortedData, setSortedData] = useState([]);
  25. useEffect(() => {
  26. fetchData();
  27. }, []);
  28. const fetchData = async () => {
  29. try {
  30. const response = await fetch('https://randomuser.me/api?results=1000');
  31. const json = await response.json();
  32. const newData = json.results;
  33. setLoading(false);
  34. setFilteredDataSource(json.results);
  35. setMasterDataSource(json.results);
  36. setTotalData(json.info.results);
  37. } catch (error) {
  38. console.error(error);
  39. }
  40. };
  41. const searchFilterFunction = (text) => {
  42. // 检查搜索文本是否不为空
  43. if (text) {
  44. // 插入的文本不为空
  45. // 过滤masterDataSource并更新FilteredDataSource
  46. const newData = masterDataSource.filter(function (item) {
  47. // 在搜索栏中应用过滤器
  48. const itemData = item.name.first
  49. ? item.name.first.toUpperCase()
  50. : ''.toUpperCase();
  51. const textData = text.toUpperCase();
  52. return itemData.indexOf(textData) > -1;
  53. });
  54. setFilteredDataSource(newData);
  55. setSearch(text);
  56. setSearchCount(newData.length);
  57. } else {
  58. // 插入的文本为空
  59. // 使用masterDataSource更新FilteredDataSource
  60. setFilteredDataSource(masterDataSource);
  61. setSearch(text);
  62. }
  63. };
  64. const renderItem = ({ item }) => (
  65. <View style={{ padding: 10 }}>
  66. <Text>Name :{`${item.name.first} ${item.name.last}`}</Text>
  67. <Text>Email :{item.email}</Text>
  68. <Text>City :{item.location.city}</Text>
  69. <Text>State :{item.location.state}</Text>
  70. <Text>Country :{item.location.country}</Text>
  71. <Text>Postcode :{item.location.postcode}</Text>
  72. </View>
  73. );
  74. const ItemSeparatorView = () => {
  75. return (
  76. // Flat List Item Separator
  77. <View
  78. style={{
  79. height: 0.5,
  80. width: '100%',
  81. backgroundColor: '#000',
  82. }}
  83. />
  84. );
  85. };
  86. return (
  87. <View>
  88. <View style={styles.header}>
  89. <Image source={require('../img/menu.png')} style={styles.menuimg} />
  90. <Image source={require('../img/logo.png')} style={styles.logoimg} />
  91. <Image source={require('../img/user.png')} style={styles.userimg} />
  92. </View>
  93. <View style={{ alignItems: 'center' }}>
  94. <View style={styles.textbox}>
  95. <TextInput
  96. style={styles.textInputStyle}
  97. onChangeText={(text) => searchFilterFunction(text)}
  98. value={search}
  99. focusable={true}
  100. underlineColorAndroid="transparent"
  101. placeholder="Search"
  102. placeholderTextColor={'#000'}
  103. selectionColor="#000"
  104. />
  105. <Image
  106. source={require('../img/filter.png')}
  107. style={{ marginRight: scale(10) }}
  108. />
  109. </View>
  110. </View>
  111. <View
  112. style={{
  113. flexDirection: 'row',
  114. marginTop: verticalScale(10),
  115. justifyContent: 'space-around',
  116. alignItems: 'center',
  117. }}>
  118. <Text style={styles.resulttext}>
  119. {searchCount == 0 ? total : searchCount} results
  120. </Text>
  121. <SelectDropdown
  122. data={sortby}
  123. // defaultValueByIndex={1}
  124. onSelect={(selectedItem, index) => {
  125. console.log(selectedItem, index);
  126. }}
  127. defaultButtonText={'Sort By'}
  128. buttonTextAfterSelection={(selectedItem, index) => {
  129. return selectedItem;
  130. }}
  131. rowTextForSelection={(item, index) => {
  132. return item;
  133. }}
  134. buttonStyle={styles.dropdown2BtnStyle}
  135. buttonTextStyle={styles.dropdown2BtnTxtStyle}
  136. renderDropdownIcon={(isOpened) => {
  137. return isOpened ? (
  138. <>
  139. <Image
  140. source={require('../img/arrowup.png')}
  141. style={{
  142. width: scale(15),
  143. height: verticalScale(15),
  144. marginRight: verticalScale(10),
  145. }}
  146. />
  147. </>
  148. ) : (
  149. <>
  150. <Image
  151. source={require('../img/arrowdown.png')}
  152. style={{
  153. width: scale(15),
  154. height: verticalScale(15),
  155. marginRight: verticalScale(10),
  156. }}
  157. />
  158. </>
  159. );
  160. }}
  161. dropdownIconPosition={'right'}
  162. dropdownStyle={styles.dropdown2DropdownStyle}
  163. rowStyle={styles.dropdown2RowStyle}
  164. rowTextStyle={styles.dropdown2RowTxtStyle}
  165. />
  166. </View>
  167. <View style={{ marginTop: scale(10) }}>
  168. <SafeAreaView>
  169. {loading ? (
  170. <ActivityIndicator size="small" color="black" />
  171. ) : (
  172. <FlatList
  173. data={filtered
  174. <details>
  175. <summary>英文:</summary>
  176. I&#39;m creating a screen that can be displayed 1000 data it comes from the API
  177. here is image
  178. [![enter image description here](https://i.stack.imgur.com/ErbDD.png)](https://i.stack.imgur.com/ErbDD.png)
  179. now I have a drop-down box where users select A to Z alphabet into a drop-down box then after selecting A to Z alphabetic order then data arange in A to Z alphabetic order. all data set in A to Z order so how can I do this function in my code
  180. I&#39;m try this code

// setData(json.results); <==================it for sorting
// setData((prevData) => [...prevData, ...newData]);

  1. // const sorted = [...sortedData, ...newData].sort((a, b) =&gt; {
  2. // const lastNameA = a.name.first.toLowerCase();
  3. // const lastNameB = b.name.first.toLowerCase();
  4. // if (lastNameA &lt; lastNameB) return -1;
  5. // if (lastNameA &gt; lastNameB) return 1;
  6. // return 0;
  7. // });
  8. // setSortedData(sorted);
  1. but its not working when I&#39;m selecting A to Z order list in to drop down box
  2. how can I do that ?
  3. **code:**

import {
StyleSheet,
Text,
View,
Image,
TextInput,
FlatList,
ActivityIndicator,
SafeAreaView,
} from 'react-native';
import React, { useEffect, useState } from 'react';
import { scale, verticalScale, moderateScale } from 'react-native-size-matters';
import { RFPercentage } from 'react-native-responsive-fontsize';
import SelectDropdown from 'react-native-select-dropdown';

const sortby = ['A to Z', 'Newawst', 'Oldest'];

const App = () => {
const [loading, setLoading] = useState(true);
const [data, setData] = useState([]);
const [search, setSearch] = useState('');
const [filteredDataSource, setFilteredDataSource] = useState([]);
const [masterDataSource, setMasterDataSource] = useState([]);
const [total, setTotalData] = useState(0);
const [searchCount, setSearchCount] = useState(0);
const [sortedData, setSortedData] = useState([]);

useEffect(() => {
fetchData();
}, []);

const fetchData = async () => {
try {
const response = await fetch('https://randomuser.me/api?results=1000');
const json = await response.json();
const newData = json.results;

  1. setLoading(false);
  2. setFilteredDataSource(json.results);
  3. setMasterDataSource(json.results);
  4. setTotalData(json.info.results);
  5. } catch (error) {
  6. console.error(error);
  7. }

};

const searchFilterFunction = (text) => {
// Check if searched text is not blank
if (text) {
// Inserted text is not blank
// Filter the masterDataSource and update FilteredDataSource
const newData = masterDataSource.filter(function (item) {
// Applying filter for the inserted text in search bar
const itemData = item.name.first
? item.name.first.toUpperCase()
: ''.toUpperCase();
const textData = text.toUpperCase();
return itemData.indexOf(textData) > -1;
});
setFilteredDataSource(newData);
setSearch(text);
setSearchCount(newData.length);
} else {
// Inserted text is blank
// Update FilteredDataSource with masterDataSource
setFilteredDataSource(masterDataSource);
setSearch(text);
}
};

const renderItem = ({ item }) => (
<View style={{ padding: 10 }}>
<Text>Name :{${item.name.first} ${item.name.last}}</Text>
<Text>Email :{item.email}</Text>
<Text>City :{item.location.city}</Text>
<Text>State :{item.location.state}</Text>
<Text>Country :{item.location.country}</Text>
<Text>Postcode :{item.location.postcode}</Text>
</View>
);
const ItemSeparatorView = () => {
return (
// Flat List Item Separator
<View
style={{
height: 0.5,
width: '100%',
backgroundColor: '#000',
}}
/>
);
};

return (
<View>
<View style={styles.header}>
<Image source={require('../img/menu.png')} style={styles.menuimg} />
<Image source={require('../img/logo.png')} style={styles.logoimg} />
<Image source={require('../img/user.png')} style={styles.userimg} />
</View>
<View style={{ alignItems: 'center' }}>
<View style={styles.textbox}>
<TextInput
style={styles.textInputStyle}
onChangeText={(text) => searchFilterFunction(text)}
value={search}
focusable={true}
underlineColorAndroid="transparent"
placeholder="Search"
placeholderTextColor={'#000'}
selectionColor="#000"
/>
<Image
source={require('../img/filter.png')}
style={{ marginRight: scale(10) }}
/>
</View>
</View>
<View
style={{
flexDirection: 'row',
marginTop: verticalScale(10),
justifyContent: 'space-around',
alignItems: 'center',
}}>
<Text style={styles.resulttext}>
{searchCount == 0 ? total : searchCount} results
</Text>
<SelectDropdown
data={sortby}
// defaultValueByIndex={1}
onSelect={(selectedItem, index) => {
console.log(selectedItem, index);
}}
defaultButtonText={'Sort By'}
buttonTextAfterSelection={(selectedItem, index) => {
return selectedItem;
}}
rowTextForSelection={(item, index) => {
return item;
}}
buttonStyle={styles.dropdown2BtnStyle}
buttonTextStyle={styles.dropdown2BtnTxtStyle}
renderDropdownIcon={(isOpened) => {
return isOpened ? (
<>
<Image
source={require('../img/arrowup.png')}
style={{
width: scale(15),
height: verticalScale(15),
marginRight: verticalScale(10),
}}
/>
</>
) : (
<>
<Image
source={require('../img/arrowdown.png')}
style={{
width: scale(15),
height: verticalScale(15),
marginRight: verticalScale(10),
}}
/>
</>
);
}}
dropdownIconPosition={'right'}
dropdownStyle={styles.dropdown2DropdownStyle}
rowStyle={styles.dropdown2RowStyle}
rowTextStyle={styles.dropdown2RowTxtStyle}
/>
</View>

  1. &lt;View style={{ marginTop: scale(10) }}&gt;
  2. &lt;SafeAreaView&gt;
  3. {loading ? (
  4. &lt;ActivityIndicator size=&quot;small&quot; color=&quot;black&quot; /&gt;
  5. ) : (
  6. &lt;FlatList
  7. data={filteredDataSource}
  8. renderItem={renderItem}
  9. ItemSeparatorComponent={ItemSeparatorView}
  10. keyExtractor={(item, index) =&gt; index.toString()}
  11. /&gt;
  12. )}
  13. &lt;/SafeAreaView&gt;
  14. &lt;/View&gt;
  15. &lt;/View&gt;

);
};

export default App;

const styles = StyleSheet.create({
contenir: {
flex: 1,
},
header: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginLeft: verticalScale(10),
marginRight: verticalScale(10),
},
menuimg: { width: scale(40), height: verticalScale(40) },
logoimg: {
width: scale(60),
height: verticalScale(60),
marginTop: scale(10),
},
userimg: {
width: scale(60),
height: verticalScale(60),
marginTop: scale(10),
},
textInputStyle: {
// height: verticalScale(60),
width: scale(260),
fontSize: RFPercentage(3),
color: '#000',
fontWeight: '900',
// borderWidth: scale(2),
// paddingLeft:moderateScale(20),
// margin: scale(5),
// borderRadius:scale(20),
// borderColor: '#000',
// backgroundColor: '#FFFFFF',
},
textbox: {
alignItems: 'center',
height: verticalScale(65),
width: scale(320),
borderColor: '#000',
backgroundColor: '#FFFFFF',
borderRadius: scale(30),
borderWidth: scale(2),
paddingLeft: moderateScale(20),
margin: scale(5),
flexDirection: 'row',
justifyContent: 'space-between',
},
resulttext: {
fontSize: RFPercentage(3.7),
fontWeight: '700',
color: '#000',
// marginLeft: scale(20)
},
// scrollViewContainer: {
// justifyContent: 'space-around',
// },
dropdown2BtnStyle: {
width: scale(150),
height: verticalScale(60),
backgroundColor: '#000',
borderRadius: scale(30),
},
dropdown2BtnTxtStyle: {
color: '#FFF',
textAlign: 'center',
fontSize: RFPercentage(2.5),
fontWeight: 'bold',
},
dropdown2DropdownStyle: {
backgroundColor: '#444',
width: scale(120),
height: verticalScale(70),
borderRadius: scale(20),
// borderBottomRightRadius: 12,
},
dropdown2RowStyle: {
backgroundColor: '#000',
},
dropdown2RowTxtStyle: {
color: '#FFF',
textAlign: 'center',
},
});

  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. 以下是代码的翻译部分:
  5. ```javascript
  6. import {
  7. StyleSheet,
  8. Text,
  9. View,
  10. Image,
  11. TextInput,
  12. FlatList,
  13. ActivityIndicator,
  14. SafeAreaView,
  15. } from 'react-native';
  16. import React, { useEffect, useState } from 'react';
  17. import { scale, verticalScale, moderateScale } from 'react-native-size-matters';
  18. import { RFPercentage } from 'react-native-responsive-fontsize';
  19. import SelectDropdown from 'react-native-select-dropdown';
  20. const sortby = ['A 到 Z', '最新', '最旧'];
  21. const App = () => {
  22. const [loading, setLoading] = useState(true);
  23. const [data, setData] = useState([]);
  24. const [search, setSearch] = useState('');
  25. const [filteredDataSource, setFilteredDataSource] = useState([]);
  26. const [masterDataSource, setMasterDataSource] = useState([]);
  27. const [total, setTotalData] = useState(0);
  28. const [searchCount, setSearchCount] = useState(0);
  29. const [sortedData, setSortedData] = useState([]);
  30. useEffect(() => {
  31. fetchData();
  32. }, []);
  33. const fetchData = async () => {
  34. try {
  35. const response = await fetch('https://randomuser.me/api?results=1000');
  36. const json = await response.json();
  37. const newData = json.results;
  38. setLoading(false);
  39. setFilteredDataSource(json.results);
  40. setMasterDataSource(json.results);
  41. setTotalData(json.info.results);
  42. } catch (error) {
  43. console.error(error);
  44. }
  45. };
  46. const searchFilterFunction = (text) => {
  47. // 检查搜索文本是否不为空
  48. if (text) {
  49. // 插入的文本不为空
  50. // 过滤主数据源并更新FilteredDataSource
  51. const newData = masterDataSource.filter(function (item) {
  52. // 在搜索栏中应用过滤器
  53. const itemData = item.name.first
  54. ? item.name.first.toUpperCase()
  55. : ''.toUpperCase();
  56. const textData = text.toUpperCase();
  57. return itemData.indexOf(textData) > -1;
  58. });
  59. setFilteredDataSource(newData);
  60. setSearch(text);
  61. setSearchCount(newData.length);
  62. } else {
  63. // 插入的文本为空
  64. // 使用主数据源更新FilteredDataSource
  65. setFilteredDataSource(masterDataSource);
  66. setSearch(text);
  67. }
  68. };
  69. const renderItem = ({ item }) => (
  70. <View style={{ padding: 10 }}>
  71. <Text>姓名: {`${item.name.first} ${item.name.last}`}</Text>
  72. <Text>电子邮件: {item.email}</Text>
  73. <Text>城市: {item.location.city}</Text>
  74. <Text>州: {item.location.state}</Text>
  75. <Text>国家: {item.location.country}</Text>
  76. <Text>邮政编码: {item.location.postcode}</Text>
  77. </View>
  78. );
  79. const ItemSeparatorView = () => {
  80. return (
  81. // 列表项分隔符
  82. <View
  83. style={{
  84. height: 0.5,
  85. width: '100%',
  86. backgroundColor: '#000',
  87. }}
  88. />
  89. );
  90. };
  91. return (
  92. <View>
  93. <View style={styles.header}>
  94. <Image source={require('../assets/google.png')} style={styles.menuimg} />
  95. <Image source={require('../assets/google.png')} style={styles.logoimg} />
  96. <Image source={require('../assets/google.png')} style={styles.userimg} />
  97. </View>
  98. <View style={{ alignItems: 'center' }}>
  99. <View style={styles.textbox}>
  100. <TextInput
  101. style={styles.textInputStyle}
  102. onChangeText={(text) => searchFilterFunction(text)}
  103. value={search}
  104. focusable={true}
  105. underlineColorAndroid="transparent"
  106. placeholder="搜索"
  107. placeholderTextColor={'#000'}
  108. selectionColor="#000"
  109. />
  110. <Image
  111. source={require('../assets/google.png')}
  112. style={{ marginRight: scale(10) }}
  113. />
  114. </View>
  115. </View>
  116. <View
  117. style={{
  118. flexDirection: 'row',
  119. marginTop: verticalScale(10),
  120. justifyContent: 'space-around',
  121. alignItems: 'center',
  122. }}>
  123. <Text style={styles.resulttext}>
  124. {searchCount == 0 ? total : searchCount} 个结果
  125. </Text>
  126. <SelectDropdown
  127. data={sortby}
  128. onSelect={(selectedItem, index) => {
  129. console.log(selectedItem, index);
  130. // 在这里进行排序
  131. if (selectedItem == 'A 到 Z') {
  132. const filterThisArray = [...filteredDataSource];
  133. filterThisArray.sort((a, b) => {
  134. const nameA = a.name.first.toLowerCase();
  135. const nameB = b.name.first.toLowerCase();
  136. if (nameA < nameB) {
  137. return -1;
  138. }
  139. if (nameA > nameB) {
  140. return 1;
  141. }
  142. return 0;
  143. });
  144. setFilteredDataSource(filterThisArray);
  145. }
  146. }}
  147. defaultButtonText={'排序方式'}
  148. buttonTextAfterSelection={(selectedItem, index) => {
  149. return selectedItem;
  150. }}
  151. rowTextForSelection={(item, index) => {
  152. return item;
  153. }}
  154. buttonStyle={styles.dropdown2BtnStyle}
  155. buttonTextStyle={styles.dropdown2BtnTxtStyle}
  156. renderDropdownIcon={(isOpened) => {
  157. return isOpened ? (
  158. <>
  159. <Image
  160. source={require('../assets/google.png')}
  161. style={{
  162. width: scale(15),
  163. height: verticalScale(15),
  164. marginRight: verticalScale(10),
  165. }}
  166. />
  167. </>
  168. ) : (
  169. <>
  170. <Image
  171. source={require('../assets/google.png')}
  172. style={{
  173. width: scale(15),
  174. height: verticalScale(15),
  175. marginRight: verticalScale(10),
  176. }}
  177. />
  178. </>
  179. );
  180. }}
  181. dropdownIconPosition={'right'}
  182. dropdownStyle={styles.dropdown2DropdownStyle}
  183. rowStyle={styles.dropdown2RowStyle}
  184. rowTextStyle={styles.dropdown2RowTxtStyle}
  185. />
  186. </View>
  187. <View style={{ marginTop: scale(10) }}>
  188. <SafeAreaView>
  189. {loading ? (
  190. <ActivityIndicator size="small" color="black" />
  191. ) : (
  192. <FlatList
  193. data={filteredDataSource}
  194. renderItem={renderItem}
  195. ItemSeparatorComponent={ItemSeparatorView}
  196. keyExtractor={(item, index) => index.toString()}
  197. />
  198. )}
  199. </SafeAreaView>
  200. </View>
  201. </View>
  202. );
  203. };
  204. export default App;
  205. const styles = StyleSheet.create({
  206. contenir: {
  207. flex: 1,
  208. },
  209. header: {
  210. flexDirection: 'row',
  211. justifyContent: 'space-between',
  212. alignItems: 'center',
  213. marginLeft: verticalScale(10),
  214. marginRight: verticalScale(10),
  215. },
  216. menuimg: { width: scale(40), height: verticalScale(40) },
  217. logoimg: {
  218. width: scale(60),
  219. height: verticalScale(60),
  220. marginTop: scale(10),
  221. },
  222. userimg: {
  223. width: scale(60),
  224. height: verticalScale(60),
  225. marginTop:
  226. <details>
  227. <summary>英文:</summary>
  228. Try this code. It worked for me.

import {
StyleSheet,
Text,
View,
Image,
TextInput,
FlatList,
ActivityIndicator,
SafeAreaView,
} from 'react-native';
import React, { useEffect, useState } from 'react';
import { scale, verticalScale, moderateScale } from 'react-native-size-matters';
import { RFPercentage } from 'react-native-responsive-fontsize';
import SelectDropdown from 'react-native-select-dropdown';

const sortby = ['A to Z', 'Newawst', 'Oldest'];

const App = () => {
const [loading, setLoading] = useState(true);
const [data, setData] = useState([]);
const [search, setSearch] = useState('');
const [filteredDataSource, setFilteredDataSource] = useState([]);
const [masterDataSource, setMasterDataSource] = useState([]);
const [total, setTotalData] = useState(0);
const [searchCount, setSearchCount] = useState(0);
const [sortedData, setSortedData] = useState([]);

  1. useEffect(() =&gt; {
  2. fetchData();
  3. }, []);
  4. const fetchData = async () =&gt; {
  5. try {
  6. const response = await fetch(&#39;https://randomuser.me/api?results=1000&#39;);
  7. const json = await response.json();
  8. const newData = json.results;
  9. setLoading(false);
  10. setFilteredDataSource(json.results);
  11. setMasterDataSource(json.results);
  12. setTotalData(json.info.results);
  13. } catch (error) {
  14. console.error(error);
  15. }
  16. };
  17. const searchFilterFunction = (text) =&gt; {
  18. // Check if searched text is not blank
  19. if (text) {
  20. // Inserted text is not blank
  21. // Filter the masterDataSource and update FilteredDataSource
  22. const newData = masterDataSource.filter(function (item) {
  23. // Applying filter for the inserted text in search bar
  24. const itemData = item.name.first
  25. ? item.name.first.toUpperCase()
  26. : &#39;&#39;.toUpperCase();
  27. const textData = text.toUpperCase();
  28. return itemData.indexOf(textData) &gt; -1;
  29. });
  30. setFilteredDataSource(newData);
  31. setSearch(text);
  32. setSearchCount(newData.length);
  33. } else {
  34. // Inserted text is blank
  35. // Update FilteredDataSource with masterDataSource
  36. setFilteredDataSource(masterDataSource);
  37. setSearch(text);
  38. }
  39. };
  40. const renderItem = ({ item }) =&gt; (
  41. &lt;View style={{ padding: 10 }}&gt;
  42. &lt;Text&gt;Name :{`${item.name.first} ${item.name.last}`}&lt;/Text&gt;
  43. &lt;Text&gt;Email :{item.email}&lt;/Text&gt;
  44. &lt;Text&gt;City :{item.location.city}&lt;/Text&gt;
  45. &lt;Text&gt;State :{item.location.state}&lt;/Text&gt;
  46. &lt;Text&gt;Country :{item.location.country}&lt;/Text&gt;
  47. &lt;Text&gt;Postcode :{item.location.postcode}&lt;/Text&gt;
  48. &lt;/View&gt;
  49. );
  50. const ItemSeparatorView = () =&gt; {
  51. return (
  52. // Flat List Item Separator
  53. &lt;View
  54. style={{
  55. height: 0.5,
  56. width: &#39;100%&#39;,
  57. backgroundColor: &#39;#000&#39;,
  58. }}
  59. /&gt;
  60. );
  61. };
  62. return (
  63. &lt;View&gt;
  64. &lt;View style={styles.header}&gt;
  65. &lt;Image source={require(&#39;../assets/google.png&#39;)} style={styles.menuimg} /&gt;
  66. &lt;Image source={require(&#39;../assets/google.png&#39;)} style={styles.logoimg} /&gt;
  67. &lt;Image source={require(&#39;../assets/google.png&#39;)} style={styles.userimg} /&gt;
  68. &lt;/View&gt;
  69. &lt;View style={{ alignItems: &#39;center&#39; }}&gt;
  70. &lt;View style={styles.textbox}&gt;
  71. &lt;TextInput
  72. style={styles.textInputStyle}
  73. onChangeText={(text) =&gt; searchFilterFunction(text)}
  74. value={search}
  75. focusable={true}
  76. underlineColorAndroid=&quot;transparent&quot;
  77. placeholder=&quot;Search&quot;
  78. placeholderTextColor={&#39;#000&#39;}
  79. selectionColor=&quot;#000&quot;
  80. /&gt;
  81. &lt;Image
  82. source={require(&#39;../assets/google.png&#39;)}
  83. style={{ marginRight: scale(10) }}
  84. /&gt;
  85. &lt;/View&gt;
  86. &lt;/View&gt;
  87. &lt;View
  88. style={{
  89. flexDirection: &#39;row&#39;,
  90. marginTop: verticalScale(10),
  91. justifyContent: &#39;space-around&#39;,
  92. alignItems: &#39;center&#39;,
  93. }}&gt;
  94. &lt;Text style={styles.resulttext}&gt;
  95. {searchCount == 0 ? total : searchCount} results
  96. &lt;/Text&gt;
  97. &lt;SelectDropdown
  98. data={sortby}
  99. // defaultValueByIndex={1}
  100. onSelect={(selectedItem, index) =&gt; {
  101. console.log(selectedItem, index);
  102. //check here
  103. if (selectedItem == &#39;A to Z&#39;) {
  104. const filterThisArray = [...filteredDataSource];
  105. filterThisArray.sort((a, b) =&gt; {
  106. const nameA = a.name.first.toLowerCase();
  107. const nameB = b.name.first.toLowerCase();
  108. if (nameA &lt; nameB) {
  109. return -1;
  110. }
  111. if (nameA &gt; nameB) {
  112. return 1;
  113. }
  114. return 0;
  115. });
  116. setFilteredDataSource(filterThisArray);
  117. }
  118. }}
  119. defaultButtonText={&#39;Sort By&#39;}
  120. buttonTextAfterSelection={(selectedItem, index) =&gt; {
  121. return selectedItem;
  122. }}
  123. rowTextForSelection={(item, index) =&gt; {
  124. return item;
  125. }}
  126. buttonStyle={styles.dropdown2BtnStyle}
  127. buttonTextStyle={styles.dropdown2BtnTxtStyle}
  128. renderDropdownIcon={(isOpened) =&gt; {
  129. return isOpened ? (
  130. &lt;&gt;
  131. &lt;Image
  132. source={require(&#39;../assets/google.png&#39;)}
  133. style={{
  134. width: scale(15),
  135. height: verticalScale(15),
  136. marginRight: verticalScale(10),
  137. }}
  138. /&gt;
  139. &lt;/&gt;
  140. ) : (
  141. &lt;&gt;
  142. &lt;Image
  143. source={require(&#39;../assets/google.png&#39;)}
  144. style={{
  145. width: scale(15),
  146. height: verticalScale(15),
  147. marginRight: verticalScale(10),
  148. }}
  149. /&gt;
  150. &lt;/&gt;
  151. );
  152. }}
  153. dropdownIconPosition={&#39;right&#39;}
  154. dropdownStyle={styles.dropdown2DropdownStyle}
  155. rowStyle={styles.dropdown2RowStyle}
  156. rowTextStyle={styles.dropdown2RowTxtStyle}
  157. /&gt;
  158. &lt;/View&gt;
  159. &lt;View style={{ marginTop: scale(10) }}&gt;
  160. &lt;SafeAreaView&gt;
  161. {loading ? (
  162. &lt;ActivityIndicator size=&quot;small&quot; color=&quot;black&quot; /&gt;
  163. ) : (
  164. &lt;FlatList
  165. data={filteredDataSource}
  166. renderItem={renderItem}
  167. ItemSeparatorComponent={ItemSeparatorView}
  168. keyExtractor={(item, index) =&gt; index.toString()}
  169. /&gt;
  170. )}
  171. &lt;/SafeAreaView&gt;
  172. &lt;/View&gt;
  173. &lt;/View&gt;
  174. );

};

export default App;

const styles = StyleSheet.create({
contenir: {
flex: 1,
},
header: {
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginLeft: verticalScale(10),
marginRight: verticalScale(10),
},
menuimg: { width: scale(40), height: verticalScale(40) },
logoimg: {
width: scale(60),
height: verticalScale(60),
marginTop: scale(10),
},
userimg: {
width: scale(60),
height: verticalScale(60),
marginTop: scale(10),
},
textInputStyle: {
// height: verticalScale(60),
width: scale(260),
fontSize: RFPercentage(3),
color: '#000',
fontWeight: '900',
// borderWidth: scale(2),
// paddingLeft:moderateScale(20),
// margin: scale(5),
// borderRadius:scale(20),
// borderColor: '#000',
// backgroundColor: '#FFFFFF',
},
textbox: {
alignItems: 'center',
height: verticalScale(65),
width: scale(320),
borderColor: '#000',
backgroundColor: '#FFFFFF',
borderRadius: scale(30),
borderWidth: scale(2),
paddingLeft: moderateScale(20),
margin: scale(5),
flexDirection: 'row',
justifyContent: 'space-between',
},
resulttext: {
fontSize: RFPercentage(3.7),
fontWeight: '700',
color: '#000',
// marginLeft: scale(20)
},
// scrollViewContainer: {
// justifyContent: 'space-around',
// },
dropdown2BtnStyle: {
width: scale(150),
height: verticalScale(60),
backgroundColor: '#000',
borderRadius: scale(30),
},
dropdown2BtnTxtStyle: {
color: '#FFF',
textAlign: 'center',
fontSize: RFPercentage(2.5),
fontWeight: 'bold',
},
dropdown2DropdownStyle: {
backgroundColor: '#444',
width: scale(120),
height: verticalScale(70),
borderRadius: scale(20),
// borderBottomRightRadius: 12,
},
dropdown2RowStyle: {
backgroundColor: '#000',
},
dropdown2RowTxtStyle: {
color: '#FFF',
textAlign: 'center',
},
});

  1. Don&#39;t forget to handle other options as well ;)
  2. </details>

huangapple
  • 本文由 发表于 2023年6月22日 13:04:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定