2

树视图创建:

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'));
        }
    });
});

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

有谁知道如何让这个工作?

4

1 回答 1

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
}
于 2014-05-28T18:24:07.500 回答