Supports anyMatch, caseSensitive and exactMatch.
Code:
/**
* @class Ext.ux.grid.filter.BetterStringFilter
* @extends Ext.ux.grid.filter.StringFilter
* Filter by a configurable Ext.form.field.Text
* <p><b><u>Example Usage:</u></b></p>
* <pre><code>
var filters = Ext.create('Ext.ux.grid.GridFilters', {
...
filters: [{
// required configs
type: 'betterString',
dataIndex: 'name',
// optional configs
value: 'foo',
active: true, // default is false
iconCls: 'ux-gridfilter-text-icon' // default
// any Ext.form.field.Text configs accepted
}]
});
* </code></pre>
*/
Ext.define('Ext.ux.grid.filter.BetterStringFilter', {
extend: 'Ext.ux.grid.filter.StringFilter',
alias: 'gridfilter.betterString',
/**
* @cfg {Boolean} anyMatch <tt>true</tt> to match any part not just the beginning (defaults to <tt>true</tt>).
*/
anyMatch: true,
/**
* @cfg {Boolean} caseSensitive <tt>true</tt> for case sensitive comparison (defaults to <tt>false</tt>).
*/
caseSensitive: false,
/**
* @cfg {Boolean} exactMatch <tt>true</tt> to force exact match (^ and $ characters added to the regex) (defaults to <tt>false</tt>).
* <p><b>Note</b>: Ignored if anyMatch is <tt>true</tt>.
*/
exactMatch: false,
/**
* Template method that is to validate the provided Ext.data.Record
* against the filters configuration.
* @param {Ext.data.Record} record The record to validate
* @return {Boolean} true if the record is valid within the bounds
* of the filter, false otherwise.
*/
validateRecord: function(record) {
var val = record.get(this.dataIndex);
var matcherValue = this.getValue();
if (typeof val != 'string') {
return (matcherValue.length === 0);
}
return this.createValueMatcher(matcherValue, this.anyMatch, this.caseSensitive, this.exactMatch).test(val);
},
/**
* Returns a regular expression based on the given value and matching options
* @private
* @param {String} value The value to create the regex for. This is escaped using Ext.escapeRe
* @param {Boolean} anyMatch True to allow any match - no regex start/end line anchors will be added. Defaults to false
* @param {Boolean} caseSensitive True to make the regex case sensitive (adds 'i' switch to regex). Defaults to false.
* @param {Boolean} exactMatch True to force exact match (^ and $ characters added to the regex). Defaults to false. Ignored if anyMatch is true.
*/
createValueMatcher: function(value, anyMatch, caseSensitive, exactMatch) {
if (value == this.lastValue) {
return this.lastValueMatcher;
}
this.lastValue = value;
if (!value.exec) { // not a regex
var escapeRe = Ext.String.escapeRegex;
value = String(value);
if (anyMatch === true) {
value = escapeRe(value);
} else {
value = '^' + escapeRe(value);
if (exactMatch === true) {
value += '$';
}
}
value = new RegExp(value, caseSensitive ? '' : 'i');
}
this.lastValueMatcher = value;
return value;
}
});