如何在React Native中以字母顺序设置标题?

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

How to set header in Alphabetic way in react native?

问题

  1. # **输出**
  2. [![在这里输入图片描述](https://i.stack.imgur.com/0PkVA.jpg)](https://i.stack.imgur.com/0PkVA.jpg)
  3. # **代码**
  4. ```jsx
  5. import React from 'react';
  6. import {
  7. Text,
  8. View,
  9. FlatList,
  10. ScrollView,
  11. SafeAreaView
  12. } from 'react-native';
  13. import { nameData } from './dummydata';
  14. const windowSize = FlatList.length > 50 ? FlatList.length / 4 : 21;
  15. const Main = () => {
  16. return (
  17. <View style={{}}>
  18. <SafeAreaView style={{}}>
  19. <ScrollView style={{}}>
  20. <FlatList
  21. disableVirtualization={true}
  22. //data={nameData.sort((a, b) => a.name.localeCompare(b.name))}
  23. data={nameData.sort(function (a, b) {
  24. return (a.name < b.name) ? -1 : (a.name > b.name) ? 1 : 0;
  25. })}
  26. renderItem={({ item }) => (
  27. <Text style={{ fontSize: 20,marginLeft:10,padding:5 }}>{item.name}</Text>
  28. )}
  29. getItemLayout={(data, index) => ({
  30. length: 80,
  31. offset: 80 * index,
  32. index,
  33. })}
  34. removeClippedSubviews={true}
  35. maxToRenderPerBatch={windowSize}
  36. windowSize={windowSize}
  37. numColumns={1}
  38. keyExtractor={(item, index) => String(index)}
  39. contentContainerStyle={{ paddingBottom: 10 }}
  40. />
  41. </ScrollView>
  42. </SafeAreaView>
  43. </View>
  44. );
  45. };
  46. export default Main;

我的数据已经排序,我想要这样:如果数据以字母A开头,那么这些数据应该包含在A标题部分中;如果数据以字母B开头,那么这些数据应该包含在B标题部分中。

就像这样,D标题只包含以D开头的数据,C标题只包含以C开头的数据。

但我不知道如何设置标题以及如何将数据放入标题部分。

简而言之,我想要的效果如下所示:

如何在React Native中以字母顺序设置标题?

有人能给我一个解决方案吗?

谢谢!🎊

  1. <details>
  2. <summary>英文:</summary>
  3. # **output**
  4. [![enter image description here](https://i.stack.imgur.com/0PkVA.jpg)](https://i.stack.imgur.com/0PkVA.jpg)
  5. # **code**

import React from 'react';
import {
Text,
View,
FlatList,
ScrollView,
SafeAreaView
} from 'react-native';

import { nameData } from './dummydata';

const windowSize = FlatList.length > 50 ? FlatList.length / 4 : 21;

const Main = () => {
return (
<View style={{}}>
<SafeAreaView style={{}}>
<ScrollView style={{}}>

  1. &lt;FlatList
  2. disableVirtualization={true}
  3. //data={nameData.sort((a, b) =&gt; a.name.localeCompare(b.name))}
  4. data={nameData.sort(function (a, b) {
  5. return (a.name &lt; b.name) ? -1 : (a.name &gt; b.name) ? 1 : 0;
  6. })}
  7. renderItem={({ item }) =&gt; (
  8. &lt;Text style={{ fontSize: 20,marginLeft:10,padding:5 }}&gt;{item.name}&lt;/Text&gt;
  9. )}
  10. getItemLayout={(data, index) =&gt; ({
  11. length: 80,
  12. offset: 80 * index,
  13. index,
  14. })}
  15. removeClippedSubviews={true}
  16. maxToRenderPerBatch={windowSize}
  17. windowSize={windowSize}
  18. numColumns={1}
  19. keyExtractor={(item, index) =&gt; String(index)}
  20. contentContainerStyle={{ paddingBottom: 10 }}
  21. /&gt;
  22. &lt;/ScrollView&gt;
  23. &lt;/SafeAreaView&gt;
  24. &lt;/View&gt;

);
};

export default Main;

  1. **I have given the code above**
  2. my data is sorted , I want like this if data is started A alphabet then this data contain in A header section if data is started B alphabet then this data contain in B header section.
  3. like this D header contain only D Starting data C header contain only C Starting data
  4. but I don&#39;t know how to set header and how to set data in header section.
  5. in sort I want like this data
  6. [![enter image description here](https://i.stack.imgur.com/X8nvK.png)](https://i.stack.imgur.com/X8nvK.png)
  7. anybody can give me solution?
  8. **thank you!&#128060;**
  9. </details>
  10. # 答案1
  11. **得分**: 0
  12. **方法1**
  13. 尝试使用这个包 [react-native-section-alphabet-list][1]。
  14. 一个简单的React Native组件,它接受一个数据数组并以字母顺序(或自定义顺序)渲染一个SectionList
  15. **方法2**
  16. 更新您的代码如下并尝试:
  17. ```javascript
  18. import React, { useEffect, useState } from "react";
  19. import { Text, View, FlatList, SafeAreaView } from "react-native";
  20. import { nameData } from "./duumydata";
  21. const windowSize = FlatList.length > 50 ? FlatList.length / 4 : 21;
  22. const Main = () => {
  23. let listData = new Array(26).fill([]);
  24. const headerArray = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
  25. const [data, setData] = useState([]);
  26. useEffect(() => {
  27. nameData.sort(function (a, b) {
  28. return a.name.toUpperCase() < b.name.toUpperCase()
  29. ? -1
  30. : a.name > b.name
  31. ? 1
  32. : 0;
  33. });
  34. headerArray.forEach((header, index) => {
  35. nameData.forEach((item) => {
  36. const headerText = item.name.split("")[0].toUpperCase();
  37. if (header == headerText) {
  38. listData[index] = [...listData[index], item];
  39. }
  40. });
  41. });
  42. setData(listData);
  43. }, []);
  44. const renderItem = ({ item, index }) => {
  45. return (
  46. <>
  47. <View style={{ backgroundColor: "grey" }}>
  48. <Text>{headerArray[index]}</Text>
  49. </View>
  50. {item?.map((obj) => renderName(obj))}
  51. </>
  52. );
  53. };
  54. const renderName = (item) => {
  55. return (
  56. <View>
  57. <Text>{item.name}</Text>
  58. </View>
  59. );
  60. };
  61. return (
  62. <View style={{}}>
  63. <SafeAreaView style={{}}>
  64. <FlatList
  65. disableVirtualization={true}
  66. data={data}
  67. renderItem={(item) => renderItem(item)}
  68. getItemLayout={(data, index) => ({
  69. length: 80,
  70. offset: 80 * index,
  71. index
  72. })}
  73. removeClippedSubviews={true}
  74. maxToRenderPerBatch={windowSize}
  75. windowSize={windowSize}
  76. numColumns={1}
  77. keyExtractor={(item, index) => String(index)}
  78. contentContainerStyle={{ paddingBottom: 10 }}
  79. />
  80. </SafeAreaView>
  81. </View>
  82. );
  83. };
  84. export default Main;
英文:

Method 1

Try this package react-native-section-alphabet-list

A simple React Native component that takes an array of data and renders a SectionList with alphabetically (or custom) sorted data.

如何在React Native中以字母顺序设置标题?

Method 2

Update your code like below and try

  1. import React, { useEffect, useState } from &quot;react&quot;;
  2. import { Text, View, FlatList, SafeAreaView } from &quot;react-native&quot;;
  3. import { nameData } from &quot;./duumydata&quot;;
  4. const windowSize = FlatList.length &gt; 50 ? FlatList.length / 4 : 21;
  5. const Main = () =&gt; {
  6. let listData = new Array(26).fill([]);
  7. const headerArray = [&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;E&quot;,&quot;F&quot;,&quot;G&quot;,&quot;H&quot;,&quot;I&quot;,&quot;J&quot;,&quot;K&quot;,&quot;L&quot;,&quot;M&quot;,&quot;N&quot;,&quot;O&quot;,&quot;P&quot;,&quot;Q&quot;,&quot;R&quot;,&quot;S&quot;,&quot;T&quot;,&quot;U&quot;,&quot;V&quot;,&quot;W&quot;,&quot;X&quot;,&quot;Y&quot;,&quot;Z&quot;];
  8. const [data, setData] = useState([]);
  9. useEffect(() =&gt; {
  10. nameData.sort(function (a, b) {
  11. return a.name.toUpperCase() &lt; b.name.toUpperCase()
  12. ? -1
  13. : a.name &gt; b.name
  14. ? 1
  15. : 0;
  16. });
  17. headerArray.forEach((header, index) =&gt; {
  18. nameData.forEach((item) =&gt; {
  19. const headerText = item.name.split(&quot;&quot;)[0].toUpperCase();
  20. if (header == headerText) {
  21. listData[index] = [...listData[index], item];
  22. }
  23. });
  24. });
  25. setData(listData);
  26. }, []);
  27. const renderItem = ({ item, index }) =&gt; {
  28. return (
  29. &lt;&gt;
  30. &lt;View style={{ backgroundColor: &quot;grey&quot; }}&gt;
  31. &lt;Text&gt;{headerArray[index]}&lt;/Text&gt;
  32. &lt;/View&gt;
  33. {item?.map((obj) =&gt; renderName(obj))}
  34. &lt;/&gt;
  35. );
  36. };
  37. const renderName = (item) =&gt; {
  38. return (
  39. &lt;View&gt;
  40. &lt;Text&gt;{item.name}&lt;/Text&gt;
  41. &lt;/View&gt;
  42. );
  43. };
  44. return (
  45. &lt;View style={{}}&gt;
  46. &lt;SafeAreaView style={{}}&gt;
  47. &lt;FlatList
  48. disableVirtualization={true}
  49. data={data}
  50. renderItem={(item) =&gt; renderItem(item)}
  51. getItemLayout={(data, index) =&gt; ({
  52. length: 80,
  53. offset: 80 * index,
  54. index
  55. })}
  56. removeClippedSubviews={true}
  57. maxToRenderPerBatch={windowSize}
  58. windowSize={windowSize}
  59. numColumns={1}
  60. keyExtractor={(item, index) =&gt; String(index)}
  61. contentContainerStyle={{ paddingBottom: 10 }}
  62. /&gt;
  63. &lt;/SafeAreaView&gt;
  64. &lt;/View&gt;
  65. );
  66. };
  67. export default Main;

huangapple
  • 本文由 发表于 2023年1月9日 14:44:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75053910.html
匿名

发表评论

匿名网友

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

确定