如何使用数据中的对象的嵌套循环呈现SectionList

时间:2019-06-27 08:10:42

标签: react-native react-native-android react-native-ios react-native-flatlist react-native-sectionlist

我具有以下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
        }
      ]
    }
  ]
}

我正在尝试将数据显示为赞

第一节标题:“名称”
1stRow:“ ABCD”
2ndRow“ BCDE”
3rdRow“ CDEF”


第二节标题:“类别” 1stRow:“蔬菜”
第二排“沙龙”
3rdRow“综合商店”



在这里,我应该使用SectionList / Flatlist /混合两者以获得上述结果。

在flatlist / sectionlist中,我在renderSectionHeader中但在renderItem中得到了节头的名称和类别。 我如何循环对象的“列表”数组。 请让我知道

1 个答案:

答案 0 :(得分:1)

您必须像以下那样更新数据,

例如:

SectionList

并使用 ... <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} /> ... 进行显示,

例如:

from pynput.keyboard import Listener

def on_press(key):
    print(key)

print("Started")

with Listener(on_press=on_press) as listener:
    listener.join()