Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / examples / ux / grid / filter / NumericFilter.js
1 /**
2  * @class Ext.ux.grid.filter.NumericFilter
3  * @extends Ext.ux.grid.filter.Filter
4  * Filters using an Ext.ux.grid.menu.RangeMenu.
5  * <p><b><u>Example Usage:</u></b></p>
6  * <pre><code>
7 var filters = Ext.create('Ext.ux.grid.GridFilters', {
8     ...
9     filters: [{
10         type: 'numeric',
11         dataIndex: 'price'
12     }]
13 });
14  * </code></pre>
15  * <p>Any of the configuration options for {@link Ext.ux.grid.menu.RangeMenu} can also be specified as
16  * configurations to NumericFilter, and will be copied over to the internal menu instance automatically.</p>
17  */
18 Ext.define('Ext.ux.grid.filter.NumericFilter', {
19     extend: 'Ext.ux.grid.filter.Filter',
20     alias: 'gridfilter.numeric',
21     uses: ['Ext.form.field.Number'],
22
23     /**
24      * @private @override
25      * Creates the Menu for this filter.
26      * @param {Object} config Filter configuration
27      * @return {Ext.menu.Menu}
28      */
29     createMenu: function(config) {
30         var me = this,
31             menu;
32         menu = Ext.create('Ext.ux.grid.menu.RangeMenu', config);
33         menu.on('update', me.fireUpdate, me);
34         return menu;
35     },
36
37     /**
38      * @private
39      * Template method that is to get and return the value of the filter.
40      * @return {String} The value of this filter
41      */
42     getValue : function () {
43         return this.menu.getValue();
44     },
45
46     /**
47      * @private
48      * Template method that is to set the value of the filter.
49      * @param {Object} value The value to set the filter
50      */
51     setValue : function (value) {
52         this.menu.setValue(value);
53     },
54
55     /**
56      * @private
57      * Template method that is to return <tt>true</tt> if the filter
58      * has enough configuration information to be activated.
59      * @return {Boolean}
60      */
61     isActivatable : function () {
62         var values = this.getValue(),
63             key;
64         for (key in values) {
65             if (values[key] !== undefined) {
66                 return true;
67             }
68         }
69         return false;
70     },
71
72     /**
73      * @private
74      * Template method that is to get and return serialized filter data for
75      * transmission to the server.
76      * @return {Object/Array} An object or collection of objects containing
77      * key value pairs representing the current configuration of the filter.
78      */
79     getSerialArgs : function () {
80         var key,
81             args = [],
82             values = this.menu.getValue();
83         for (key in values) {
84             args.push({
85                 type: 'numeric',
86                 comparison: key,
87                 value: values[key]
88             });
89         }
90         return args;
91     },
92
93     /**
94      * Template method that is to validate the provided Ext.data.Record
95      * against the filters configuration.
96      * @param {Ext.data.Record} record The record to validate
97      * @return {Boolean} true if the record is valid within the bounds
98      * of the filter, false otherwise.
99      */
100     validateRecord : function (record) {
101         var val = record.get(this.dataIndex),
102             values = this.getValue(),
103             isNumber = Ext.isNumber;
104         if (isNumber(values.eq) && val != values.eq) {
105             return false;
106         }
107         if (isNumber(values.lt) && val >= values.lt) {
108             return false;
109         }
110         if (isNumber(values.gt) && val <= values.gt) {
111             return false;
112         }
113         return true;
114     }
115 });