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