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