如何在React Native中按组显示数组值

阿比吉安·高拉夫(Abhigyan gaurav)

这在我的代码中customerSearch是一个数组,我得到了一些价值。现在,我试图以这种方式映射此值,例如是否将其显示在UI上,则应根据键将其分组。

就像customer该键中的键存在数组和两个值一样,因此第一个客户来此波纹管两个数组值,然后是第二个键及其值。其他键也一样。

 customerSearch[
      {
        "key": "customer",
        "data": [
          {
            "name": "John",
            "status": "Active",
            "nationalId": "NBGVH6676"
          },
          { "name": "Abhi",
            "status": "Active",
            "nationalId": "NBGVH6890"}
        ]
      },
      {
        "key": "requests",
        "data": [
          {
            "name": "Kartik",
            "status": "Active",
            "nationalId": "K0089"
          },
          { "name": "Ashi",
            "status": "Active",
            "nationalId": "AS420"

          }
        ]
      },
      {
        "key": "invoice",
        "data": [
          {
            "name": "John",
            "status": "Active",
            "nationalId": "IN998"
          },
          { "name": "Abhi2",
            "status": "Active",
            "nationalId": "ABh007"

          }
        ]
      },
      {
        "key": "offering",
        "data": [
          {},
          {}
        ]
      }
    ]

渲染功能中的以下代码

 <View style={{ marginLeft: 5, marginRight: 5, marginTop: 10, backgroundColor: '#f1f1f1' }}>
            {this.props.customerSearch.map(
              (data, index) => {
                return (
                  <View style={{ marginBottom: 10, marginLeft: 5, marginRight: 5 }} key={index}>
                    <Card>
                      <CardItem header style={{ backgroundColor: '#fff', width: '100%', justifyContent: 'space-between', borderBottomColor: '#f1f1f1', borderBottomWidth: 1 }}>
                        <View style={{ flexDirection: 'column', justifyContent: 'space-between' }}>
                          <View>
                            <RegularText text={`${data.key}`} style={{ fontWeight: 'bold', flexWrap: 'wrap' }} />
                            <SmallText text={` Name ${""}`} textColor="grey" />
                            <SmallText text={` Status ${""}`} textColor="grey" />
                            <SmallText text={` NationalId ${""}`} textColor="grey" />
                          </View>
                        </View>

                      </CardItem>

                    </Card>
                  </View>
                );
              })
            }
          </View>

UI显示示例
客户
名称
状态
国家ID
名称状态
国家ID

请求
名称状态
国家ID
名称状态
国家ID

都山

您可以使用React Native SectionList来实现布局,如下所示:

import React, { Component } from 'react';
import { SectionList, Text, View, SafeAreaView } from 'react-native';

const customerSearch = [
  {
    "key": "customer",
    "data": [
      {
        "name": "John",
        "status": "Active",
        "nationalId": "NBGVH6676"
      },
      {
        "name": "Abhi",
        "status": "Active",
        "nationalId": "NBGVH6890"
      }
    ]
  },
  {
    "key": "requests",
    "data": [
      {
        "name": "Kartik",
        "status": "Active",
        "nationalId": "K0089"
      },
      {
        "name": "Ashi",
        "status": "Active",
        "nationalId": "AS420"

      }
    ]
  },
  {
    "key": "invoice",
    "data": [
      {
        "name": "John",
        "status": "Active",
        "nationalId": "IN998"
      },
      {
        "name": "Abhi2",
        "status": "Active",
        "nationalId": "ABh007"

      }
    ]
  },
  {
    "key": "offering",
    "data": [
      {},
      {}
    ]
  }
]


export default class Example extends Component {

  renderItems = ({ item }) => (
    <View style={{padding: 5}}>
      <Text>{`Name - ${item.name}`}</Text>
      <Text>{`Status - ${item.status}`}</Text>
      <Text>{`NationalId - ${item.nationalId}`}</Text>
    </View>
  )

  render() {
    return (
      <SafeAreaView style={{ flex: 1, marginTop: 20 }}>
        <SectionList
          sections={customerSearch}
          keyExtractor={(item, index) => item + index}
          renderSectionHeader={({ section }) => (
            <Text style={{ fontSize: 20, backgroundColor: 'gray' }}>{section.key}</Text>
          )}
          renderItem={this.renderItems}
        />
      </SafeAreaView>
    );
  }
}

根据您的要求进行更改。

希望这对您有所帮助。如有疑问,请放心。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

如何在React Native App中显示超链接?

来自分类Dev

如何在React Native中显示图标?

来自分类Dev

如何在php中显示数组值?

来自分类Dev

如何在React Native中显示动画gif?

来自分类Dev

如何在react-native android应用中显示GIF?

来自分类Dev

如何在react中访问数组值?

来自分类Dev

如何在React Native中显示获取的数据

来自分类Dev

如何在React Native的两栏中显示卡片?

来自分类Dev

如何在React Native中水平显示来自API的数据

来自分类Dev

如何在React Native中为数组设置状态值?

来自分类Dev

如何在React Native中对数组进行分组

来自分类Dev

如何在React Native中的对象中获取特定值?

来自分类Dev

如何在react中显示文本数组?

来自分类Dev

如何在React Native中显示一次屏幕?

来自分类Dev

如何在React Native中显示来自Firebase Firestore的数据

来自分类Dev

如何在PrimeNG表中按json显示此组?

来自分类Dev

如何在React Native中突出显示单个按钮?

来自分类Dev

如何在React中显示数组的数组?

来自分类Dev

如何在React Native中从单独的组件创建对象数组?

来自分类Dev

如何在React Native中传递View / Component的数组?

来自分类Dev

如何在C中显示数组的值?

来自分类Dev

如何在ios中的react-native-tableview中显示数组数据

来自分类Dev

如何在全局变量 React Native 中设置数组值

来自分类Dev

如何在 React 中的数组 onClick 中显示特定对象?

来自分类Dev

如何在 React Native 中显示底部导航上方的组件?

来自分类Dev

如何在 React Native 中显示渐变 SVG 图像

来自分类Dev

如何在 react-native 中按 desc 或 asc 排序?

来自分类Dev

如何在 React Native 中显示 Facebook 用户名?

来自分类Dev

如何在 React Native 中显示 https 图像?

Related 相关文章

  1. 1

    如何在React Native App中显示超链接?

  2. 2

    如何在React Native中显示图标?

  3. 3

    如何在php中显示数组值?

  4. 4

    如何在React Native中显示动画gif?

  5. 5

    如何在react-native android应用中显示GIF?

  6. 6

    如何在react中访问数组值?

  7. 7

    如何在React Native中显示获取的数据

  8. 8

    如何在React Native的两栏中显示卡片?

  9. 9

    如何在React Native中水平显示来自API的数据

  10. 10

    如何在React Native中为数组设置状态值?

  11. 11

    如何在React Native中对数组进行分组

  12. 12

    如何在React Native中的对象中获取特定值?

  13. 13

    如何在react中显示文本数组?

  14. 14

    如何在React Native中显示一次屏幕?

  15. 15

    如何在React Native中显示来自Firebase Firestore的数据

  16. 16

    如何在PrimeNG表中按json显示此组?

  17. 17

    如何在React Native中突出显示单个按钮?

  18. 18

    如何在React中显示数组的数组?

  19. 19

    如何在React Native中从单独的组件创建对象数组?

  20. 20

    如何在React Native中传递View / Component的数组?

  21. 21

    如何在C中显示数组的值?

  22. 22

    如何在ios中的react-native-tableview中显示数组数据

  23. 23

    如何在全局变量 React Native 中设置数组值

  24. 24

    如何在 React 中的数组 onClick 中显示特定对象?

  25. 25

    如何在 React Native 中显示底部导航上方的组件?

  26. 26

    如何在 React Native 中显示渐变 SVG 图像

  27. 27

    如何在 react-native 中按 desc 或 asc 排序?

  28. 28

    如何在 React Native 中显示 Facebook 用户名?

  29. 29

    如何在 React Native 中显示 https 图像?

热门标签

归档