CheckColumn.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @class SimpleTasks.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('SimpleTasks.ux.CheckColumn', {
  30. extend: 'Ext.grid.column.Column',
  31. xtype: 'checkcolumn',
  32. tdCls: Ext.baseCSSPrefix + 'grid-cell-checkcolumn',
  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. * @private
  48. * Process and refire events routed from the GridView's processEvent method.
  49. */
  50. processEvent: function(type, view, cell, recordIndex, cellIndex, e) {
  51. var me = this,
  52. cssPrefix = Ext.baseCSSPrefix,
  53. target = Ext.get(e.getTarget()),
  54. dataIndex, record, checked;
  55. if (target.hasCls(cssPrefix + 'grid-checkheader-inner')) {
  56. if(type === 'mousedown' && e.button === 0) {
  57. record = view.panel.store.getAt(recordIndex);
  58. dataIndex = me.dataIndex;
  59. checked = !record.get(dataIndex);
  60. record.set(dataIndex, checked);
  61. me.fireEvent('checkchange', me, recordIndex, checked);
  62. // cancel selection.
  63. return false;
  64. } else if(type === 'mouseover') {
  65. target.parent().addCls(cssPrefix + 'grid-checkheader-over');
  66. } else if(type === 'mouseout') {
  67. target.parent().removeCls(cssPrefix + 'grid-checkheader-over');
  68. }
  69. } else {
  70. return me.callParent(arguments);
  71. }
  72. },
  73. // Note: class names are not placed on the prototype bc renderer scope
  74. // is not in the header.
  75. renderer : function(value){
  76. var cssPrefix = Ext.baseCSSPrefix,
  77. cls = [cssPrefix + 'grid-checkheader'];
  78. if (value) {
  79. cls.push(cssPrefix + 'grid-checkheader-checked');
  80. }
  81. return '<div class="' + cls.join(' ') + '"><div class="' + cssPrefix + 'grid-checkheader-inner' + '">&#160;</div></div>';
  82. }
  83. });