how do I refresh a HierarchicalDataSource for a Kendo TreeView?

RJ. picture RJ. · Aug 7, 2013 · Viewed 8.3k times · Source

TreeView creation:

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

Where things get updated and I need to refresh the 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'));
        }
    });
});

The problem here is that it does refresh the tree view.... BUT NOT the CHILDREN nodes...

anyone know how to get this working?

Answer

Matt picture Matt · May 28, 2014

It looks like you are completely rebuilding the tree view. Any reason why you don't just refresh the tree view's data source?

Given your code above, I would recommend this:

treeView.dataSource.read();

Also, depending on what type of server you are getting the JSON from, it may be allowing the browser to cache the results, as Kendo data sources default to using GET statements. This could be fixed on the server side, or you could switch to using a POST to retrieve the data:

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