Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / examples / ux / CheckColumn.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.CheckColumn
17  * @extends Ext.grid.column.Column
18  * <p>A Header subclass which renders a checkbox in each column cell which toggles the truthiness of the associated data field on click.</p>
19  * <p><b>Note. As of ExtJS 3.3 this no longer has to be configured as a plugin of the GridPanel.</b></p>
20  * <p>Example usage:</p>
21  * <pre><code>
22 // create the grid
23 var grid = Ext.create('Ext.grid.Panel', {
24     ...
25     columns: [{
26            text: 'Foo',
27            ...
28         },{
29            xtype: 'checkcolumn',
30            text: 'Indoor?',
31            dataIndex: 'indoor',
32            width: 55
33         }
34     ]
35     ...
36 });
37  * </code></pre>
38  * In addition to toggling a Boolean value within the record data, this
39  * class adds or removes a css class <tt>'x-grid-checked'</tt> on the td
40  * based on whether or not it is checked to alter the background image used
41  * for a column.
42  */
43 Ext.define('Ext.ux.CheckColumn', {
44     extend: 'Ext.grid.column.Column',
45     alias: 'widget.checkcolumn',
46     
47     constructor: function() {
48         this.addEvents(
49             /**
50              * @event checkchange
51              * Fires when the checked state of a row changes
52              * @param {Ext.ux.CheckColumn} this
53              * @param {Number} rowIndex The row index
54              * @param {Boolean} checked True if the box is checked
55              */
56             'checkchange'
57         );
58         this.callParent(arguments);
59     },
60
61     /**
62      * @private
63      * Process and refire events routed from the GridView's processEvent method.
64      */
65     processEvent: function(type, view, cell, recordIndex, cellIndex, e) {
66         if (type == 'mousedown' || (type == 'keydown' && (e.getKey() == e.ENTER || e.getKey() == e.SPACE))) {
67             var record = view.panel.store.getAt(recordIndex),
68                 dataIndex = this.dataIndex,
69                 checked = !record.get(dataIndex);
70                 
71             record.set(dataIndex, checked);
72             this.fireEvent('checkchange', this, recordIndex, checked);
73             // cancel selection.
74             return false;
75         } else {
76             return this.callParent(arguments);
77         }
78     },
79
80     // Note: class names are not placed on the prototype bc renderer scope
81     // is not in the header.
82     renderer : function(value){
83         var cssPrefix = Ext.baseCSSPrefix,
84             cls = [cssPrefix + 'grid-checkheader'];
85
86         if (value) {
87             cls.push(cssPrefix + 'grid-checkheader-checked');
88         }
89         return '<div class="' + cls.join(' ') + '">&#160;</div>';
90     }
91 });
92