Skip to content

Commit 53a37a1

Browse files
committed
added tree node filtering
1 parent f24050e commit 53a37a1

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

angular-tree-control.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
onNodeToggle: "&",
3636
options: "=?",
3737
orderBy: "@",
38-
reverseOrder: "@"
38+
reverseOrder: "@",
39+
filterExpression: "=?",
40+
filterComparator: "=?"
3941
},
4042
controller: ['$scope', function( $scope ) {
4143

@@ -149,7 +151,7 @@
149151
//tree template
150152
var template =
151153
'<ul '+classIfDefined($scope.options.injectClasses.ul, true)+'>' +
152-
'<li ng-repeat="node in node.' + $scope.options.nodeChildren + ' | orderBy:orderBy:reverseOrder" ng-class="headClass(node)" '+classIfDefined($scope.options.injectClasses.li, true)+'>' +
154+
'<li ng-repeat="node in node.' + $scope.options.nodeChildren + ' | filter:filterExpression:filterComparator | orderBy:orderBy:reverseOrder" ng-class="headClass(node)" '+classIfDefined($scope.options.injectClasses.li, true)+'>' +
153155
'<i class="tree-branch-head" ng-class="iBranchClass()" ng-click="selectNodeHead(node)"></i>' +
154156
'<i class="tree-leaf-head '+classIfDefined($scope.options.injectClasses.iLeaf, false)+'"></i>' +
155157
'<div class="tree-label '+classIfDefined($scope.options.injectClasses.label, false)+'" ng-class="selectedClass()" ng-click="selectNodeLabel(node)" tree-transclude></div>' +
@@ -248,7 +250,7 @@
248250
});
249251
}
250252
if (scope.options.equality(scope.node, scope.selectedNode)) {
251-
scope.selectNodeLabel(scope.node);
253+
scope.selectedNode = scope.node;
252254
}
253255

254256
// create a scope for the transclusion, whos parent is the parent of the tree control
@@ -272,4 +274,4 @@
272274
}
273275
}
274276
});
275-
})( angular );
277+
})( angular );

index.html

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,62 @@ <h1>Sorting tree nodes <small>(order-by, reserve-order)</small></h1>
850850
</script>
851851
</section>
852852

853+
854+
<section id="filtering" ng-controller="Filtering">
855+
<div class="page-header">
856+
<h1>Filtering tree nodes <small>(filter-expression, filter-comparator)</small></h1>
857+
</div>
858+
<div class="row">
859+
<div class="col-md-6 show-grid">
860+
<div class="panel panel-default">
861+
<div class="panel-body">
862+
<div class="example-caption">EXAMPLE:</div>
863+
<div save-content="filtering-html">
864+
<treecontrol class="tree-classic"
865+
tree-model="treedata"
866+
filter-expression="predicate"
867+
filter-comparator="comparator">
868+
label: {{node.label}} ({{node.id}})
869+
</treecontrol>
870+
</div>
871+
</div>
872+
</div>
873+
</div>
874+
<div class="col-md-6">
875+
<p>The <code>filter-expression</code> and <code>filter-comparator</code> properties allows filtering the nodes of the tree.
876+
The value of those attributes is used with the <code>ng-repeat</code> Filter filter -
877+
see more details at the <a href="https://code.angularjs.org/1.2.25/docs/api/ng/filter/filter">Angular JS Filter</a> docs.</p>
878+
<p>The filter is done for each branch individually.</p>
879+
<p>The filter expression (predicate) is used for selecting nodes from the tree to display.
880+
It can be a string, object or a function. If a string, it is used to match values of the node properties.
881+
If an object, each property of the expression object is used to match values of the node properties with the same name.
882+
A function can be used to write arbitrary filters, and will be invoked for each node of the tree.</p>
883+
<p>The filter comparator is used in determining if the expected value (from the filter expression) and actual value (from the object in the array) should be considered a match.
884+
If false, it looks for substring match in a case insensitive way (the default).
885+
If true, it looks for exact match.
886+
If a function, the function will be given the object value and the predicate value to compare and should return true if the item should be included in filtered result.</p>
887+
<input type="text" ng-model="predicate"/> Filter expression (string)<br>
888+
<input type="checkbox" ng-model="comparator"/> Filter comparator (true or false)
889+
</div>
890+
</div>
891+
<div class="row">
892+
<tabset>
893+
<tab heading="Markup" >
894+
<pre class="code" apply-content="filtering-html" highlight-lang="html"></pre>
895+
</tab>
896+
<tab heading="JavaScript">
897+
<pre class="code" apply-content="filtering-js" highlight-lang="js"></pre>
898+
</tab>
899+
</tabset>
900+
</div>
901+
<script save-content="filtering-js">
902+
function Filtering($scope) {
903+
$scope.treedata=createSubTree(3, 4, "");
904+
$scope.predicate = "Node 1";
905+
$scope.comparator = false;
906+
}
907+
</script>
908+
</section>
853909
<section id="equality" ng-controller="Equality">
854910
<div class="page-header">
855911
<h1>Custom Equality <small>(options.equality)</small></h1>

0 commit comments

Comments
 (0)