gpt4 book ai didi

javascript - 如何为 Kendo TreeView 刷新 HierarchicalDataSource?

转载 作者:行者123 更新时间:2023-11-30 06:30:17 26 4
gpt4 key购买 nike

Treeview 创建:

function CreateNotificationTree(userId)
{
debugger;
var data = new kendo.data.HierarchicalDataSource({
transport: {
read: {
url: "../api/notifications/byuserid/" + userId,
contentType: "application/json"
}
},
schema: {
model: {
children: "notifications"
}
}
});

$("#treeview").kendoTreeView({
dataSource: data,
loadOnDemand: true,
dataUrlField: "LinksTo",
checkboxes: {
checkChildren: true
},
dataTextField: ["notificationType", "NotificationDesc"],
select: treeviewSelect
});

function treeviewSelect(e)
{
var node = this.dataItem(e.node);
window.open(node.NotificationLink, "_self");
}
}

事情更新的地方,我需要刷新数据集:

$('#btnDelete').on('click', function()
{
var treeView = $("#treeview").data("kendoTreeView");
var userId = $('#user_id').val();

$('#treeview').find('input:checkbox:checked').each(function()
{
debugger;
var li = $(this).closest(".k-item")[0];
var notificationId = treeView.dataSource.getByUid(li.getAttribute('data-uid')).ID;

if (notificationId == "undefined")
{
alert('No ID was found for one or more notifications selected. These notifications will not be deleted. Please contact IT about this issue.');
}
else
{
$.ajax(
{
url: '../api/notifications/deleteNotification?userId=' + userId + '&notificationId=' + notificationId,
type: 'DELETE',
success: function()
{
alert('Delete successful.');
//Here is where I try to refresh the data source.
CreateNotificationTree(userId);
},
failure: function()
{
alert('Delete failed.');
}
});
treeView.remove($(this).closest('.k-item'));
}
});
});

这里的问题是它确实刷新了 Treeview ....但不是子节点...

有人知道如何让它工作吗?

最佳答案

看起来您正在完全重建 Treeview 。您为什么不只刷新 Treeview 的数据源的任何原因?

鉴于您上面的代码,我建议这样做:

treeView.dataSource.read();

此外,根据您从哪种类型的服务器获取 JSON,它可能允许浏览器缓存结果,因为 Kendo 数据源默认使用 GET 语句。这可以在服务器端修复,或者您可以切换到使用 POST 来检索数据:

read: {
url: "../api/notifications/byuserid/" + userId,
contentType: "application/json",
type: "POST" // Fixes issue if browser was caching GET requests
}

关于javascript - 如何为 Kendo TreeView 刷新 HierarchicalDataSource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18092377/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com