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