如果没有可用的modelData,我可以用Image替换SimpleRow

时间:2019-04-01 23:50:17

标签: qt listview model qml v-play

我有一个ListView,当前显示的modelData会随着按钮在几个部门选项之间的循环而变化。如果这些部门之一没有数据,我的delegate会继续显示以前的列表数据,直到到达带有数据的modelData的新部分为止。

我想做的是,当模型为“空”时(未定义,当要查找的键尚未在我的Firebase数据库中创建,或者当前不可见任何项时,可能会发生)而是显示图像;即“现在进行中,这里什么也看不到”。

我的模型是从JSON提取的,下面是一个示例。并且我的calendarUserItems是Firebase数据库中多个子节点的根节点,我的AppButton.groupCycle的目的也为每个子节点增加了一个方向,以此过滤数据以在内部查看和编辑页。

我的代码示例是:

Page {
id: adminPage

property var departments: [1,2,3,4]
property int currGroupIndex: 0

    AppButton {
        id: groupCycle   
        text: "Viewing: " + departments[currGroupIndex]
        onClicked: {
            if (currGroupIndex == departments.length - 1)
                currGroupIndex = 0;
            else
                currGroupIndex++;
        }
    }

    ListView {

        model: Object.keys(dataModel.calendarUserItems[departments[currGroupIndex]])

        delegate: modelData.visible ? currentGroupList : emptyHol

        Component {
            id: emptyHol
            AppText {
                text: "nothing to see here move along now!"
            }
        }
        Component {
            id: currentGroupList
            SimpleRow {
                id: container                

                readonly property var calendarUserItem: dataModel.calendarUserItems[departments[currGroupIndex]][modelData] || {}

                visible: container.calendarUserItem.status === "pending" ? true : false
                 // only pending items visible
                 // remaining code for simple row
            }
        }
    }
}

我的dataModel.calendarUserItems中的JSON示例是:

"groupName": [
    { "department1": 
        { "1555111624727" : {
              "creationDate" : 1555111624727,
              "date" : "2019-03-15T12:00:00.000",
              "name" : "Edward Lawrence",
              "status": "pending"
             },
//several of these entries within department1
        },
    },
    { "department2":
        { "1555111624727" : {
              "creationDate" : 1555111624456,
              "date" : "2019-05-1T12:00:00.000",
              "name" : "Katie P",
              "status": 1
             },
//several of these entries within department2
        },
    }
//departments 3 & 4 as the same
]

如果部门2和3拥有modelData,而部门1和4没有,我希望显示文本,并清空ListView,而不是显示前一个modelData

我已经尝试过使用图像/文本可见性,但是问题更多在于清除modelData,我不确定从哪里开始?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我已经通过使用以下内容作为我的代表来实现显示:

    delegate: {
        if(!(departments[currGroupIndex] in dataModel.calendarUserItems) )
            return emptyHol
        var subgroups = Object.keys(dataModel.calendarUserItems[departments[currGroupIndex]]);
        for (var i in subgroups) {
            var subgroup = dataModel.calendarUserItems[departments[currGroupIndex]][subgroups[i]];
            modelArr.push(subgroup)
            console.log(modelArr)
        }
        var modelObect = modelArr.find(
                    function(obj) {
                        return obj.status === "pending";
                    }
                    );

        if(modelObect === undefined)
            return emptyHol;

        return currentGroupList;
    }

然后,当按下我的AppButton.groupCycle时,我添加了modelArr = []来清除每次按下时的数组,这按预期工作。

谢谢!