You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
16 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-grid-EditorGridPanel-method-constructor'><span id='Ext-grid-EditorGridPanel'>/**
  19. </span></span> * @class Ext.grid.EditorGridPanel
  20. * @extends Ext.grid.GridPanel
  21. * &lt;p&gt;This class extends the {@link Ext.grid.GridPanel GridPanel Class} to provide cell editing
  22. * on selected {@link Ext.grid.Column columns}. The editable columns are specified by providing
  23. * an {@link Ext.grid.ColumnModel#editor editor} in the {@link Ext.grid.Column column configuration}.&lt;/p&gt;
  24. * &lt;p&gt;Editability of columns may be controlled programatically by inserting an implementation
  25. * of {@link Ext.grid.ColumnModel#isCellEditable isCellEditable} into the
  26. * {@link Ext.grid.ColumnModel ColumnModel}.&lt;/p&gt;
  27. * &lt;p&gt;Editing is performed on the value of the &lt;i&gt;field&lt;/i&gt; specified by the column's
  28. * &lt;tt&gt;{@link Ext.grid.ColumnModel#dataIndex dataIndex}&lt;/tt&gt; in the backing {@link Ext.data.Store Store}
  29. * (so if you are using a {@link Ext.grid.ColumnModel#setRenderer renderer} in order to display
  30. * transformed data, this must be accounted for).&lt;/p&gt;
  31. * &lt;p&gt;If a value-to-description mapping is used to render a column, then a {@link Ext.form.Field#ComboBox ComboBox}
  32. * which uses the same {@link Ext.form.Field#valueField value}-to-{@link Ext.form.Field#displayFieldField description}
  33. * mapping would be an appropriate editor.&lt;/p&gt;
  34. * If there is a more complex mismatch between the visible data in the grid, and the editable data in
  35. * the {@link Edt.data.Store Store}, then code to transform the data both before and after editing can be
  36. * injected using the {@link #beforeedit} and {@link #afteredit} events.
  37. * @constructor
  38. * @param {Object} config The config object
  39. * @xtype editorgrid
  40. */
  41. Ext.grid.EditorGridPanel = Ext.extend(Ext.grid.GridPanel, {
  42. <span id='Ext-grid-EditorGridPanel-cfg-clicksToEdit'> /**
  43. </span> * @cfg {Number} clicksToEdit
  44. * &lt;p&gt;The number of clicks on a cell required to display the cell's editor (defaults to 2).&lt;/p&gt;
  45. * &lt;p&gt;Setting this option to 'auto' means that mousedown &lt;i&gt;on the selected cell&lt;/i&gt; starts
  46. * editing that cell.&lt;/p&gt;
  47. */
  48. clicksToEdit: 2,
  49. <span id='Ext-grid-EditorGridPanel-cfg-forceValidation'> /**
  50. </span> * @cfg {Boolean} forceValidation
  51. * True to force validation even if the value is unmodified (defaults to false)
  52. */
  53. forceValidation: false,
  54. <span id='Ext-grid-EditorGridPanel-property-isEditor'> // private
  55. </span> isEditor : true,
  56. <span id='Ext-grid-EditorGridPanel-property-detectEdit'> // private
  57. </span> detectEdit: false,
  58. <span id='Ext-grid-EditorGridPanel-cfg-autoEncode'> /**
  59. </span> * @cfg {Boolean} autoEncode
  60. * True to automatically HTML encode and decode values pre and post edit (defaults to false)
  61. */
  62. autoEncode : false,
  63. <span id='Ext-grid-EditorGridPanel-cfg-trackMouseOver'> /**
  64. </span> * @cfg {Boolean} trackMouseOver @hide
  65. */
  66. <span id='Ext-grid-EditorGridPanel-cfg-trackMouseOver'> // private
  67. </span> trackMouseOver: false, // causes very odd FF errors
  68. <span id='Ext-grid-EditorGridPanel-method-initComponent'> // private
  69. </span> initComponent : function(){
  70. Ext.grid.EditorGridPanel.superclass.initComponent.call(this);
  71. if(!this.selModel){
  72. <span id='Ext-grid-EditorGridPanel-cfg-selModel'> /**
  73. </span> * @cfg {Object} selModel Any subclass of AbstractSelectionModel that will provide the selection model for
  74. * the grid (defaults to {@link Ext.grid.CellSelectionModel} if not specified).
  75. */
  76. this.selModel = new Ext.grid.CellSelectionModel();
  77. }
  78. this.activeEditor = null;
  79. this.addEvents(
  80. <span id='Ext-grid-EditorGridPanel-event-beforeedit'> /**
  81. </span> * @event beforeedit
  82. * Fires before cell editing is triggered. The edit event object has the following properties &lt;br /&gt;
  83. * &lt;ul style=&quot;padding:5px;padding-left:16px;&quot;&gt;
  84. * &lt;li&gt;grid - This grid&lt;/li&gt;
  85. * &lt;li&gt;record - The record being edited&lt;/li&gt;
  86. * &lt;li&gt;field - The field name being edited&lt;/li&gt;
  87. * &lt;li&gt;value - The value for the field being edited.&lt;/li&gt;
  88. * &lt;li&gt;row - The grid row index&lt;/li&gt;
  89. * &lt;li&gt;column - The grid column index&lt;/li&gt;
  90. * &lt;li&gt;cancel - Set this to true to cancel the edit or return false from your handler.&lt;/li&gt;
  91. * &lt;/ul&gt;
  92. * @param {Object} e An edit event (see above for description)
  93. */
  94. &quot;beforeedit&quot;,
  95. <span id='Ext-grid-EditorGridPanel-event-afteredit'> /**
  96. </span> * @event afteredit
  97. * Fires after a cell is edited. The edit event object has the following properties &lt;br /&gt;
  98. * &lt;ul style=&quot;padding:5px;padding-left:16px;&quot;&gt;
  99. * &lt;li&gt;grid - This grid&lt;/li&gt;
  100. * &lt;li&gt;record - The record being edited&lt;/li&gt;
  101. * &lt;li&gt;field - The field name being edited&lt;/li&gt;
  102. * &lt;li&gt;value - The value being set&lt;/li&gt;
  103. * &lt;li&gt;originalValue - The original value for the field, before the edit.&lt;/li&gt;
  104. * &lt;li&gt;row - The grid row index&lt;/li&gt;
  105. * &lt;li&gt;column - The grid column index&lt;/li&gt;
  106. * &lt;/ul&gt;
  107. *
  108. * &lt;pre&gt;&lt;code&gt;
  109. grid.on('afteredit', afterEdit, this );
  110. function afterEdit(e) {
  111. // execute an XHR to send/commit data to the server, in callback do (if successful):
  112. e.record.commit();
  113. };
  114. * &lt;/code&gt;&lt;/pre&gt;
  115. * @param {Object} e An edit event (see above for description)
  116. */
  117. &quot;afteredit&quot;,
  118. <span id='Ext-grid-EditorGridPanel-event-validateedit'> /**
  119. </span> * @event validateedit
  120. * Fires after a cell is edited, but before the value is set in the record. Return false
  121. * to cancel the change. The edit event object has the following properties &lt;br /&gt;
  122. * &lt;ul style=&quot;padding:5px;padding-left:16px;&quot;&gt;
  123. * &lt;li&gt;grid - This grid&lt;/li&gt;
  124. * &lt;li&gt;record - The record being edited&lt;/li&gt;
  125. * &lt;li&gt;field - The field name being edited&lt;/li&gt;
  126. * &lt;li&gt;value - The value being set&lt;/li&gt;
  127. * &lt;li&gt;originalValue - The original value for the field, before the edit.&lt;/li&gt;
  128. * &lt;li&gt;row - The grid row index&lt;/li&gt;
  129. * &lt;li&gt;column - The grid column index&lt;/li&gt;
  130. * &lt;li&gt;cancel - Set this to true to cancel the edit or return false from your handler.&lt;/li&gt;
  131. * &lt;/ul&gt;
  132. * Usage example showing how to remove the red triangle (dirty record indicator) from some
  133. * records (not all). By observing the grid's validateedit event, it can be cancelled if
  134. * the edit occurs on a targeted row (for example) and then setting the field's new value
  135. * in the Record directly:
  136. * &lt;pre&gt;&lt;code&gt;
  137. grid.on('validateedit', function(e) {
  138. var myTargetRow = 6;
  139. if (e.row == myTargetRow) {
  140. e.cancel = true;
  141. e.record.data[e.field] = e.value;
  142. }
  143. });
  144. * &lt;/code&gt;&lt;/pre&gt;
  145. * @param {Object} e An edit event (see above for description)
  146. */
  147. &quot;validateedit&quot;
  148. );
  149. },
  150. <span id='Ext-grid-EditorGridPanel-method-initEvents'> // private
  151. </span> initEvents : function(){
  152. Ext.grid.EditorGridPanel.superclass.initEvents.call(this);
  153. this.getGridEl().on('mousewheel', this.stopEditing.createDelegate(this, [true]), this);
  154. this.on('columnresize', this.stopEditing, this, [true]);
  155. if(this.clicksToEdit == 1){
  156. this.on(&quot;cellclick&quot;, this.onCellDblClick, this);
  157. }else {
  158. var view = this.getView();
  159. if(this.clicksToEdit == 'auto' &amp;&amp; view.mainBody){
  160. view.mainBody.on('mousedown', this.onAutoEditClick, this);
  161. }
  162. this.on('celldblclick', this.onCellDblClick, this);
  163. }
  164. },
  165. <span id='Ext-grid-EditorGridPanel-method-onResize'> onResize : function(){
  166. </span> Ext.grid.EditorGridPanel.superclass.onResize.apply(this, arguments);
  167. var ae = this.activeEditor;
  168. if(this.editing &amp;&amp; ae){
  169. ae.realign(true);
  170. }
  171. },
  172. <span id='Ext-grid-EditorGridPanel-method-onCellDblClick'> // private
  173. </span> onCellDblClick : function(g, row, col){
  174. this.startEditing(row, col);
  175. },
  176. <span id='Ext-grid-EditorGridPanel-method-onAutoEditClick'> // private
  177. </span> onAutoEditClick : function(e, t){
  178. if(e.button !== 0){
  179. return;
  180. }
  181. var row = this.view.findRowIndex(t),
  182. col = this.view.findCellIndex(t);
  183. if(row !== false &amp;&amp; col !== false){
  184. this.stopEditing();
  185. if(this.selModel.getSelectedCell){ // cell sm
  186. var sc = this.selModel.getSelectedCell();
  187. if(sc &amp;&amp; sc[0] === row &amp;&amp; sc[1] === col){
  188. this.startEditing(row, col);
  189. }
  190. }else{
  191. if(this.selModel.isSelected(row)){
  192. this.startEditing(row, col);
  193. }
  194. }
  195. }
  196. },
  197. <span id='Ext-grid-EditorGridPanel-method-onEditComplete'> // private
  198. </span> onEditComplete : function(ed, value, startValue){
  199. this.editing = false;
  200. this.lastActiveEditor = this.activeEditor;
  201. this.activeEditor = null;
  202. var r = ed.record,
  203. field = this.colModel.getDataIndex(ed.col);
  204. value = this.postEditValue(value, startValue, r, field);
  205. if(this.forceValidation === true || String(value) !== String(startValue)){
  206. var e = {
  207. grid: this,
  208. record: r,
  209. field: field,
  210. originalValue: startValue,
  211. value: value,
  212. row: ed.row,
  213. column: ed.col,
  214. cancel:false
  215. };
  216. if(this.fireEvent(&quot;validateedit&quot;, e) !== false &amp;&amp; !e.cancel &amp;&amp; String(value) !== String(startValue)){
  217. r.set(field, e.value);
  218. delete e.cancel;
  219. this.fireEvent(&quot;afteredit&quot;, e);
  220. }
  221. }
  222. this.view.focusCell(ed.row, ed.col);
  223. },
  224. <span id='Ext-grid-EditorGridPanel-method-startEditing'> /**
  225. </span> * Starts editing the specified for the specified row/column
  226. * @param {Number} rowIndex
  227. * @param {Number} colIndex
  228. */
  229. startEditing : function(row, col){
  230. this.stopEditing();
  231. if(this.colModel.isCellEditable(col, row)){
  232. this.view.ensureVisible(row, col, true);
  233. var r = this.store.getAt(row),
  234. field = this.colModel.getDataIndex(col),
  235. e = {
  236. grid: this,
  237. record: r,
  238. field: field,
  239. value: r.data[field],
  240. row: row,
  241. column: col,
  242. cancel:false
  243. };
  244. if(this.fireEvent(&quot;beforeedit&quot;, e) !== false &amp;&amp; !e.cancel){
  245. this.editing = true;
  246. var ed = this.colModel.getCellEditor(col, row);
  247. if(!ed){
  248. return;
  249. }
  250. if(!ed.rendered){
  251. ed.parentEl = this.view.getEditorParent(ed);
  252. ed.on({
  253. scope: this,
  254. render: {
  255. fn: function(c){
  256. c.field.focus(false, true);
  257. },
  258. single: true,
  259. scope: this
  260. },
  261. specialkey: function(field, e){
  262. this.getSelectionModel().onEditorKey(field, e);
  263. },
  264. complete: this.onEditComplete,
  265. canceledit: this.stopEditing.createDelegate(this, [true])
  266. });
  267. }
  268. Ext.apply(ed, {
  269. row : row,
  270. col : col,
  271. record : r
  272. });
  273. this.lastEdit = {
  274. row: row,
  275. col: col
  276. };
  277. this.activeEditor = ed;
  278. if (ed.field.isXType('checkbox')) {
  279. ed.allowBlur = false;
  280. this.setupCheckbox(ed.field);
  281. }
  282. // Set the selectSameEditor flag if we are reusing the same editor again and
  283. // need to prevent the editor from firing onBlur on itself.
  284. ed.selectSameEditor = (this.activeEditor == this.lastActiveEditor);
  285. var v = this.preEditValue(r, field);
  286. ed.startEdit(this.view.getCell(row, col).firstChild, Ext.isDefined(v) ? v : '');
  287. // Clear the selectSameEditor flag
  288. (function(){
  289. delete ed.selectSameEditor;
  290. }).defer(50);
  291. }
  292. }
  293. },
  294. <span id='Ext-grid-EditorGridPanel-method-setupCheckbox'> setupCheckbox: function(field){
  295. </span> var me = this,
  296. fn = function() {
  297. field.el.on('click', me.onCheckClick, me, {single: true});
  298. };
  299. if (field.rendered) {
  300. fn();
  301. } else {
  302. field.on('render', fn, null, {single: true});
  303. }
  304. },
  305. <span id='Ext-grid-EditorGridPanel-method-onCheckClick'> onCheckClick: function(){
  306. </span> var ed = this.activeEditor;
  307. ed.allowBlur = true;
  308. ed.field.focus(false, 10);
  309. },
  310. <span id='Ext-grid-EditorGridPanel-method-preEditValue'> // private
  311. </span> preEditValue : function(r, field){
  312. var value = r.data[field];
  313. return this.autoEncode &amp;&amp; Ext.isString(value) ? Ext.util.Format.htmlDecode(value) : value;
  314. },
  315. <span id='Ext-grid-EditorGridPanel-method-postEditValue'> // private
  316. </span> postEditValue : function(value, originalValue, r, field){
  317. return this.autoEncode &amp;&amp; Ext.isString(value) ? Ext.util.Format.htmlEncode(value) : value;
  318. },
  319. <span id='Ext-grid-EditorGridPanel-method-stopEditing'> /**
  320. </span> * Stops any active editing
  321. * @param {Boolean} cancel (optional) True to cancel any changes
  322. */
  323. stopEditing : function(cancel){
  324. if(this.editing){
  325. // Store the lastActiveEditor to check if it is changing
  326. var ae = this.lastActiveEditor = this.activeEditor;
  327. if(ae){
  328. ae[cancel === true ? 'cancelEdit' : 'completeEdit']();
  329. this.view.focusCell(ae.row, ae.col);
  330. }
  331. this.activeEditor = null;
  332. }
  333. this.editing = false;
  334. }
  335. });
  336. Ext.reg('editorgrid', Ext.grid.EditorGridPanel);</pre>
  337. </body>
  338. </html>