Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / examples / ux / grid / filter / BooleanFilter.js
1 /**
2  * @class Ext.ux.grid.filter.BooleanFilter
3  * @extends Ext.ux.grid.filter.Filter
4  * Boolean filters use unique radio group IDs (so you can have more than one!)
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         // required configs
11         type: 'boolean',
12         dataIndex: 'visible'
13
14         // optional configs
15         defaultValue: null, // leave unselected (false selected by default)
16         yesText: 'Yes',     // default
17         noText: 'No'        // default
18     }]
19 });
20  * </code></pre>
21  */
22 Ext.define('Ext.ux.grid.filter.BooleanFilter', {
23     extend: 'Ext.ux.grid.filter.Filter',
24     alias: 'gridfilter.boolean',
25
26         /**
27          * @cfg {Boolean} defaultValue
28          * Set this to null if you do not want either option to be checked by default. Defaults to false.
29          */
30         defaultValue : false,
31         /**
32          * @cfg {String} yesText
33          * Defaults to 'Yes'.
34          */
35         yesText : 'Yes',
36         /**
37          * @cfg {String} noText
38          * Defaults to 'No'.
39          */
40         noText : 'No',
41
42     /**
43      * @private
44      * Template method that is to initialize the filter and install required menu items.
45      */
46     init : function (config) {
47         var gId = Ext.id();
48                 this.options = [
49                         Ext.create('Ext.menu.CheckItem', {text: this.yesText, group: gId, checked: this.defaultValue === true}),
50                         Ext.create('Ext.menu.CheckItem', {text: this.noText, group: gId, checked: this.defaultValue === false})];
51
52                 this.menu.add(this.options[0], this.options[1]);
53
54                 for(var i=0; i<this.options.length; i++){
55                         this.options[i].on('click', this.fireUpdate, this);
56                         this.options[i].on('checkchange', this.fireUpdate, this);
57                 }
58         },
59
60     /**
61      * @private
62      * Template method that is to get and return the value of the filter.
63      * @return {String} The value of this filter
64      */
65     getValue : function () {
66                 return this.options[0].checked;
67         },
68
69     /**
70      * @private
71      * Template method that is to set the value of the filter.
72      * @param {Object} value The value to set the filter
73      */
74         setValue : function (value) {
75                 this.options[value ? 0 : 1].setChecked(true);
76         },
77
78     /**
79      * @private
80      * Template method that is to get and return serialized filter data for
81      * transmission to the server.
82      * @return {Object/Array} An object or collection of objects containing
83      * key value pairs representing the current configuration of the filter.
84      */
85     getSerialArgs : function () {
86                 var args = {type: 'boolean', value: this.getValue()};
87                 return args;
88         },
89
90     /**
91      * Template method that is to validate the provided Ext.data.Record
92      * against the filters configuration.
93      * @param {Ext.data.Record} record The record to validate
94      * @return {Boolean} true if the record is valid within the bounds
95      * of the filter, false otherwise.
96      */
97     validateRecord : function (record) {
98                 return record.get(this.dataIndex) == this.getValue();
99         }
100 });