RowEditing.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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-plugin-RowEditing'>/**
  19. </span> * The Ext.grid.plugin.RowEditing plugin injects editing at a row level for a Grid. When editing begins,
  20. * a small floating dialog will be shown for the appropriate row. Each editable column will show a field
  21. * for editing. There is a button to save or cancel all changes for the edit.
  22. *
  23. * The field that will be used for the editor is defined at the
  24. * {@link Ext.grid.column.Column#editor editor}. The editor can be a field instance or a field configuration.
  25. * If an editor is not specified for a particular column then that column won't be editable and the value of
  26. * the column will be displayed. To provide a custom renderer for non-editable values, use the
  27. * {@link Ext.grid.column.Column#editRenderer editRenderer} configuration on the column.
  28. *
  29. * The editor may be shared for each column in the grid, or a different one may be specified for each column.
  30. * An appropriate field type should be chosen to match the data structure that it will be editing. For example,
  31. * to edit a date, it would be useful to specify {@link Ext.form.field.Date} as the editor.
  32. *
  33. * @example
  34. * Ext.create('Ext.data.Store', {
  35. * storeId:'simpsonsStore',
  36. * fields:['name', 'email', 'phone'],
  37. * data: [
  38. * {&quot;name&quot;:&quot;Lisa&quot;, &quot;email&quot;:&quot;lisa@simpsons.com&quot;, &quot;phone&quot;:&quot;555-111-1224&quot;},
  39. * {&quot;name&quot;:&quot;Bart&quot;, &quot;email&quot;:&quot;bart@simpsons.com&quot;, &quot;phone&quot;:&quot;555-222-1234&quot;},
  40. * {&quot;name&quot;:&quot;Homer&quot;, &quot;email&quot;:&quot;home@simpsons.com&quot;, &quot;phone&quot;:&quot;555-222-1244&quot;},
  41. * {&quot;name&quot;:&quot;Marge&quot;, &quot;email&quot;:&quot;marge@simpsons.com&quot;, &quot;phone&quot;:&quot;555-222-1254&quot;}
  42. * ]
  43. * });
  44. *
  45. * Ext.create('Ext.grid.Panel', {
  46. * title: 'Simpsons',
  47. * store: Ext.data.StoreManager.lookup('simpsonsStore'),
  48. * columns: [
  49. * {header: 'Name', dataIndex: 'name', editor: 'textfield'},
  50. * {header: 'Email', dataIndex: 'email', flex:1,
  51. * editor: {
  52. * xtype: 'textfield',
  53. * allowBlank: false
  54. * }
  55. * },
  56. * {header: 'Phone', dataIndex: 'phone'}
  57. * ],
  58. * selType: 'rowmodel',
  59. * plugins: [
  60. * Ext.create('Ext.grid.plugin.RowEditing', {
  61. * clicksToEdit: 1
  62. * })
  63. * ],
  64. * height: 200,
  65. * width: 400,
  66. * renderTo: Ext.getBody()
  67. * });
  68. *
  69. */
  70. Ext.define('Ext.grid.plugin.RowEditing', {
  71. extend: 'Ext.grid.plugin.Editing',
  72. alias: 'plugin.rowediting',
  73. requires: [
  74. 'Ext.grid.RowEditor'
  75. ],
  76. editStyle: 'row',
  77. <span id='Ext-grid-plugin-RowEditing-cfg-autoCancel'> /**
  78. </span> * @cfg {Boolean} autoCancel
  79. * True to automatically cancel any pending changes when the row editor begins editing a new row.
  80. * False to force the user to explicitly cancel the pending changes. Defaults to true.
  81. */
  82. autoCancel: true,
  83. <span id='Ext-grid-plugin-RowEditing-cfg-clicksToMoveEditor'> /**
  84. </span> * @cfg {Number} clicksToMoveEditor
  85. * The number of clicks to move the row editor to a new row while it is visible and actively editing another row.
  86. * This will default to the same value as {@link Ext.grid.plugin.Editing#clicksToEdit clicksToEdit}.
  87. */
  88. <span id='Ext-grid-plugin-RowEditing-cfg-errorSummary'> /**
  89. </span> * @cfg {Boolean} errorSummary
  90. * True to show a {@link Ext.tip.ToolTip tooltip} that summarizes all validation errors present
  91. * in the row editor. Set to false to prevent the tooltip from showing. Defaults to true.
  92. */
  93. errorSummary: true,
  94. constructor: function() {
  95. var me = this;
  96. me.callParent(arguments);
  97. if (!me.clicksToMoveEditor) {
  98. me.clicksToMoveEditor = me.clicksToEdit;
  99. }
  100. me.autoCancel = !!me.autoCancel;
  101. },
  102. init: function(grid) {
  103. this.callParent([grid]);
  104. },
  105. <span id='Ext-grid-plugin-RowEditing-method-destroy'> /**
  106. </span> * @private
  107. * AbstractComponent calls destroy on all its plugins at destroy time.
  108. */
  109. destroy: function() {
  110. var me = this;
  111. Ext.destroy(me.editor);
  112. me.callParent(arguments);
  113. },
  114. <span id='Ext-grid-plugin-RowEditing-method-startEdit'> /**
  115. </span> * Starts editing the specified record, using the specified Column definition to define which field is being edited.
  116. * @param {Ext.data.Model} record The Store data record which backs the row to be edited.
  117. * @param {Ext.data.Model} columnHeader The Column object defining the column to be edited.
  118. * @return `true` if editing was started, `false` otherwise.
  119. */
  120. startEdit: function(record, columnHeader) {
  121. var me = this,
  122. editor = me.getEditor();
  123. if ((editor.beforeEdit() !== false) &amp;&amp; (me.callParent(arguments) !== false)) {
  124. editor.startEdit(me.context.record, me.context.column);
  125. return true;
  126. }
  127. return false;
  128. },
  129. // private
  130. cancelEdit: function() {
  131. var me = this;
  132. if (me.editing) {
  133. me.getEditor().cancelEdit();
  134. me.callParent(arguments);
  135. }
  136. },
  137. // private
  138. completeEdit: function() {
  139. var me = this;
  140. if (me.editing &amp;&amp; me.validateEdit()) {
  141. me.editing = false;
  142. me.fireEvent('edit', me, me.context);
  143. }
  144. },
  145. // private
  146. validateEdit: function() {
  147. var me = this,
  148. editor = me.editor,
  149. context = me.context,
  150. record = context.record,
  151. newValues = {},
  152. originalValues = {},
  153. editors = editor.items.items,
  154. e,
  155. eLen = editors.length,
  156. name, item;
  157. for (e = 0; e &lt; eLen; e++) {
  158. item = editors[e];
  159. name = item.name;
  160. newValues[name] = item.getValue();
  161. originalValues[name] = record.get(name);
  162. }
  163. Ext.apply(context, {
  164. newValues : newValues,
  165. originalValues : originalValues
  166. });
  167. return me.callParent(arguments) &amp;&amp; me.getEditor().completeEdit();
  168. },
  169. // private
  170. getEditor: function() {
  171. var me = this;
  172. if (!me.editor) {
  173. me.editor = me.initEditor();
  174. }
  175. return me.editor;
  176. },
  177. // private
  178. initEditor: function() {
  179. var me = this,
  180. grid = me.grid,
  181. view = me.view,
  182. headerCt = grid.headerCt,
  183. btns = ['saveBtnText', 'cancelBtnText', 'errorsText', 'dirtyText'],
  184. b,
  185. bLen = btns.length,
  186. cfg = {
  187. autoCancel: me.autoCancel,
  188. errorSummary: me.errorSummary,
  189. fields: headerCt.getGridColumns(),
  190. hidden: true,
  191. view: view,
  192. // keep a reference..
  193. editingPlugin: me,
  194. renderTo: view.el
  195. },
  196. item;
  197. for (b = 0; b &lt; bLen; b++) {
  198. item = btns[b];
  199. if (Ext.isDefined(me[item])) {
  200. cfg[item] = me[item];
  201. }
  202. }
  203. return Ext.create('Ext.grid.RowEditor', cfg);
  204. },
  205. // private
  206. initEditTriggers: function() {
  207. var me = this,
  208. view = me.view,
  209. moveEditorEvent = me.clicksToMoveEditor === 1 ? 'click' : 'dblclick';
  210. me.callParent(arguments);
  211. if (me.clicksToMoveEditor !== me.clicksToEdit) {
  212. me.mon(view, 'cell' + moveEditorEvent, me.moveEditorByClick, me);
  213. }
  214. view.on({
  215. render: function() {
  216. me.mon(me.grid.headerCt, {
  217. scope: me,
  218. columnresize: me.onColumnResize,
  219. columnhide: me.onColumnHide,
  220. columnshow: me.onColumnShow,
  221. columnmove: me.onColumnMove
  222. });
  223. },
  224. single: true
  225. });
  226. },
  227. startEditByClick: function() {
  228. var me = this;
  229. if (!me.editing || me.clicksToMoveEditor === me.clicksToEdit) {
  230. me.callParent(arguments);
  231. }
  232. },
  233. moveEditorByClick: function() {
  234. var me = this;
  235. if (me.editing) {
  236. me.superclass.onCellClick.apply(me, arguments);
  237. }
  238. },
  239. // private
  240. onColumnAdd: function(ct, column) {
  241. if (column.isHeader) {
  242. var me = this,
  243. editor;
  244. me.initFieldAccessors(column);
  245. // Only inform the editor about a new column if the editor has already been instantiated,
  246. // so do not use getEditor which instantiates the editor if not present.
  247. editor = me.editor;
  248. if (editor &amp;&amp; editor.onColumnAdd) {
  249. editor.onColumnAdd(column);
  250. }
  251. }
  252. },
  253. // private
  254. onColumnRemove: function(ct, column) {
  255. if (column.isHeader) {
  256. var me = this,
  257. editor = me.getEditor();
  258. if (editor &amp;&amp; editor.onColumnRemove) {
  259. editor.onColumnRemove(column);
  260. }
  261. me.removeFieldAccessors(column);
  262. }
  263. },
  264. // private
  265. onColumnResize: function(ct, column, width) {
  266. if (column.isHeader) {
  267. var me = this,
  268. editor = me.getEditor();
  269. if (editor &amp;&amp; editor.onColumnResize) {
  270. editor.onColumnResize(column, width);
  271. }
  272. }
  273. },
  274. // private
  275. onColumnHide: function(ct, column) {
  276. // no isHeader check here since its already a columnhide event.
  277. var me = this,
  278. editor = me.getEditor();
  279. if (editor &amp;&amp; editor.onColumnHide) {
  280. editor.onColumnHide(column);
  281. }
  282. },
  283. // private
  284. onColumnShow: function(ct, column) {
  285. // no isHeader check here since its already a columnshow event.
  286. var me = this,
  287. editor = me.getEditor();
  288. if (editor &amp;&amp; editor.onColumnShow) {
  289. editor.onColumnShow(column);
  290. }
  291. },
  292. // private
  293. onColumnMove: function(ct, column, fromIdx, toIdx) {
  294. // no isHeader check here since its already a columnmove event.
  295. var me = this,
  296. editor = me.getEditor();
  297. if (editor &amp;&amp; editor.onColumnMove) {
  298. // Must adjust the toIdx to account for removal if moving rightwards
  299. // because RowEditor.onColumnMove just calls Container.move which does not do this.
  300. editor.onColumnMove(column, fromIdx, toIdx - (toIdx &gt; fromIdx ? 1 : 0));
  301. }
  302. },
  303. // private
  304. setColumnField: function(column, field) {
  305. var me = this,
  306. editor = me.getEditor();
  307. editor.removeField(column);
  308. me.callParent(arguments);
  309. me.getEditor().setField(column);
  310. }
  311. });
  312. </pre>
  313. </body>
  314. </html>