2013-08-06 127 views
2

TreeView控件创建:如何刷新Kendo TreeView的HierarchicalDataSource?

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

这里的问题是,它刷新树视图....但不是儿童节点...

任何人都知道如何得到这个工作?

+0

参见[这个答案](http://stackoverflow.com/a/18091462/1267304)。 – DontVoteMeDown

回答

0

它看起来像你完全重建树视图。任何你不只是刷新树视图的数据源的原因?

鉴于上面的代码,我会推荐这:也

treeView.dataSource.read(); 

,取决于什么类型的服务器你是从获得JSON,可以允许浏览器缓存结果,作为剑道数据源默认使用GET语句。这可能是固定在服务器端,或者你可以切换到使用POST来检索数据:

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

这是非分层数据的正确答案,对分层数据不起作用。 – theycallmemorty