CheckColumn.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. 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.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @class Ext.ux.CheckColumn
  11. * @extends Ext.grid.column.Column
  12. * <p>A Header subclass which renders a checkbox in each column cell which toggles the truthiness of the associated data field on click.</p>
  13. * <p><b>Note. As of ExtJS 3.3 this no longer has to be configured as a plugin of the GridPanel.</b></p>
  14. * <p>Example usage:</p>
  15. * <pre><code>
  16. // create the grid
  17. var grid = Ext.create('Ext.grid.Panel', {
  18. ...
  19. columns: [{
  20. text: 'Foo',
  21. ...
  22. },{
  23. xtype: 'checkcolumn',
  24. text: 'Indoor?',
  25. dataIndex: 'indoor',
  26. width: 55
  27. }
  28. ]
  29. ...
  30. });
  31. * </code></pre>
  32. * In addition to toggling a Boolean value within the record data, this
  33. * class adds or removes a css class <tt>'x-grid-checked'</tt> on the td
  34. * based on whether or not it is checked to alter the background image used
  35. * for a column.
  36. */
  37. Ext.define('Ext.ux.CheckColumn', {
  38. extend: 'Ext.grid.column.Column',
  39. alias: 'widget.checkcolumn',
  40. constructor: function() {
  41. this.addEvents(
  42. /**
  43. * @event checkchange
  44. * Fires when the checked state of a row changes
  45. * @param {Ext.ux.CheckColumn} this
  46. * @param {Number} rowIndex The row index
  47. * @param {Boolean} checked True if the box is checked
  48. */
  49. 'checkchange'
  50. );
  51. this.callParent(arguments);
  52. this.sortable = false;
  53. if(!this.renderer){
  54. this.renderer = this.rendererFn;
  55. }
  56. var me = this;
  57. fn = function(ch){
  58. me.selectAll(ch.getAttribute('grid'), ch.getAttribute('cm'), ch.checked);
  59. };
  60. },
  61. headerCheckable: true,
  62. singleChecked: false,
  63. listeners: {
  64. afterrender: function(){
  65. if(this.headerCheckable) {
  66. this.setText("<input type='checkbox' id='" + this.dataIndex + "-checkbox' grid='" +
  67. this.ownerCt.ownerCt.id + "' cm='" + this.dataIndex + "' onclick='fn(this);'/>" + this.text);
  68. }
  69. if(this.singleChecked) {
  70. this.on('checkchange', this.onSingleCheck, this, {delay: 100});
  71. }
  72. }
  73. },
  74. /**
  75. * @private
  76. * Process and refire events routed from the GridView's processEvent method.
  77. */
  78. processEvent: function(type, view, cell, recordIndex, cellIndex, e) {
  79. if (type == 'mousedown' || (type == 'keydown' && (e.getKey() == e.ENTER || e.getKey() == e.SPACE))) {
  80. if((caller=='Craft'&&Ext.getCmp('cr_statuscode')&&Ext.getCmp('cr_statuscode').value!='ENTERING')
  81. ||(caller=='STANDARDCRAFT'&&Ext.getCmp('sc_statuscode')&&Ext.getCmp('sc_statuscode').value!='ENTERING')){
  82. return false;
  83. }
  84. var record = null;
  85. var dataIndex = this.dataIndex;
  86. var checked = null;
  87. if(view.panel.store.tree){//treegrid
  88. var tree = Ext.ComponentQuery.query('treepanel')[0];
  89. tree.getRecordByRecordIndex(recordIndex);
  90. record = tree.findRecord;
  91. checked = !record.get(dataIndex);
  92. //如果父节点checked,就把其子孙节点checked,否则unchecked
  93. tree.checkRecord(record, dataIndex, checked);
  94. } else {//普通的grid
  95. record = view.panel.store.getAt(recordIndex);
  96. checked = !record.get(dataIndex);
  97. }
  98. record.set(dataIndex, checked);
  99. this.fireEvent('checkchange', this, recordIndex, checked);
  100. // cancel selection.
  101. return false;
  102. } else {
  103. return this.callParent(arguments);
  104. }
  105. },
  106. // Note: class names are not placed on the prototype bc renderer scope
  107. // is not in the header.
  108. rendererFn : function(value, m, record){
  109. var cssPrefix = Ext.baseCSSPrefix,
  110. cls = [cssPrefix + 'grid-checkheader'];
  111. if (value) {
  112. cls.push(cssPrefix + 'grid-checkheader-checked');
  113. }
  114. return '<div class="' + cls.join(' ') + '">&#160;</div>';
  115. },
  116. /**
  117. * (取消)全选
  118. */
  119. selectAll: function(g, c, checked){
  120. var grid = Ext.getCmp(g);
  121. if(!grid.store)
  122. grid = grid.ownerCt;
  123. if(grid && grid.store.data){
  124. if(checked){
  125. grid.store.each(function(){
  126. if(!this.get(c)) {
  127. this.set(c, true);
  128. }
  129. });
  130. } else {
  131. grid.store.each(function(){
  132. if(this.get(c)) {
  133. this.set(c, false);
  134. }
  135. });
  136. }
  137. } else if(grid.store.tree){//tree grid
  138. var items = grid.store.tree.root.childNodes;
  139. Ext.each(items, function(item){
  140. });
  141. }
  142. },
  143. onSingleCheck: function(cm, rIdx, check) {
  144. if(check) {
  145. var grid = this.up('grid'), field = this.dataIndex;
  146. grid.store.each(function(r, i){
  147. if(i != rIdx && r.get(field)) {
  148. r.set(field, false);
  149. }
  150. });
  151. }
  152. }
  153. });