Created
February 10, 2012 12:58
-
-
Save colinramsay/1789536 to your computer and use it in GitHub Desktop.
Add filtering to Ext JS 4's TreePanel (Ext.tree.Panel)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add basic filtering to Ext.tree.Panel. Add as a mixin: | |
* mixins: { | |
* treeFilter: 'MyApp.lib.TreeFilter' | |
* } | |
*/ | |
Ext.define('MyApp.lib.TreeFilter', { | |
filterByText: function(text) { | |
this.filterBy(text, 'text'); | |
}, | |
/** | |
* Filter the tree on a string, hiding all nodes expect those which match and their parents. | |
* @param The term to filter on. | |
* @param The field to filter on (i.e. 'text'). | |
*/ | |
filterBy: function(text, by) { | |
this.clearFilter(); | |
var view = this.getView(), | |
me = this, | |
nodesAndParents = []; | |
// Find the nodes which match the search term, expand them. | |
// Then add them and their parents to nodesAndParents. | |
this.getRootNode().cascadeBy(function(tree, view){ | |
var currNode = this; | |
if(currNode && currNode.data[by] && currNode.data[by].toString().toLowerCase().indexOf(text.toLowerCase()) > -1) { | |
me.expandPath(currNode.getPath()); | |
while(currNode.parentNode) { | |
nodesAndParents.push(currNode.id); | |
currNode = currNode.parentNode; | |
} | |
} | |
}, null, [me, view]); | |
// Hide all of the nodes which aren't in nodesAndParents | |
this.getRootNode().cascadeBy(function(tree, view){ | |
var uiNode = view.getNodeByRecord(this); | |
if(uiNode && !Ext.Array.contains(nodesAndParents, this.id)) { | |
Ext.get(uiNode).setDisplayed('none'); | |
} | |
}, null, [me, view]); | |
}, | |
clearFilter: function() { | |
var view = this.getView(); | |
this.getRootNode().cascadeBy(function(tree, view){ | |
var uiNode = view.getNodeByRecord(this); | |
if(uiNode) { | |
Ext.get(uiNode).setDisplayed('table-row'); | |
} | |
}, null, [this, view]); | |
} | |
}); |
Awesom!!! Thank..
It's very useful in ExtJS 5.1.1
Awesome work..! Thanks :) It's also working with ExtJS 6.0.0 👍
hi, i use unigui , a lib for delphi, that use extjs 4.2.5.
Taking your approach to create filter, I get the goals of hiding nodes not respondig to filter (thanks to your work) , but when i collapse a brunch and re-expand , the tree is re-redered and the nodes are displyed again!
someone else have encoutered the same beaviour ?
or is a special case for my lib?
dave
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks !! I couldn't use this custom tree filter as a mixin but created panel function using same logic.