2017-07-04 70 views
0

基於下面的代碼,如果我提供的id屬於父節點,我可以選擇樹視圖中的節點,例如:1.但是,當我提供子節點的id例如:2.1,它無法從代碼級別選擇節點。 以下是我目前使用的代碼:AngularJS kendo treeview編程選擇的子節點

<div id="example" ng-app="KendoDemos"> 
    <div class="demo-section k-content" ng-controller="MyCtrl"> 
     <div class="box-col"> 
      <h4>TreeView</h4> 
      <div kendo-tree-view="tree" 
       k-data-source="treeData" 
       k-on-change="selectedItem = dataItem"> 
       <span k-template> 
        {{dataItem.text}} 
       </span> 
      </div> 
     </div> 
    </div> 
</div> 


angular.module("KendoDemos", [ "kendo.directives" ]) 
.controller("MyCtrl", ["$scope", "$timeout", function($scope, $timeout) { 
     $scope.treeData = new kendo.data.HierarchicalDataSource({ 
      data: [ 
       { text: "Item 1", id: 1 }, 
       { text: "Item 2", id: 2, items: [ 
       { text: "SubItem 2.1", id: 2.1 }, 
       { text: "SubItem 2.2", id: 2.2 } 
      ] }, 
      { text: "Item 3", id: 3 } 
     ]}); 

     $scope.click = function(dataItem) { 
     alert(dataItem.text); 
     }; 

     $scope.selectNode = function(id) { 
      $scope.branchId = id; 
      var item = $scope.treeData._data.find(findKendoBranchById); 
      var node = $scope.tree.findByUid(item.uid); 
      $scope.tree.select(node); 
     } 

     function findKendoBranchById(item, index, kendo) { 
      var isThisBranch = false; 

      if (item.id == null) { 
       isThisBranch = item.text == $scope.branchId; 
      } else { 
       isThisBranch = item.id == $scope.branchId; 
      } 

      return isThisBranch; 
     } 

     $timeout(function() { 
      $scope.selectNode(2); 
      //when this runs, will show the error below 
      $scope.selectNode(2.1); 
     }, 2000); 
    }]); 

VM1703 angular.min.js:107 TypeError: Cannot read property 'uid' of undefined 
at n.$scope.selectNode (<anonymous>:20:50) 
at <anonymous>:38:18 
at VM1703 angular.min.js:146 
at e (VM1703 angular.min.js:43) 
at VM1703 angular.min.js:45 

回答

0

在端我選擇時所選擇的節點改變爲重新初​​始化樹形視圖。

$scope.rawData = [ 
    { text: "Item 1", id: 1 }, 
    { text: "Item 2", id: 2, items: [ 
     { text: "SubItem 2.1", id: 2.1 }, 
     { text: "SubItem 2.2", id: 2.2 } 
     ] 
    }]; 
$scope.selectNode = function(id){ 
    $scope.treeData = new kendo.data.ObservableArray(processSelectedNode($scope.rawData, id)); 
}; 

function processSelectedNode(array, id){ 
    var navigationData = []; 

    for (var i = 0; i < array.length; i++) { 
     var obj = array[i]; 
     obj.expanded = true; 
     obj.selected = obj.id == id; 

     if (array[i].items) { 
      obj.items = prepareNavigationTreeDataSource(array[i].items, id); 
     } 

     navigationData.push(obj); 
    } 
} 

這對我的作品的劍道樹視圖會選擇與所選=真

節點
相關問題