javascript - 从 QML 中的数组中的 bool 更改颜色

标签 javascript arrays boolean qml v-play

我希望通过数组 (calendarListModel) 中的 boolean 值更改日历单元格的颜色。

这是数组的示例:

[
{"date":1544443200000,"name":"Edward","status":1},
{"date":1544529600000,"name":"Katie","status":0},
{"date":1544616000000,"name":"Claire","status":1}
]

我想做的是根据 calendarListModel ("status": 0 or 1) 中的 boolean 值更改日历标记的颜色.

我的标记代码是;

Rectangle {
    id: calendarMarker
    visible: arrayFromFireBase.indexOf(styleData.date.getTime()) > -1
    anchors.fill: parent
    color:  "blue"
} 

我试过用诸如 calendarListModel.status 之类的东西来改变矩形的颜色? "blue": "red" 以及其他变体,但是是不是让每个单元格都变成蓝色或红色,或者根本没有?

问题:根据数组中的真/假值更改颜色的最佳方法是什么?

我正在使用 QtQuick.Controls 中的 Calendar 来显示日期,并使用 QtQuick.Controls.Styles 中的 CalendarStyle > 分配自定义样式。我也在使用 V-Play SDK。

这是我目前正在做的一个最小的、完整的、可验证的例子:

import VPlayApps 1.0
import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.1

App {
    id: app

    //  this model is read from a firebase db using v-play
    property var calendarListModel: [
        {"date":1544443200000,"name":"Edward","status":1},
        {"date":1544529600000,"name":"Katie","status":0},
        {"date":1547182800000,"name":"Claire","status":1}
    ]

    Calendar {
        id: calendar
        anchors.fill: parent
        selectedDate: new Date()
        weekNumbersVisible: true
        focus: true
        onSelectedDateChanged: {
            const day = selectedDate.getDate();
            const month = selectedDate.getMonth() + 1;
            const year = selectedDate.getFullYear();
        }

       style: CalendarStyle {
           dayOfWeekDelegate: Item {
               width: parent.width
               height: dp(30)
               Rectangle {
                   anchors.fill: parent
                   border.color: "#00000000"
                   Label {
                       id: dayOfWeekDelegateText
                       text: Qt.locale().dayName(styleData.dayOfWeek, Locale.ShortFormat)
                       anchors.centerIn: parent
                       color: "black"
                   }
               }
           }

           //  a delegate for each day cell
           dayDelegate: Item {
               id: container

               readonly property color sameMonthDateTextColor: "#444"
               readonly property color selectedDateColor: "#20d5f0"
               readonly property color selectedDateTextColor: "white"
               readonly property color differentMonthDateTextColor: "#bbb"

               //  the background of each cell
               Rectangle {
                   anchors.fill: parent
                   border.color: "#00000000"
                   color: styleData.selected ? selectedDateColor : "white"
               }

               //  a marker on the upper-left corner on each cell
               Rectangle {
                   id: calendarMarker
                   property bool isConfirmed: false
                   anchors {
                       top: parent.top; left: parent.left
                   }

                   width: 12
                   height: width

                   //  the issue lies here
                   color: {
                        var confCol
                        calendarListModel.indexOf(status? true : false)
                        confCol ? "#4286f4" : "red"
                   }

               }

               // the day number
               Label {
                   id: dayDelegateText
                   text: styleData.date.getDate()
                   anchors.centerIn: parent
                   color:  styleData.selected ? selectedDateTextColor : styleData.visibleMonth ? sameMonthDateTextColor : differentMonthDateTextColor
               }
            }
        }
    }
}

最佳答案

好吧,现在您正在使用一个对象数组并尝试直接对其进行索引,这并不好。 (特别是因为您目前正在为 true/false 值编制索引,这些值不包含 calendarListModel 中的单个元素。)即使您可以用日期、名称和状态重建对象,它们仍然是不同的对象。

使用Array.prototype.find()相反。

color: {
    var modelObject = calendarListModel.find(
       function(obj) {
            //  look for a matching date
           return obj.date === styleData.date.getTime();
       }
    );

    if (modelObject === undefined)  //  not found in list model
       return "lightgrey";

    return modelObject.status ? "blue" : "red";
}

这是在 macOS 上测试前后的对比:

之前:注意所有标记都是红色的。只有 calendarListModel 中指定的日期应该有颜色。 Before

之后:请注意,只有 1 月 11 日的标记有颜色(蓝色)。事实上,这是因为日期包含在 calendarListModel 中:1544616000000。 After

关于javascript - 从 QML 中的数组中的 bool 更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54014711/

相关文章:

javascript - 通过javascript打开Outlook

javascript - 是否可以在 Knockout.js 中将 valueUpdate :'afterkeydown' 设置为默认 valueUpdate?

javascript - 属性选择器不起作用

python - 获取索引数组,在另一个数组中找到匹配的索引并替换值

javascript - 仅当将真正的 boolean 值传递给函数时才打印输出

javascript - MongoDB 中支持 session 的 Node.js 框架

javascript - 从数组中删除几个单词 - Javascript

arrays - 如何通过 VBA 函数填充 Excel 工作表中的单元格?

javascript - 如何遍历JSON数组?

python - 使用 boolean 逻辑合并和过滤一个数据帧的多列