使用 Kendo ListView,当我的页面重新加载时,如果已进行选择,我需要让我的代码自动选择之前选择的 ListView 项。我可以通过遍历数据源集合来获取 Kendo DataItem,但是当我执行 listView.select(item) 时,UI 不会显示任何选定内容。
这是我的 ListView :
$("#listview").kendoListView({
dataSource: coverages,
template: kendo.template($("#listTemplate").html()),
selectable: true,
change: function() {
var index = this.select().index();
dataItem = this.dataSource.view()[index];
if (selectedCoverageCode == null) {
selectedCoverageCode = dataItem;
}
onCodeChanged(categoryId, planId, dataItem);
}
});
这是我设置先前选择的项目的代码:
function setSelectedCoverageCode(code) {
var listView = $("#listview").data("kendoListView");
var dataSource = listView.dataSource.view();
if (listView) {
$.each(dataSource, function(index, item) {
if (item.Code === code) {
listView.select(item);
selectedCoverageCode = item;
}
});
}
}
我相信我需要 DOM 对象而不是数据源 DataItem。上面的 setSelectedCoverageCode 函数触发了 Change 事件,但实际元素在 DOM 中的那个点没有被选中。
我该怎么做才能在重新加载时显示已选择的项目?有什么建议吗?
谢谢
最佳答案
您可以通过查找其 UID 来获取数据项的 DOM 元素。
var item = // the item out of the DataSource that you want to select
var listView = $("#listview").data("kendoListView");
listView.select(listView.element.find('[data-uid="' + item.uid + '"]'));
关于javascript - 选择 Kendo UI ListView 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29035530/