CheckColumn.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @class Ext.ux.CheckColumn
  3. * @extends Ext.grid.column.Column
  4. * A Header subclass which renders a checkbox in each column cell which toggles the truthiness of the associated data field on click.
  5. *
  6. * Example usage:
  7. *
  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. * In addition to toggling a Boolean value within the record data, this
  24. * class adds or removes a css class <tt>'x-grid-checked'</tt> on the td
  25. * based on whether or not it is checked to alter the background image used
  26. * for a column.
  27. */
  28. Ext.define('Ext.ux.CheckColumn', {
  29. extend: 'Ext.grid.column.Column',
  30. alias: 'widget.checkcolumn',
  31. /**
  32. * @cfg {Boolean} [stopSelection=true]
  33. * Prevent grid selection upon mousedown.
  34. */
  35. stopSelection: true,
  36. tdCls: Ext.baseCSSPrefix + 'grid-cell-checkcolumn',
  37. constructor: function() {
  38. this.addEvents(
  39. /**
  40. * @event beforecheckchange
  41. * Fires when before checked state of a row changes.
  42. * The change may be vetoed by returning `false` from a listener.
  43. * @param {Ext.ux.CheckColumn} this CheckColumn
  44. * @param {Number} rowIndex The row index
  45. * @param {Boolean} checked True if the box is to be checked
  46. */
  47. 'beforecheckchange',
  48. /**
  49. * @event checkchange
  50. * Fires when the checked state of a row changes
  51. * @param {Ext.ux.CheckColumn} this CheckColumn
  52. * @param {Number} rowIndex The row index
  53. * @param {Boolean} checked True if the box is now checked
  54. */
  55. 'checkchange'
  56. );
  57. this.callParent(arguments);
  58. },
  59. /**
  60. * @private
  61. * Process and refire events routed from the GridView's processEvent method.
  62. */
  63. processEvent: function(type, view, cell, recordIndex, cellIndex, e) {
  64. var me = this,
  65. key = type === 'keydown' && e.getKey(),
  66. mousedown = type == 'mousedown';
  67. if (mousedown || (key == e.ENTER || key == e.SPACE)) {
  68. var record = view.panel.store.getAt(recordIndex),
  69. dataIndex = me.dataIndex,
  70. checked = !record.get(dataIndex);
  71. // Allow apps to hook beforecheckchange
  72. if (me.fireEvent('beforecheckchange', me, recordIndex, checked) !== false) {
  73. record.set(dataIndex, checked);
  74. me.fireEvent('checkchange', me, recordIndex, checked);
  75. // Mousedown on the now nonexistent cell causes the view to blur, so stop it continuing.
  76. if (mousedown) {
  77. e.stopEvent();
  78. }
  79. // Selection will not proceed after this because of the DOM update caused by the record modification
  80. // Invoke the SelectionModel unless configured not to do so
  81. if (!me.stopSelection) {
  82. view.selModel.selectByPosition({
  83. row: recordIndex,
  84. column: cellIndex
  85. });
  86. }
  87. // Prevent the view from propagating the event to the selection model - we have done that job.
  88. return false;
  89. } else {
  90. // Prevent the view from propagating the event to the selection model if configured to do so.
  91. return !me.stopSelection;
  92. }
  93. } else {
  94. return me.callParent(arguments);
  95. }
  96. },
  97. // Note: class names are not placed on the prototype bc renderer scope
  98. // is not in the header.
  99. renderer : function(value){
  100. var cssPrefix = Ext.baseCSSPrefix,
  101. cls = [cssPrefix + 'grid-checkheader'];
  102. if (value) {
  103. cls.push(cssPrefix + 'grid-checkheader-checked');
  104. }
  105. return '<div class="' + cls.join(' ') + '">&#160;</div>';
  106. }
  107. });