Как обновить HierarchicalDataSource для Kendo TreeView?
Создание 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");
}
}
Где все обновляется, и мне нужно обновить dataSet:
$('#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 + '¬ificationId=' + 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'));
}
});
});
Проблема здесь в том, что он обновляет древовидное представление.... НО НЕ ДЕТСКИЕ узлы...
Кто-нибудь знает, как заставить это работать?
1 ответ
Похоже, вы полностью перестраиваете вид дерева. Есть ли причина, по которой вы не просто обновляете источник данных древовидного представления?
Учитывая ваш код выше, я бы порекомендовал это:
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
}