Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / examples / ux / grid / filter / NumericFilter.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.ux.grid.filter.NumericFilter
17  * @extends Ext.ux.grid.filter.Filter
18  * Filters using an Ext.ux.grid.menu.RangeMenu.
19  * <p><b><u>Example Usage:</u></b></p>
20  * <pre><code>
21 var filters = Ext.create('Ext.ux.grid.GridFilters', {
22     ...
23     filters: [{
24         type: 'numeric',
25         dataIndex: 'price'
26     }]
27 });
28  * </code></pre>
29  * <p>Any of the configuration options for {@link Ext.ux.grid.menu.RangeMenu} can also be specified as
30  * configurations to NumericFilter, and will be copied over to the internal menu instance automatically.</p>
31  */
32 Ext.define('Ext.ux.grid.filter.NumericFilter', {
33     extend: 'Ext.ux.grid.filter.Filter',
34     alias: 'gridfilter.numeric',
35     uses: ['Ext.form.field.Number'],
36
37     /**
38      * @private @override
39      * Creates the Menu for this filter.
40      * @param {Object} config Filter configuration
41      * @return {Ext.menu.Menu}
42      */
43     createMenu: function(config) {
44         var me = this,
45             menu;
46         menu = Ext.create('Ext.ux.grid.menu.RangeMenu', config);
47         menu.on('update', me.fireUpdate, me);
48         return menu;
49     },
50
51     /**
52      * @private
53      * Template method that is to get and return the value of the filter.
54      * @return {String} The value of this filter
55      */
56     getValue : function () {
57         return this.menu.getValue();
58     },
59
60     /**
61      * @private
62      * Template method that is to set the value of the filter.
63      * @param {Object} value The value to set the filter
64      */
65     setValue : function (value) {
66         this.menu.setValue(value);
67     },
68
69     /**
70      * @private
71      * Template method that is to return <tt>true</tt> if the filter
72      * has enough configuration information to be activated.
73      * @return {Boolean}
74      */
75     isActivatable : function () {
76         var values = this.getValue(),
77             key;
78         for (key in values) {
79             if (values[key] !== undefined) {
80                 return true;
81             }
82         }
83         return false;
84     },
85
86     /**
87      * @private
88      * Template method that is to get and return serialized filter data for
89      * transmission to the server.
90      * @return {Object/Array} An object or collection of objects containing
91      * key value pairs representing the current configuration of the filter.
92      */
93     getSerialArgs : function () {
94         var key,
95             args = [],
96             values = this.menu.getValue();
97         for (key in values) {
98             args.push({
99                 type: 'numeric',
100                 comparison: key,
101                 value: values[key]
102             });
103         }
104         return args;
105     },
106
107     /**
108      * Template method that is to validate the provided Ext.data.Record
109      * against the filters configuration.
110      * @param {Ext.data.Record} record The record to validate
111      * @return {Boolean} true if the record is valid within the bounds
112      * of the filter, false otherwise.
113      */
114     validateRecord : function (record) {
115         var val = record.get(this.dataIndex),
116             values = this.getValue(),
117             isNumber = Ext.isNumber;
118         if (isNumber(values.eq) && val != values.eq) {
119             return false;
120         }
121         if (isNumber(values.lt) && val >= values.lt) {
122             return false;
123         }
124         if (isNumber(values.gt) && val <= values.gt) {
125             return false;
126         }
127         return true;
128     }
129 });
130