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