我希望在日历的当前selectedDate
上填充一个带有来自数组(calendarListModel
)的数据的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"}
但是,当执行此操作时,eventListView
和eventModel
保持空白吗?
我该怎么做才能纠正这个方向或需要在什么方向?
您传递到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
。
阅读有关JavaScript的逗号操作员的更多信息...