Używam https://github.com/tchatel/angular-treeRepeat i próbuję filtrować węzły, które nie zostały rozwinięte. Więc ja zmodyfikowany kod zawierać filtr angularjs:Filtr AngularJS nie rozwija filtrowanych węzłów
treeRepeat.html:
<p id="expand-collapse-all">
<a href="" ng-click="expandAll()">Expand all</a>
<a href="" ng-click="collapseAll()">Collapse all</a>
</p>
Filter : <input ng-model="myFilter" type="text">
<ul frang-tree>
<li frang-tree-repeat="node in treeData | filter:myFilter">
<div><span class="icon"
ng-class="{collapsed: node.collapsed, expanded: !node.collapsed}"
ng-show="node.children && node.children.length > 0"
ng-click="node.collapsed = !node.collapsed"></span>
<span class="label"
ng-class="{folder: node.children && node.children.length > 0}"
ng-bind="node.label"
ng-click="action(node)"></span>
</div>
<ul ng-if="!node.collapsed && node.children && node.children.length > 0"
frang-tree-insert-children="node.children | filter:myFilter"></ul>
</li>
</ul>
To działa zgodnie z oczekiwaniami, jeśli są rozszerzane wszystkich węzłów drzewa: linii 20 na controllers.js:
$scope.treeData = JSON.parse("[ { \"label\": \"root\", \"children\": [ { \"label\": \"folder A\", \"collapsed\": true, \"children\": [ { \"label\": \"folder B\", \"collapsed\": true, \"children\": [ { \"label\": \"file B1\", \"collapsed\": true }, { \"label\": \"file B2\", \"collapsed\": true } ] }, { \"label\": \"file A1\", \"collapsed\": true }, { \"label\": \"file A2\", \"collapsed\": true }, { \"label\": \"file A3\", \"collapsed\": true }, { \"label\": \"file A4\", \"collapsed\": true } ] }, { \"label\": \"folder C\", \"collapsed\": true, \"children\": [ { \"label\": \"folder D\", \"collapsed\": true, \"children\": [ { \"label\": \"folder E\", \"collapsed\": true, \"children\": [ { \"label\": \"file E1\", \"collapsed\": true }, { \"label\": \"file E2\", \"collapsed\": true }, { \"label\": \"file E3\", \"collapsed\": true } ] } ] }, { \"label\": \"folder F\", \"collapsed\": true, \"children\": [ { \"label\": \"file F1\", \"collapsed\": true }, { \"label\": \"file F2\", \"collapsed\": true } ] }, { \"label\": \"file C1\", \"collapsed\": true } ] }, { \"label\": \"folder G\", \"collapsed\": true, \"children\": [ { \"label\": \"file G1\", \"collapsed\": true }, { \"label\": \"file G2\", \"collapsed\": true }, { \"label\": \"file G3\", \"collapsed\": true }, { \"label\": \"file G4\", \"collapsed\": true } ] }, { \"label\": \"folder H\", \"collapsed\": true, \"children\": [ { \"label\": \"file H1\", \"collapsed\": true }, { \"label\": \"file H2\", \"collapsed\": true }, { \"label\": \"file H3\", \"collapsed\": true } ] } ] } ]");
Ale jeśli węzły są zwinięte, dopasowane węzły rozszerzone/widoczne. Drzewo pozostaje zwinięte. Config dla zwiniętych węzłów: Linia 21 na controllers.js:
$scope.treeData = JSON.parse("[ { \"label\": \"root\", \"children\": [ { \"label\": \"folder A\", \"collapsed\": true, \"children\": [ { \"label\": \"folder B\", \"collapsed\": true, \"children\": [ { \"label\": \"file B1\", \"collapsed\": true }, { \"label\": \"file B2\", \"collapsed\": true } ] }, { \"label\": \"file A1\", \"collapsed\": true }, { \"label\": \"file A2\", \"collapsed\": true }, { \"label\": \"file A3\", \"collapsed\": true }, { \"label\": \"file A4\", \"collapsed\": true } ] }, { \"label\": \"folder C\", \"collapsed\": true, \"children\": [ { \"label\": \"folder D\", \"collapsed\": true, \"children\": [ { \"label\": \"folder E\", \"collapsed\": true, \"children\": [ { \"label\": \"file E1\", \"collapsed\": true }, { \"label\": \"file E2\", \"collapsed\": true }, { \"label\": \"file E3\", \"collapsed\": true } ] } ] }, { \"label\": \"folder F\", \"collapsed\": true, \"children\": [ { \"label\": \"file F1\", \"collapsed\": true }, { \"label\": \"file F2\", \"collapsed\": true } ] }, { \"label\": \"file C1\", \"collapsed\": true } ] }, { \"label\": \"folder G\", \"collapsed\": true, \"children\": [ { \"label\": \"file G1\", \"collapsed\": true }, { \"label\": \"file G2\", \"collapsed\": true }, { \"label\": \"file G3\", \"collapsed\": true }, { \"label\": \"file G4\", \"collapsed\": true } ] }, { \"label\": \"folder H\", \"collapsed\": true, \"children\": [ { \"label\": \"file H1\", \"collapsed\": true }, { \"label\": \"file H2\", \"collapsed\": true }, { \"label\": \"file H3\", \"collapsed\": true } ] } ] } ]");
Plunkr: https://plnkr.co/edit/CtXlRfdreolTTc018c0A?p=preview
Czy muszę ręcznie rozszerzać węzły jako typów użytkowników lub ma kanciasty config mogę używać, aby rozwinąć te węzły?
Próbowałem dodanie funkcji niestandardowej, że pożary typy użytkowników Everytime:
function matchChildNode(objData , parentNode) {
angular.forEach(objData, function(childNode, key) {
var searchText = "";
//AngularJS does not initialise the searchText var until used. As the function is re-initialised for every node
//need to check if is undefined
if ($scope.searchText == undefined) {
searchText = ""
} else {
searchText = $scope.searchText
}
if (searchText.toLowerCase() === childNode.label.toLowerCase()) {
parentNode.collapsed = false
}
matchChildNode(childNode.children , childNode);
});
}
}
Ale jest to bardzo nieefektywne, ponieważ przemierza całą strukturę drzewa dla każdego słowa kluczowego typów użytkowników. Działa to również dla dokładnie dopasowanego tekstu: searchText.toLowerCase() === childNode.label.toLowerCase()
. Próbowałem użyć contains
zamiast ===
bez powodzenia.
plnkr src:
app.css (removed due to stackoverflow 30000 character limitation when asking questions)
directives.js (removed due to stackoverflow 30000 character limitation when asking questions)
filter.js :
'use strict';
angular.module('app.filters', []);
index.html :
<!doctype html>
<html lang="en" ng-app="app">
<head>
<meta charset="utf-8">
<title>treeRepeat demo</title>
<link rel="stylesheet" href="app.css"/>
</head>
<body>
<h1>treeRepeat</h1>
<div id="menu" ng-controller="MenuCtrl">
<ul>
<li ng-repeat="item in menu" ng-class="{selected: item == getCurrentMenuItem()}"><a href="#/{{item.index}}">{{item.shortLabel}}</a></li>
</ul>
<h2>{{getCurrentMenuItem().fullLabel}}</h2>
</div>
<ng-view></ng-view>
<script src="angular.js"></script>
<script src="angular-route.js"></script>
<script src="app.js"></script>
<script src="services.js"></script>
<script src="controllers.js"></script>
<script src="filters.js"></script>
<script src="directives.js"></script>
</body>
</html>
services.js :
'use strict';
angular.module('app.services', [])
.constant('menu', []);
treerepeat.html :
<p id="expand-collapse-all">
<a href="" ng-click="expandAll()">Expand all</a>
<a href="" ng-click="collapseAll()">Collapse all</a>
</p>
Filter : <input ng-model="myFilter" type="text">
<ul frang-tree>
<li frang-tree-repeat="node in treeData | filter:myFilter">
<div><span class="icon"
ng-class="{collapsed: node.collapsed, expanded: !node.collapsed}"
ng-show="node.children && node.children.length > 0"
ng-click="node.collapsed = !node.collapsed"></span>
<span class="label"
ng-class="{folder: node.children && node.children.length > 0}"
ng-bind="node.label"
ng-click="action(node)"></span>
</div>
<ul ng-if="!node.collapsed && node.children && node.children.length > 0"
frang-tree-insert-children="node.children | filter:myFilter"></ul>
</li>
</ul>
Nie każdy scenariusz działa w rozbudowanych węzłów. Rozwiń wszystkie i wyszukaj "A". Powinien działać tak, jakbyś szukał "pliku A". Dobrze? – Valijon
@Valijon tak, wydaje się działać, tylko problem polega na tym, że gdy użytkownik usuwa cały tekst z pola tekstowego drzewo pozostaje rozwinięte, powinienem móc sprawdzić ten scenariusz i po prostu ponownie załadować drzewo: https://plnkr.co/edit/CtXlRfdreolTTc018c0A ? p = preview –
@ blue-sky Czy chcesz filtrować tylko te węzły, które nie zostały rozwinięte? –