【问题标题】:Loop through ListView for matching calendar selectedDate循环 ListView 以匹配日历 selectedDate
【发布时间】:2019-02-22 02:05:26
【问题描述】:

我希望使用数组中的数据(calendarListModel)在日历的当前 selectedDate 上填充 listView 的事件

当从日历中选择新日期时,如果在新选择的日期不存在任何事件,我需要更新列表,清除并保持空白,或者用与新选择的日期匹配的新代表替换 listView。

我的数组是通过从 firebase 数据库读取创建的,它按预期工作。我的数组的一个例子是;

calendarListModel: [
    {"date":2019-02-12,"name":"user1"},
    {"date":2019-02-13","name":"user1"},
    {"date":2019-02-12,"name":"user2"}
]

如果我将模型设置为calendarListModel,我的列表会显示每个数据库条目,无论listView 上的日期如何。

我尝试过诸如;

model: calendarListView.date(calendar.selectedDate

还使用循环访问数据,我没有成功,最近还有以下示例;

function updateEvents() {
                    var eventModel = calendarListModel.find(
                                function(obj){
                                return obj.date === calendar.selectedDate.getDate(),
                                console.log(JSON.stringify(obj));
                                }
                            );
                    if (eventModel === undefined)
                        return eventListModel.length = [];
                    return eventListModel.push(eventModel)
                }

Calendar {
        id: calendar
        selectedDate: new Date()

        onSelectedDateChanged: {
            const day = selectedDate.getDate();
            const month = selectedDate.getMonth() + 1;
            const year = selectedDate.getFullYear();
            updateEvents()
        }
    }

            ListView {
            id:eventListView
            model: eventListModel
        }

来自JSON.stringify(obj) 的控制台日志似乎将我的数组拆分为单独的对象,日志显示:

{"date":1549972800000,"name":"user1"} {"date":1550059200000,"name":"user1"} {"date":1549972800000,"name":"user2"}

但是在这样做时eventListVieweventModel 保持空白?

我能做些什么来纠正这个问题或我需要朝什么方向努力?

【问题讨论】:

    标签: javascript arrays listview qml v-play


    【解决方案1】:

    你传递给find的函数有问题。

    function(obj) {
        return obj.date === calendar.selectedDate.getDate(),     // <-- oh no! lé comma!
            console.log(JSON.stringify(obj));
    }
    

    请注意,您使用了逗号运算符,在 JS 中,它将丢弃左边的表达式并返回右边的结果(这里是 undefined,因为这是 console.log 返回的内容)。在 JS 控制台上的快速测试表明这不会产生并返回所需的结果(在您的情况下为布尔值)。

    function comma() {
        return 1, console.log('blunder');
    }
    function noComma {
        console.log('success');
        return 1;
    }
    
    x = comma();    // blunder
    y = noComma();  // success
    
    console.log(x);  // undefined   //  but expected 1 ?!?
    console.log(y);  // 1
    

    你可能会追求这样的东西:

    function(obj) {
        console.log(JSON.stringify(obj));
    
        return obj.date === calendar.selectedDate.getDate();
    }
    

    但是,这会将...字符串 (?) 与整数(由 getDate() 返回)进行比较。你可能想改为这样做

    return new Date(obj.date).getDate() === calendar.selectedDate.getDate();
    

    这仍然会记录obj,同时返回一个布尔值。

    Read more about JavaScript's comma operator...

    【讨论】:

    • 嘿@TrebuchetMS 感谢您的回复,需要匹配和缺少日期,因为它们将清除列表(不匹配日期)或创建ListView(匹配日期),我更改了我的代码根据您的建议,但没有收到带有日期或布尔值的控制台日志?当所选日期发生更改时(应触发该功能完成其工作),该功能目前在我日历的onSelectedDateChanged: {….} 部分内,我正在使用 v-play(现为 Felgo),如果这会有所作为吗?再次感谢
    • 您好,我已经更改了上一个代码 sn-p 并删除了 if 语句。似乎您想要记录所有对象。 ? 到目前为止,它看起来像是一个仅限 QML/JS 的问题(所以 V-Play/Felgo 并没有真正产生影响)......除非你有其他我没有看到的问题......跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    相关资源
    最近更新 更多