在 listView QML 中使用嵌套 JSON 作为 section.property

Using Nested JSON as section.property in listView QML

我正在创建一个 listView,我的代表及其信息来自 JSON - 我的问题是我试图从 [=] 中的嵌套区域设置 section.property 59=]。嵌套数据将是动态的,因此我需要以适应每个应用程序用户的方式工作。

我正在使用的文档是: JsonListModel & SortFilterProxyModel

我的 JSON 的一个例子是;

[{
"firstname":"Edward",
"subGroup":"Reception",
"admin":1,
"roles":
    {"Assistant":0,"Reception":1,"Stylist":1,"Technical":0
    }
},
{
"firstname":"Claire",
"subGroup":"Stylist",
"admin":1,
"roles":
    {"Assistant":1,"Reception":0,"Stylist":1,"Technical":0
    }
}]

我正在使用 JsonListModel、QT Creator 工作,我的代码的最小部分如下:

import Felgo 3.0
import QtQuick 2.0

Page {
id: editAdmins


property var newArr: dataModel.calendarUserItems.users
property var userRoles: ({})
property var l

JsonListModel { // list model for json data
    id: jsonModel
    source: dataModel.jsonList // my full JSON
    keyField: "firstname " + " surname" 
    fields: ["firstname", "surname", "subGroup", "admin", "email", "roles"]
}

SortFilterProxyModel { // SortFilterProxyModel for sorting or filtering lists
    id: sortedModel
   // Note: when using JsonListModel, the sorters or filter might not be applied correctly when directly assigning sourceModel
   // use the Component.onCompleted handler instead to initialize SortFilterProxyModel
    Component.onCompleted: sourceModel = jsonModel
    sorters: [
        RoleSorter {
            id: groupSorter
            roleName: "subGroup"
        },
    ]
}

AppListView {
    id:view
    model: sortedModel
    anchors.fill: parent

    section.property: "subGroup" //How I have used it previously before needing to set 'roles' as my property (which shows nothing)
    section.delegate: SimpleSection {}

delegate: SimpleRow {
    id: container

    text: model.firstname
    detailText: model.surname
    }
}
}

根据上面的 JSON,预期的结果是我的 listView 在 roles 嵌套中有 4 个部分。 Assistant, Reception, Stylist, Technical

我的问题是: 我想使用 roles 作为我的 ListView {section.property},并且用户的可见性取决于他们在该角色中的布尔值 - 所以从上面的例子来看; Edward 将同时出现在接待处和造型师中,而 Claire 将同时出现在助理和造型师中!

如何将嵌套的 JSON 定义为列表中的部分 属性?

------更新------

通过在我的 SortFilterProxyModel { } 中添加一个额外的 proxyRole,如图所示:

proxyRoles: ExpressionRole {
            name: "role"
            expression: JSON.stringify(model.roles)
       }

并将我的 section.property: 更改为 "role" 我可以为那些具有匹配角色的人创建部分,更接近,但仍然不是预期的(参见随附的屏幕截图)

我进一步尝试循环遍历数据,有多种变体:

        expression: {
            for(var x in model.roles){
                if(model.roles[x] === 1){
                    x
                }
            }
        }

并返回最终索引(正如上面所预期的那样)或再次将项目推入新数组但仍将它们全部作为结果返回。

ListViews 从他们的模型数据中创建每个委托,这意味着委托不能像我试图做的那样有效地复制,这取决于用户角色中的布尔值。

我纠正了这个问题,在创建数组时,任何在其角色中具有多个布尔值的用户都会为每个角色获得一个额外的数组对象,并将我的 section.property 指向每个单独的角色名称。

从下面的代码中,Edward 将创建 2 个数组对象,然后充当 'duplicate' 委托。

[{
"firstname":"Edward",
"subGroup":"Reception",
"admin":1,
"roles":
    {"Assistant":0,"Reception":1,"Stylist":1,"Technical":0
    }
},
{
"firstname":"Claire",
"subGroup":"Stylist",
"admin":1,
"roles":
    {"Assistant":1,"Reception":0,"Stylist":1,"Technical":0
    }
}]