Kendo UI重新加载树视图

时间:2013-07-24 07:13:24

标签: kendo-ui

我通过ajax使用kendo ui加载一个复杂的树视图,因为我需要用一个请求加载树(工作正常):

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

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

如果我在通过ajax更改某些数据后尝试重新加载完整的树,则新的构建树无法正常工作,也不会更新文本。

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

Ido能做些什么? 谢谢 斯文

2 个答案:

答案 0 :(得分:3)

在ajax成功回调试试这个

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

答案 1 :(得分:0)

我让我的工作。

这就是我所做的:

创建树视图的函数:

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

修改&数据源刷新:

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

希望有所帮助。