如何用数据中的对象嵌套循环渲染截面清单



i具有以下JSON结构

{
  "title": "Name(s)",
  "type": "Text",
  "data": [
    {
      "source": "DB",
      "title": "All",
      "list": [
        {
          "name": "ABCD",
          "count": 1
        },
        {
          "name": "BCDE",
          "count": 1
        },
        {
          "name": "CDEF",
          "count": 1
        },
        {
          "name": "DEFG",
          "count": 2
        },
        {
          "name": "EFGH",
          "count": 1
        }
      ]
    }
  ]
},
{
  "title": "Category(s)",
  "type": "Text",
  "data": [
    {
      "source": "DB",
      "title": "All",
      "list": [
        {
          "name": "Vegetables",
          "count": 1942
        },
        {
          "name": "Saloon",
          "count": 355
        },
        {
          "name": "General Store",
          "count": 331
        },
        {
          "name": "Restaurants",
          "count": 130
        },
        {
          "name": "Fast Food",
          "count": 108
        }
      ]
    }
  ]
}

我试图显示数据

第一部分header:"名称"
1 strow:" abcd"
2ndrow" bcde"
3rdrow" cdef"


第二部分header:"类别"1斯特罗:"蔬菜"
2ndrow"轿车"
3RDROW"杂货店"



在这里,我应该使用sectionList/flatlist/混合两个以获得上述结果。

在flatlist/sectionList中,我获得了"标题名称"&Rendersectionheader中的类别,但在RenderItem中,如何循环对象的"列表"数组。请让我知道

您必须如下更新数据,

eg:

[
  {
    "title": "Name(s)",
    "type": "Text",
    "data": [
      {
        "name": "ABCD",
        "count": 1
      },
      {
        "name": "BCDE",
        "count": 1
      },
      ...
    ]
  },
  {
    "title": "Category(s)",
    "type": "Text",
    "data": [
      {
        "name": "Vegetables",
        "count": 1942
      },
      {
        "name": "Saloon",
        "count": 355
      },
      ...
    ]
  }
...
]

并使用SectionList进行显示,

eg:

 ...
    <SectionList
      renderItem={({item, index, section}) => <Text key={index}>{item.name}</Text>}
      renderSectionHeader={({section: {title}}) => (
        <Text style={{fontWeight: 'bold'}}>{title}</Text>
      )}
      sections={this.state.data}
      keyExtractor={(item, index) => item + index}
    />
    ...

相关内容

  • 没有找到相关文章

最新更新