Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / docs / source / Filter.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5   <title>The source code</title>
6   <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../prettify/prettify.js"></script>
8   <style type="text/css">
9     .highlight { display: block; background-color: #ddd; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js"><span id='Ext-util-Filter'>/**
19 </span> * @class Ext.util.Filter
20  * @extends Object
21  * &lt;p&gt;Represents a filter that can be applied to a {@link Ext.util.MixedCollection MixedCollection}. Can either simply
22  * filter on a property/value pair or pass in a filter function with custom logic. Filters are always used in the context
23  * of MixedCollections, though {@link Ext.data.Store Store}s frequently create them when filtering and searching on their
24  * records. Example usage:&lt;/p&gt;
25 &lt;pre&gt;&lt;code&gt;
26 //set up a fictional MixedCollection containing a few people to filter on
27 var allNames = new Ext.util.MixedCollection();
28 allNames.addAll([
29     {id: 1, name: 'Ed',    age: 25},
30     {id: 2, name: 'Jamie', age: 37},
31     {id: 3, name: 'Abe',   age: 32},
32     {id: 4, name: 'Aaron', age: 26},
33     {id: 5, name: 'David', age: 32}
34 ]);
35
36 var ageFilter = new Ext.util.Filter({
37     property: 'age',
38     value   : 32
39 });
40
41 var longNameFilter = new Ext.util.Filter({
42     filterFn: function(item) {
43         return item.name.length &gt; 4;
44     }
45 });
46
47 //a new MixedCollection with the 3 names longer than 4 characters
48 var longNames = allNames.filter(longNameFilter);
49
50 //a new MixedCollection with the 2 people of age 24:
51 var youngFolk = allNames.filter(ageFilter);
52 &lt;/code&gt;&lt;/pre&gt;
53  */
54 Ext.define('Ext.util.Filter', {
55
56     /* Begin Definitions */
57
58     /* End Definitions */
59 <span id='Ext-util-Filter-cfg-property'>    /**
60 </span>     * @cfg {String} property The property to filter on. Required unless a {@link #filterFn} is passed
61      */
62     
63 <span id='Ext-util-Filter-cfg-filterFn'>    /**
64 </span>     * @cfg {Function} filterFn A custom filter function which is passed each item in the {@link Ext.util.MixedCollection} 
65      * in turn. Should return true to accept each item or false to reject it
66      */
67     
68 <span id='Ext-util-Filter-cfg-anyMatch'>    /**
69 </span>     * @cfg {Boolean} anyMatch True to allow any match - no regex start/end line anchors will be added. Defaults to false
70      */
71     anyMatch: false,
72     
73 <span id='Ext-util-Filter-cfg-exactMatch'>    /**
74 </span>     * @cfg {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false.
75      * Ignored if anyMatch is true.
76      */
77     exactMatch: false,
78     
79 <span id='Ext-util-Filter-cfg-caseSensitive'>    /**
80 </span>     * @cfg {Boolean} caseSensitive True to make the regex case sensitive (adds 'i' switch to regex). Defaults to false.
81      */
82     caseSensitive: false,
83     
84 <span id='Ext-util-Filter-cfg-root'>    /**
85 </span>     * @cfg {String} root Optional root property. This is mostly useful when filtering a Store, in which case we set the
86      * root to 'data' to make the filter pull the {@link #property} out of the data object of each item
87      */
88
89 <span id='Ext-util-Filter-method-constructor'>    /**
90 </span>     * Creates new Filter.
91      * @param {Object} config (optional) Config object
92      */
93     constructor: function(config) {
94         Ext.apply(this, config);
95         
96         //we're aliasing filter to filterFn mostly for API cleanliness reasons, despite the fact it dirties the code here.
97         //Ext.util.Sorter takes a sorterFn property but allows .sort to be called - we do the same here
98         this.filter = this.filter || this.filterFn;
99         
100         if (this.filter == undefined) {
101             if (this.property == undefined || this.value == undefined) {
102                 // Commented this out temporarily because it stops us using string ids in models. TODO: Remove this once
103                 // Model has been updated to allow string ids
104                 
105                 // Ext.Error.raise(&quot;A Filter requires either a property or a filterFn to be set&quot;);
106             } else {
107                 this.filter = this.createFilterFn();
108             }
109             
110             this.filterFn = this.filter;
111         }
112     },
113     
114 <span id='Ext-util-Filter-method-createFilterFn'>    /**
115 </span>     * @private
116      * Creates a filter function for the configured property/value/anyMatch/caseSensitive options for this Filter
117      */
118     createFilterFn: function() {
119         var me       = this,
120             matcher  = me.createValueMatcher(),
121             property = me.property;
122         
123         return function(item) {
124             return matcher.test(me.getRoot.call(me, item)[property]);
125         };
126     },
127     
128 <span id='Ext-util-Filter-method-getRoot'>    /**
129 </span>     * @private
130      * Returns the root property of the given item, based on the configured {@link #root} property
131      * @param {Object} item The item
132      * @return {Object} The root property of the object
133      */
134     getRoot: function(item) {
135         return this.root == undefined ? item : item[this.root];
136     },
137     
138 <span id='Ext-util-Filter-method-createValueMatcher'>    /**
139 </span>     * @private
140      * Returns a regular expression based on the given value and matching options
141      */
142     createValueMatcher : function() {
143         var me            = this,
144             value         = me.value,
145             anyMatch      = me.anyMatch,
146             exactMatch    = me.exactMatch,
147             caseSensitive = me.caseSensitive,
148             escapeRe      = Ext.String.escapeRegex;
149         
150         if (!value.exec) { // not a regex
151             value = String(value);
152
153             if (anyMatch === true) {
154                 value = escapeRe(value);
155             } else {
156                 value = '^' + escapeRe(value);
157                 if (exactMatch === true) {
158                     value += '$';
159                 }
160             }
161             value = new RegExp(value, caseSensitive ? '' : 'i');
162          }
163          
164          return value;
165     }
166 });</pre>
167 </body>
168 </html>