Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / docs / source / TreeFilter.html
1 <html>
2 <head>
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
4   <title>The source code</title>
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 </head>
8 <body  onload="prettyPrint();">
9     <pre class="prettyprint lang-js">/*!
10  * Ext JS Library 3.2.2
11  * Copyright(c) 2006-2010 Ext JS, Inc.
12  * licensing@extjs.com
13  * http://www.extjs.com/license
14  */
15 <div id="cls-Ext.tree.TreeFilter"></div>/**
16  * @class Ext.tree.TreeFilter
17  * Note this class is experimental and doesn't update the indent (lines) or expand collapse icons of the nodes
18  * @param {TreePanel} tree
19  * @param {Object} config (optional)
20  */
21 Ext.tree.TreeFilter = function(tree, config){
22     this.tree = tree;
23     this.filtered = {};
24     Ext.apply(this, config);
25 };
26
27 Ext.tree.TreeFilter.prototype = {
28     clearBlank:false,
29     reverse:false,
30     autoClear:false,
31     remove:false,
32
33      <div id="method-Ext.tree.TreeFilter-filter"></div>/**
34      * Filter the data by a specific attribute.
35      * @param {String/RegExp} value Either string that the attribute value
36      * should start with or a RegExp to test against the attribute
37      * @param {String} attr (optional) The attribute passed in your node's attributes collection. Defaults to "text".
38      * @param {TreeNode} startNode (optional) The node to start the filter at.
39      */
40     filter : function(value, attr, startNode){
41         attr = attr || "text";
42         var f;
43         if(typeof value == "string"){
44             var vlen = value.length;
45             // auto clear empty filter
46             if(vlen == 0 && this.clearBlank){
47                 this.clear();
48                 return;
49             }
50             value = value.toLowerCase();
51             f = function(n){
52                 return n.attributes[attr].substr(0, vlen).toLowerCase() == value;
53             };
54         }else if(value.exec){ // regex?
55             f = function(n){
56                 return value.test(n.attributes[attr]);
57             };
58         }else{
59             throw 'Illegal filter type, must be string or regex';
60         }
61         this.filterBy(f, null, startNode);
62         },
63
64     <div id="method-Ext.tree.TreeFilter-filterBy"></div>/**
65      * Filter by a function. The passed function will be called with each
66      * node in the tree (or from the startNode). If the function returns true, the node is kept
67      * otherwise it is filtered. If a node is filtered, its children are also filtered.
68      * @param {Function} fn The filter function
69      * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to the current Node.
70      */
71     filterBy : function(fn, scope, startNode){
72         startNode = startNode || this.tree.root;
73         if(this.autoClear){
74             this.clear();
75         }
76         var af = this.filtered, rv = this.reverse;
77         var f = function(n){
78             if(n == startNode){
79                 return true;
80             }
81             if(af[n.id]){
82                 return false;
83             }
84             var m = fn.call(scope || n, n);
85             if(!m || rv){
86                 af[n.id] = n;
87                 n.ui.hide();
88                 return false;
89             }
90             return true;
91         };
92         startNode.cascade(f);
93         if(this.remove){
94            for(var id in af){
95                if(typeof id != "function"){
96                    var n = af[id];
97                    if(n && n.parentNode){
98                        n.parentNode.removeChild(n);
99                    }
100                }
101            }
102         }
103     },
104
105     <div id="method-Ext.tree.TreeFilter-clear"></div>/**
106      * Clears the current filter. Note: with the "remove" option
107      * set a filter cannot be cleared.
108      */
109     clear : function(){
110         var t = this.tree;
111         var af = this.filtered;
112         for(var id in af){
113             if(typeof id != "function"){
114                 var n = af[id];
115                 if(n){
116                     n.ui.show();
117                 }
118             }
119         }
120         this.filtered = {};
121     }
122 };
123 </pre>    
124 </body>
125 </html>