2017-04-13 62 views
0

對於Angular UI樹,我想添加expland all或全部摺疊。如何摺疊每個有角度的UI樹上的所有功能

我試過下面的代碼,它的工作原理。

$scope.collapseAll = function() { 
     $scope.$broadcast('angular-ui-tree:collapse-all'); 
     }; 

    $scope.expandAll = function() { 
    $scope.$broadcast('angular-ui-tree:expand-all'); 
    }; 
    ... 

問題是,我的頁面有多個UI樹,我只想觸發expandAll函數單獨。現在我觸發這個expandAll函數。 全部 ui樹會受到影響。

有沒有解決方案將每一個設置爲單獨的?

How to call collapseAll function on Angular UI tree?

回答

1

如果選中在角UI樹Source,在'angular-ui-tree:collapse-all'事件觸發一個範圍值。其他問題是angular-ui-tree中的每個指令都會繼承父級的範圍,而不是具有隔離範圍。因此,改變廣播事件時,它實際上正在改變相同的範圍值。

你應該找到一種方法讓每棵樹有一個單獨的作用域,或者考慮使用其他樹例如v-accordion

+1

謝謝你的回覆。 V型手風琴是一個奇妙的插件,後來會考慮使用它。無論如何,我使用while循環並遞歸獲取childNode,可以解決它。謝謝 –

0

我們應該爲指定的樹作用域廣播angular-ui-tree:collapse-all。 它適用於我,你可以毫不猶豫地使用它。

function collapseAll() { 
     var treeScope = _getRootNodesScope('myTreeId'); 
     if (treeScope) { 
      treeScope.$broadcast('angular-ui-tree:collapse-all'); 
     } 
    } 


function expandAll() { 
     var treeScope = _getRootNodesScope('userTreeroot'); 
     if (treeScope) { 
      treeScope.$broadcast('angular-ui-tree:expand-all'); 
     } 
    } 

    function _getRootNodesScope(myTreeId) { 
var treeElement = angular.element(document.getElementById(myTreeId)); 
if (treeElement) { 
var treeScope = (typeof treeElement.scope === 'function') ? 
treeElement.scope() : undefined; 
      return treeScope; 
     } 
     return undefined; 
    }; 
相關問題