如何刷新Kendo TreeView的HierarchicalDataSource?

时间:2013-08-06 23:47:34

标签: javascript jquery 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 + '&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'));
        }
    });
});

这里的问题是它确实刷新了树视图......但不是CHILDREN节点......

谁知道如何让这个工作?

1 个答案:

答案 0 :(得分:0)

看起来您正在完全重建树视图。有什么理由不刷新树视图的数据源?

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

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
}