CheckColumn.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. constructor: function() {
  33. this.addEvents(
  34. /**
  35. * @event checkchange
  36. * Fires when the checked state of a row changes
  37. * @param {Ext.ux.CheckColumn} this
  38. * @param {Number} rowIndex The row index
  39. * @param {Boolean} checked True if the box is checked
  40. */
  41. 'checkchange'
  42. );
  43. this.callParent(arguments);
  44. },
  45. /**
  46. * @private
  47. * Process and refire events routed from the GridView's processEvent method.
  48. */
  49. processEvent: function(type, view, cell, recordIndex, cellIndex, e) {
  50. if (type == 'mousedown' || (type == 'keydown' && (e.getKey() == e.ENTER || e.getKey() == e.SPACE))) {
  51. var record = view.panel.store.getAt(recordIndex),
  52. dataIndex = this.dataIndex,
  53. checked = !record.get(dataIndex);
  54. record.set(dataIndex, checked);
  55. this.fireEvent('checkchange', this, recordIndex, checked);
  56. // cancel selection.
  57. return false;
  58. } else {
  59. return this.callParent(arguments);
  60. }
  61. },
  62. // Note: class names are not placed on the prototype bc renderer scope
  63. // is not in the header.
  64. renderer : function(value){
  65. var cssPrefix = Ext.baseCSSPrefix,
  66. cls = [cssPrefix + 'grid-checkheader'];
  67. if (value) {
  68. cls.push(cssPrefix + 'grid-checkheader-checked');
  69. }
  70. return '<div class="' + cls.join(' ') + '">&#160;</div>';
  71. }
  72. });