]>
git.proxmox.com Git - sencha-touch.git/blob - src/src/util/Filter.js
2 * Represents a filter that can be applied to a {@link Ext.util.MixedCollection MixedCollection}. Can either simply
3 * filter on a property/value pair or pass in a filter function with custom logic. Filters are always used in the
4 * context of MixedCollections, though {@link Ext.data.Store Store}s frequently create them when filtering and searching
5 * on their records. Example usage:
7 * // Set up a fictional MixedCollection containing a few people to filter on
8 * var allNames = new Ext.util.MixedCollection();
10 * { id: 1, name: 'Ed', age: 25 },
11 * { id: 2, name: 'Jamie', age: 37 },
12 * { id: 3, name: 'Abe', age: 32 },
13 * { id: 4, name: 'Aaron', age: 26 },
14 * { id: 5, name: 'David', age: 32 }
17 * var ageFilter = new Ext.util.Filter({
22 * var longNameFilter = new Ext.util.Filter({
23 * filterFn: function(item) {
24 * return item.name.length > 4;
28 * // a new MixedCollection with the 3 names longer than 4 characters
29 * var longNames = allNames.filter(longNameFilter);
31 * // a new MixedCollection with the 2 people of age 32:
32 * var youngFolk = allNames.filter(ageFilter);
34 Ext
.define('Ext.util.Filter', {
39 * @cfg {String} [property=null]
40 * The property to filter on. Required unless a `filter` is passed
45 * @cfg {RegExp/Mixed} [value=null]
46 * The value you want to match against. Can be a regular expression which will be used as matcher or any other
47 * value. Mixed can be an object or an array of objects.
52 * @cfg {Function} filterFn
53 * A custom filter function which is passed each item in the {@link Ext.util.MixedCollection} in turn. Should
54 * return true to accept each item or false to reject it
56 filterFn
: Ext
.emptyFn
,
59 * @cfg {Boolean} [anyMatch=false]
60 * True to allow any match - no regex start/end line anchors will be added.
65 * @cfg {Boolean} [exactMatch=false]
66 * True to force exact match (^ and $ characters added to the regex). Ignored if anyMatch is true.
71 * @cfg {Boolean} [caseSensitive=false]
72 * True to make the regex case sensitive (adds 'i' switch to regex).
77 * @cfg {String} [root=null]
78 * Optional root property. This is mostly useful when filtering a Store, in which case we set the root to 'data'
79 * to make the filter pull the {@link #property} out of the data object of each item
85 * An optional id this filter can be keyed by in Collections. If no id is specified it will generate an id by
86 * first trying a combination of property-value, and if none if these were specified (like when having a
87 * filterFn) it will generate a random id.
92 * @cfg {Object} [scope=null]
93 * The scope in which to run the filterFn
98 applyId: function(id
) {
100 if (this.getProperty()) {
101 id
= this.getProperty() + '-' + String(this.getValue());
104 id
= Ext
.id(null, 'ext-filter-');
112 * Creates new Filter.
113 * @param {Object} config Config object
115 constructor: function(config
) {
116 this.initConfig(config
);
119 applyFilterFn: function(filterFn
) {
120 if (filterFn
=== Ext
.emptyFn
) {
121 filterFn
= this.getInitialConfig('filter');
126 var value
= this.getValue();
127 if (!this.getProperty() && !value
&& value
!== 0) {
129 Ext
.Logger
.error('A Filter requires either a property and value, or a filterFn to be set');
134 return this.createFilterFn();
142 * Creates a filter function for the configured property/value/anyMatch/caseSensitive options for this Filter
144 createFilterFn: function() {
146 matcher
= me
.createValueMatcher();
148 return function(item
) {
149 var root
= me
.getRoot(),
150 property
= me
.getProperty();
156 return matcher
.test(item
[property
]);
162 * Returns a regular expression based on the given value and matching options
164 createValueMatcher: function() {
166 value
= me
.getValue(),
167 anyMatch
= me
.getAnyMatch(),
168 exactMatch
= me
.getExactMatch(),
169 caseSensitive
= me
.getCaseSensitive(),
170 escapeRe
= Ext
.String
.escapeRegex
;
172 if (value
=== null || value
=== undefined || !value
.exec
) { // not a regex
173 value
= String(value
);
175 if (anyMatch
=== true) {
176 value
= escapeRe(value
);
178 value
= '^' + escapeRe(value
);
179 if (exactMatch
=== true) {
183 value
= new RegExp(value
, caseSensitive
? '' : 'i');