3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * The Ext.grid.plugin.CellEditing plugin injects editing at a cell level for a Grid. Only a single
17 * cell will be editable at a time. The field that will be used for the editor is defined at the
18 * {@link Ext.grid.column.Column#editor editor}. The editor can be a field instance or a field configuration.
20 * If an editor is not specified for a particular column then that cell will not be editable and it will
21 * be skipped when activated via the mouse or the keyboard.
23 * The editor may be shared for each column in the grid, or a different one may be specified for each column.
24 * An appropriate field type should be chosen to match the data structure that it will be editing. For example,
25 * to edit a date, it would be useful to specify {@link Ext.form.field.Date} as the editor.
28 * Ext.create('Ext.data.Store', {
29 * storeId:'simpsonsStore',
30 * fields:['name', 'email', 'phone'],
32 * {"name":"Lisa", "email":"lisa@simpsons.com", "phone":"555-111-1224"},
33 * {"name":"Bart", "email":"bart@simpsons.com", "phone":"555--222-1234"},
34 * {"name":"Homer", "email":"home@simpsons.com", "phone":"555-222-1244"},
35 * {"name":"Marge", "email":"marge@simpsons.com", "phone":"555-222-1254"}
46 * Ext.create('Ext.grid.Panel', {
48 * store: Ext.data.StoreManager.lookup('simpsonsStore'),
50 * {header: 'Name', dataIndex: 'name', editor: 'textfield'},
51 * {header: 'Email', dataIndex: 'email', flex:1,
57 * {header: 'Phone', dataIndex: 'phone'}
59 * selType: 'cellmodel',
61 * Ext.create('Ext.grid.plugin.CellEditing', {
67 * renderTo: Ext.getBody()
70 Ext.define('Ext.grid.plugin.CellEditing', {
71 alias: 'plugin.cellediting',
72 extend: 'Ext.grid.plugin.Editing',
73 requires: ['Ext.grid.CellEditor', 'Ext.util.DelayedTask'],
75 constructor: function() {
78 * Fires before cell editing is triggered. Return false from event handler to stop the editing.
80 * @param {Object} e An edit event with the following properties:
83 * - record - The record being edited
84 * - field - The field name being edited
85 * - value - The value for the field being edited.
86 * - row - The grid table row
87 * - column - The grid {@link Ext.grid.column.Column Column} defining the column that is being edited.
88 * - rowIdx - The row index that is being edited
89 * - colIdx - The column index that is being edited
90 * - cancel - Set this to true to cancel the edit or return false from your handler.
94 * Fires after a cell is edited. Usage example:
96 * grid.on('edit', function(editor, e) {
97 * // commit the changes right after editing finished
101 * @param {Ext.grid.plugin.Editing} editor
102 * @param {Object} e An edit event with the following properties:
105 * - record - The record that was edited
106 * - field - The field name that was edited
107 * - value - The value being set
108 * - originalValue - The original value for the field, before the edit.
109 * - row - The grid table row
110 * - column - The grid {@link Ext.grid.column.Column Column} defining the column that was edited.
111 * - rowIdx - The row index that was edited
112 * - colIdx - The column index that was edited
115 * @event validateedit
116 * Fires after a cell is edited, but before the value is set in the record. Return false from event handler to
119 * Usage example showing how to remove the red triangle (dirty record indicator) from some records (not all). By
120 * observing the grid's validateedit event, it can be cancelled if the edit occurs on a targeted row (for
121 * example) and then setting the field's new value in the Record directly:
123 * grid.on('validateedit', function(editor, e) {
124 * var myTargetRow = 6;
126 * if (e.row == myTargetRow) {
128 * e.record.data[e.field] = e.value;
132 * @param {Ext.grid.plugin.Editing} editor
133 * @param {Object} e An edit event with the following properties:
136 * - record - The record being edited
137 * - field - The field name being edited
138 * - value - The value being set
139 * - originalValue - The original value for the field, before the edit.
140 * - row - The grid table row
141 * - column - The grid {@link Ext.grid.column.Column Column} defining the column that is being edited.
142 * - rowIdx - The row index that is being edited
143 * - colIdx - The column index that is being edited
144 * - cancel - Set this to true to cancel the edit or return false from your handler.
146 this.callParent(arguments);
147 this.editors = Ext.create('Ext.util.MixedCollection', false, function(editor) {
148 return editor.editorId;
150 this.editTask = Ext.create('Ext.util.DelayedTask');
153 onReconfigure: function(){
154 this.editors.clear();
160 * AbstractComponent calls destroy on all its plugins at destroy time.
162 destroy: function() {
164 me.editTask.cancel();
165 me.editors.each(Ext.destroy, Ext);
167 me.callParent(arguments);
170 onBodyScroll: function() {
171 var ed = this.getActiveEditor();
172 if (ed && ed.field) {
173 if (ed.field.triggerBlur) {
174 ed.field.triggerBlur();
182 // Template method called from base class's initEvents
183 initCancelTriggers: function() {
188 view.addElListener('mousewheel', me.cancelEdit, me);
189 me.mon(view, 'bodyscroll', me.onBodyScroll, me);
191 columnresize: me.cancelEdit,
192 columnmove: me.cancelEdit,
198 * Starts editing the specified record, using the specified Column definition to define which field is being edited.
199 * @param {Ext.data.Model} record The Store data record which backs the row to be edited.
200 * @param {Ext.data.Model} columnHeader The Column object defining the column to be edited. @override
202 startEdit: function(record, columnHeader) {
204 value = record.get(columnHeader.dataIndex),
205 context = me.getEditingContext(record, columnHeader),
208 record = context.record;
209 columnHeader = context.column;
211 // Complete the edit now, before getting the editor's target
212 // cell DOM element. Completing the edit causes a view refresh.
215 context.originalValue = context.value = value;
216 if (me.beforeEdit(context) === false || me.fireEvent('beforeedit', context) === false || context.cancel) {
220 // See if the field is editable for the requested record
221 if (columnHeader && !columnHeader.getEditor(record)) {
225 ed = me.getEditor(record, columnHeader);
227 me.context = context;
228 me.setActiveEditor(ed);
229 me.setActiveRecord(record);
230 me.setActiveColumn(columnHeader);
232 // Defer, so we have some time between view scroll to sync up the editor
233 me.editTask.delay(15, ed.startEdit, ed, [me.getCell(record, columnHeader), value]);
235 // BrowserBug: WebKit & IE refuse to focus the element, rather
236 // it will focus it and then immediately focus the body. This
237 // temporary hack works for Webkit and IE6. IE7 and 8 are still
239 me.grid.getView().getEl(columnHeader).focus((Ext.isWebKit || Ext.isIE) ? 10 : false);
243 completeEdit: function() {
244 var activeEd = this.getActiveEditor();
246 activeEd.completeEdit();
250 // internal getters/setters
251 setActiveEditor: function(ed) {
252 this.activeEditor = ed;
255 getActiveEditor: function() {
256 return this.activeEditor;
259 setActiveColumn: function(column) {
260 this.activeColumn = column;
263 getActiveColumn: function() {
264 return this.activeColumn;
267 setActiveRecord: function(record) {
268 this.activeRecord = record;
271 getActiveRecord: function() {
272 return this.activeRecord;
275 getEditor: function(record, column) {
277 editors = me.editors,
278 editorId = column.getItemId(),
279 editor = editors.getByKey(editorId);
284 editor = column.getEditor(record);
289 // Allow them to specify a CellEditor in the Column
290 if (!(editor instanceof Ext.grid.CellEditor)) {
291 editor = Ext.create('Ext.grid.CellEditor', {
296 editor.parentEl = me.grid.getEditorParent();
297 // editor.parentEl should be set here.
300 specialkey: me.onSpecialKey,
301 complete: me.onEditComplete,
302 canceledit: me.cancelEdit
310 setColumnField: function(column, field) {
311 var ed = this.editors.getByKey(column.getItemId());
312 Ext.destroy(ed, column.field);
313 this.editors.removeAtKey(column.getItemId());
314 this.callParent(arguments);
318 * Gets the cell (td) for a particular record and column.
319 * @param {Ext.data.Model} record
320 * @param {Ext.grid.column.Column} column
323 getCell: function(record, column) {
324 return this.grid.getView().getCell(record, column);
327 onSpecialKey: function(ed, field, e) {
328 var grid = this.grid,
330 if (e.getKey() === e.TAB) {
332 sm = grid.getSelectionModel();
333 if (sm.onEditorTab) {
334 sm.onEditorTab(this, e);
339 onEditComplete : function(ed, value, startValue) {
342 sm = grid.getSelectionModel(),
343 activeColumn = me.getActiveColumn(),
347 dataIndex = activeColumn.dataIndex;
349 me.setActiveEditor(null);
350 me.setActiveColumn(null);
351 me.setActiveRecord(null);
352 delete sm.wasEditing;
354 if (!me.validateEdit()) {
357 // Only update the record if the new value is different than the
358 // startValue, when the view refreshes its el will gain focus
359 if (value !== startValue) {
360 me.context.record.set(dataIndex, value);
361 // Restore focus back to the view's element.
363 grid.getView().getEl(activeColumn).focus();
365 me.context.value = value;
366 me.fireEvent('edit', me, me.context);
371 * Cancels any active editing.
373 cancelEdit: function() {
375 activeEd = me.getActiveEditor(),
376 viewEl = me.grid.getView().getEl(me.getActiveColumn());
378 me.setActiveEditor(null);
379 me.setActiveColumn(null);
380 me.setActiveRecord(null);
382 activeEd.cancelEdit();
388 * Starts editing by position (row/column)
389 * @param {Object} position A position with keys of row and column.
391 startEditByPosition: function(position) {
394 sm = grid.getSelectionModel(),
395 editRecord = grid.store.getAt(position.row),
396 editColumnHeader = grid.headerCt.getHeaderAtIndex(position.column);
398 if (sm.selectByPosition) {
399 sm.selectByPosition(position);
401 me.startEdit(editRecord, editColumnHeader);