2013-08-06 93 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