简体   繁体   English

处理ListView的嵌套JSON数据

[英]handling nested JSON data for ListView

I am looking to access nested JSON data from a firebase database to use within a listView , this is something I have applied throughout my app, but am now having trouble having to access children of the data with dynamic properties. 我正在寻找从firebase数据库访问嵌套JSON数据以在listView使用的方法,这是我在整个应用程序中应用的方法,但现在遇到了必须访问具有动态属性的数据子级的麻烦。

An export of my JSON data from my data base is; 从我的数据库中导出我的JSON数据是;

{
    "T&G Canary Wharf" : {
        "Administrator" : {
            "1550633367665" : {
                "date" : "2019-02-12T12:00:00.000",
                "details" : "Full Day",
                "name" : "Edward Lawrence",
                "status" : "pending"
            },
            "1550633370715" : {
                "date" : "2019-02-13T12:00:00.000",
                "details" : "Full Day",
                "name" : "Edward Lawrence",
                "status" : false
            },
            "1550845072137" : {
                "date" : "2019-02-12T12:00:00.000",
                "details" : "Full Day",
                "name" : "Katie Prescott ",
                "status" : 1
            },
        },
        "Stylist" : {
            "1551222170677" : {
            "date" : "2019-02-19T12:00:00.000",
            "details" : "Full Day",
            "name" : "Stylist Stylist",
            "status" : "pending"
      }
    }
  }
}

Per user of the app, group (in the above example T&G Canary Wharf) will always be set, but the subgroup (in the example Administrator & Stylists) is dynamic for admins, and this is where I am struggling, 对于该应用程序的每个用户,始终会设置group (在上面的示例中,T&G Canary Wharf),但是对于管理员来说, subgroup (在示例中的Administrator&Stylists)是动态的,这就是我在努力的地方,

My code for my firebase database read is below: 我为firebase数据库读取的代码如下:

I am using felgo(formerly v-play), a link to their firebase documentation is: 我正在使用felgo(以前为v-play),其Firebase文档的链接为:

Felgo Firebase Plugin Felgo Firebase插件

App {

    property var adminModel: []
    property var groupName //the group name is assigned onLoggedIn, in this example group name === T&G Canary Wharf

    FirebaseDatabase {
        onLoggedIn: { 
        firebaseDb.getValue("groups" + "/" + groupName, {
                                orderByValue: true
                            }, function(success, key, value) {
                            if(success){
                            console.log("ADMIN MODEL: " + JSON.stringify(value))
                            adminModel = value // my array where I intend to push the data too                              
                            }
                        })
                    }
                }
            }

I have been reading through; 我一直在阅读;

access/process nested objects, arrays or JSON 访问/处理嵌套对象,数组或JSON

(super helpful by the way) specifically the section of What if the property names are dynamic and I don't know them beforehand? (顺便说一句超级有帮助),特别是“如果属性名称是动态的并且我事先不知道该怎么办?”部分。

I can create two list entries, of Administrator and Stylist (the subGroups, but where the child key of this is also dynamic (the time the entry is created, eg: "1550633367665"), I cannot seem to get further? 我可以创建两个列表条目,即AdministratorStylist (子组),但是其中的子键也是动态的(创建条目的时间,例如:“ 1550633367665”),我似乎无法进一步?

to create this model I have the code: 要创建此模型,我需要以下代码:

ListPage {
    id: adminPage

    model: Object.keys(adminModel)

delegate: SwipeOptionsContainer {
    id: container

    rightOption:  SwipeButton {
        anchors.fill: parent
        backgroundColor: "red"
        icon: IconType.trash

        onClicked: {
            container.hideOptions()
        }
    }

    SimpleRow {
        id: userRow
        width: parent.width
        text: modelData
        }
    }
}

My question is 我的问题是

How can I create a listView with the delegates being any object containing a "status" : "pending" - done in other areas using a loop of if (value[i].status === pending) { arr.push({...})} , but whilst the subgroup (Stylist/Administrator) is unknown, from the above database example there would be 2 list elements, but in reality will contain many more with multiple subGroup 's. 我该如何创建一个listView ,使委托为包含"status" : "pending"任何对象-使用if (value[i].status === pending) { arr.push({...})} ,但是虽然subgroup (Stylist/Administrator)是未知的,但是从上面的数据库示例中,将有2个列表元素,但实际上将包含更多具有多个subGroup

After doing getValue from FireBase, it seems like you need to perform a few things: 从FireBase执行getValue之后,似乎您需要执行一些操作:

  1. Retrieve the subgroups from your group. 从您的组中检索子组。
  2. Iterate through the subgroups. 遍历子组。
  3. For each subgroup, retrieve the time entries. 对于每个子组,检索时间条目。
  4. Filter the time entries based on whether their status is "pending" . 根据时间条目的状态是否为"pending"过滤时间条目。

It looks like you've already achieved Step 1 with Object.keys(adminModel) . 看来您已经用Object.keys(adminModel)实现了步骤1。 Now we'll take that a couple steps further with a for-loop (Step 2 √). 现在,我们将使用for循环(步骤2√)进一步走几步。

var subgroups = Object.keys(adminModel);
for (var i in subgroups) {
    var subgroup = adminModel[subgroups[i]];

    // ...
}

For convenience, we've defined subgroup_obj which will store the data of a subgroup. 为了方便起见,我们定义了subgroup_obj ,它将存储subgroup_obj的数据。 Eg 例如

"Stylist": {
      "1551222170677" : {
           "date" : "2019-02-19T12:00:00.000",
           "details" : "Full Day",
           "name" : "Stylist Stylist",
           "status" : "pending"
      }
}

We then go on to Step 3, retrieving time entries by again using Object.keys() but this time on the subgroup. 然后,我们继续执行步骤3,再次使用Object.keys()检索时间条目,但这一次是在子组上。

var timeEntries = Object.keys(subgroup);

Since Object.keys() returns an array, we can accomplish Step 4 by using the filter() array method to filter entries with a pending status. 由于Object.keys()返回一个数组,我们可以通过使用filter()数组方法过滤具有待处理状态的条目来完成步骤4。

var filteredEntries = timeEntries.filter(function(t) { return subgroup[t].status === "pending"; } );

With newer version of JS, you could do timeEntries.filter(t => subgroup[t].status === "pending") but this doesn't seem to be fully supported by Qt yet. 使用较新版本的JS,您可以执行timeEntries.filter(t => subgroup[t].status === "pending")但是Qt似乎尚未完全支持它。

And that's that. 就是这样。 filteredEntries gives us an array of time entries (eg [ "1551222170677" ] ) for each subgroup. filteredEntries为我们提供了每个子组的时间条目数组(例如[ "1551222170677" ] )。

If you want the complete time entry object, you could use the map array method to get 如果需要完整的时间输入对象,则可以使用map数组方法获取

var filteredEntries2 = filteredEntries.map(function(t) { return subgroup[t]; });

which gives you an array of objects. 这给了你一系列对象。 So something like 所以像

[
{
    date: "2019-02-19T12:00:00.000"
    details: "Full Day"
    name: "Stylist Stylist"
    status: "pending"
}
]

Hope this helps! 希望这可以帮助!

When you get data from the Firebase Database, you get back DataSnapshot that contains a forEach method which you can use to loop over all child nodes. 从Firebase数据库获取数据时,您将获得DataSnapshot ,该数据快照包含一个forEach方法,可用于循环所有子节点。

So to load a group and show the names of all items: 因此,要加载组并显示所有项目的名称:

firebase.database().ref("group").once("value").then(function(snapshot) {
  snapshot.forEach(function(groupSnapshot) {
    groupSnapshot.forEach(function(subgroupSnapshot) {
      console.log(subgroupSnapshot.val().name);
    })
  })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM