Kendo UI reload treeview

2019-02-26 12:27发布

问题:

I load a complex treeview with kendo ui via ajax because I need to load the tree with one request (works fine):

$(document).ready(function() {    
    buildTree();        
});

function buildTree(){
    $.getJSON("admin_get_treedata.php", function (data) {
        $("#treeview").kendoTreeView({
            select: function(item) { editTreeElement(item,'tree'); },
            dataSource: data
        });
    })
}

If I try to reload the complete tree after changing some data via ajax the new build tree does not work correct and does not update the text.

  $.ajax({
        type: 'POST',
        url: 'ajax/ajax_update_layer.php',
        data: {
            layerid:id,
            ...
        },
        success: function(data){
                      buildTree();
                }
        });   

What can Ido? Thanks Sven

回答1:

try this on ajax success callback

var data = $("#treeView").data('kendoTreeView');
    data.dataSource.read();


回答2:

I got mine to work.

This is what I did:

Function that creates the tree view:

function CreateNotificationTree(userId)
{
    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 $item = this.dataItem(e.node);
        window.open($item.NotificationLink, "_self");
    }
}

Modification & data source refresh:

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

    $('#treeview').find('input:checkbox:checked').each(function()
    {
        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()
                    {
                        CreateNotificationTree(userId);
                        alert('Delete successful.');

                    },
                    failure: function()
                    {
                        alert('Delete failed.');
                    }
                });
            treeView.remove($(this).closest('.k-item'));
        }
    });
});

Hope that helps.



标签: kendo-ui