1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-grid.plugin.CellEditing'>/**
2 </span> * @class Ext.grid.plugin.CellEditing
3 * @extends Ext.grid.plugin.Editing
5 * The Ext.grid.plugin.CellEditing plugin injects editing at a cell level for a Grid. Only a single
6 * cell will be editable at a time. The field that will be used for the editor is defined at the
7 * {@link Ext.grid.column.Column#field field}. The editor can be a field instance or a field configuration.
9 * If an editor is not specified for a particular column then that cell will not be editable and it will
10 * be skipped when activated via the mouse or the keyboard.
12 * The editor may be shared for each column in the grid, or a different one may be specified for each column.
13 * An appropriate field type should be chosen to match the data structure that it will be editing. For example,
14 * to edit a date, it would be useful to specify {@link Ext.form.field.Date} as the editor.
16 * {@img Ext.grid.plugin.CellEditing/Ext.grid.plugin.CellEditing.png Ext.grid.plugin.CellEditing plugin}
20 * Ext.create('Ext.data.Store', {
21 * storeId:'simpsonsStore',
22 * fields:['name', 'email', 'phone'],
24 * {"name":"Lisa", "email":"lisa@simpsons.com", "phone":"555-111-1224"},
25 * {"name":"Bart", "email":"bart@simpsons.com", "phone":"555--222-1234"},
26 * {"name":"Homer", "email":"home@simpsons.com", "phone":"555-222-1244"},
27 * {"name":"Marge", "email":"marge@simpsons.com", "phone":"555-222-1254"}
38 * Ext.create('Ext.grid.Panel', {
40 * store: Ext.data.StoreManager.lookup('simpsonsStore'),
42 * {header: 'Name', dataIndex: 'name', field: 'textfield'},
43 * {header: 'Email', dataIndex: 'email', flex:1,
49 * {header: 'Phone', dataIndex: 'phone'}
51 * selType: 'cellmodel',
53 * Ext.create('Ext.grid.plugin.CellEditing', {
59 * renderTo: Ext.getBody()
62 Ext.define('Ext.grid.plugin.CellEditing', {
63 alias: 'plugin.cellediting',
64 extend: 'Ext.grid.plugin.Editing',
65 requires: ['Ext.grid.CellEditor'],
67 constructor: function() {
68 <span id='Ext-grid.plugin.CellEditing-event-beforeedit'> /**
69 </span> * @event beforeedit
70 * Fires before cell editing is triggered. The edit event object has the following properties <br />
71 * <ul style="padding:5px;padding-left:16px;">
72 * <li>grid - The grid</li>
73 * <li>record - The record being edited</li>
74 * <li>field - The field name being edited</li>
75 * <li>value - The value for the field being edited.</li>
76 * <li>row - The grid table row</li>
77 * <li>column - The grid {@link Ext.grid.column.Column Column} defining the column that is being edited.</li>
78 * <li>rowIdx - The row index that is being edited</li>
79 * <li>colIdx - The column index that is being edited</li>
80 * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
82 * @param {Ext.grid.plugin.Editing} editor
83 * @param {Object} e An edit event (see above for description)
85 <span id='Ext-grid.plugin.CellEditing-event-edit'> /**
87 * Fires after a cell is edited. The edit event object has the following properties <br />
88 * <ul style="padding:5px;padding-left:16px;">
89 * <li>grid - The grid</li>
90 * <li>record - The record that was edited</li>
91 * <li>field - The field name that was edited</li>
92 * <li>value - The value being set</li>
93 * <li>originalValue - The original value for the field, before the edit.</li>
94 * <li>row - The grid table row</li>
95 * <li>column - The grid {@link Ext.grid.column.Column Column} defining the column that was edited.</li>
96 * <li>rowIdx - The row index that was edited</li>
97 * <li>colIdx - The column index that was edited</li>
100 * <pre><code>
101 grid.on('edit', onEdit, this);
104 // execute an XHR to send/commit data to the server, in callback do (if successful):
107 * </code></pre>
108 * @param {Ext.grid.plugin.Editing} editor
109 * @param {Object} e An edit event (see above for description)
111 <span id='Ext-grid.plugin.CellEditing-event-validateedit'> /**
112 </span> * @event validateedit
113 * Fires after a cell is edited, but before the value is set in the record. Return false
114 * to cancel the change. The edit event object has the following properties <br />
115 * <ul style="padding:5px;padding-left:16px;">
116 * <li>grid - The grid</li>
117 * <li>record - The record being edited</li>
118 * <li>field - The field name being edited</li>
119 * <li>value - The value being set</li>
120 * <li>originalValue - The original value for the field, before the edit.</li>
121 * <li>row - The grid table row</li>
122 * <li>column - The grid {@link Ext.grid.column.Column Column} defining the column that is being edited.</li>
123 * <li>rowIdx - The row index that is being edited</li>
124 * <li>colIdx - The column index that is being edited</li>
125 * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
127 * Usage example showing how to remove the red triangle (dirty record indicator) from some
128 * records (not all). By observing the grid's validateedit event, it can be cancelled if
129 * the edit occurs on a targeted row (for example) and then setting the field's new value
130 * in the Record directly:
131 * <pre><code>
132 grid.on('validateedit', function(e) {
135 if (e.row == myTargetRow) {
137 e.record.data[e.field] = e.value;
140 * </code></pre>
141 * @param {Ext.grid.plugin.Editing} editor
142 * @param {Object} e An edit event (see above for description)
144 this.callParent(arguments);
145 this.editors = Ext.create('Ext.util.MixedCollection', false, function(editor) {
146 return editor.editorId;
150 <span id='Ext-grid.plugin.CellEditing-method-destroy'> /**
152 * AbstractComponent calls destroy on all its plugins at destroy time.
154 destroy: function() {
156 me.editors.each(Ext.destroy, Ext);
158 me.callParent(arguments);
162 // Template method called from base class's initEvents
163 initCancelTriggers: function() {
176 columnresize: me.cancelEdit,
177 columnmove: me.cancelEdit,
182 <span id='Ext-grid.plugin.CellEditing-method-startEdit'> /**
183 </span> * Start editing the specified record, using the specified Column definition to define which field is being edited.
184 * @param {Model} record The Store data record which backs the row to be edited.
185 * @param {Model} columnHeader The Column object defining the column to be edited.
188 startEdit: function(record, columnHeader) {
190 ed = me.getEditor(record, columnHeader),
191 value = record.get(columnHeader.dataIndex),
192 context = me.getEditingContext(record, columnHeader);
194 record = context.record;
195 columnHeader = context.column;
197 // Complete the edit now, before getting the editor's target
198 // cell DOM element. Completing the edit causes a view refresh.
201 // See if the field is editable for the requested record
202 if (columnHeader && !columnHeader.getEditor(record)) {
207 context.originalValue = context.value = value;
208 if (me.beforeEdit(context) === false || me.fireEvent('beforeedit', context) === false || context.cancel) {
212 me.context = context;
213 me.setActiveEditor(ed);
214 me.setActiveRecord(record);
215 me.setActiveColumn(columnHeader);
217 // Defer, so we have some time between view scroll to sync up the editor
218 Ext.defer(ed.startEdit, 15, ed, [me.getCell(record, columnHeader), value]);
220 // BrowserBug: WebKit & IE refuse to focus the element, rather
221 // it will focus it and then immediately focus the body. This
222 // temporary hack works for Webkit and IE6. IE7 and 8 are still
224 me.grid.getView().el.focus((Ext.isWebKit || Ext.isIE) ? 10 : false);
228 completeEdit: function() {
229 var activeEd = this.getActiveEditor();
231 activeEd.completeEdit();
235 // internal getters/setters
236 setActiveEditor: function(ed) {
237 this.activeEditor = ed;
240 getActiveEditor: function() {
241 return this.activeEditor;
244 setActiveColumn: function(column) {
245 this.activeColumn = column;
248 getActiveColumn: function() {
249 return this.activeColumn;
252 setActiveRecord: function(record) {
253 this.activeRecord = record;
256 getActiveRecord: function() {
257 return this.activeRecord;
260 getEditor: function(record, column) {
261 var editors = this.editors,
262 editorId = column.itemId || column.id,
263 editor = editors.getByKey(editorId);
268 editor = column.getEditor(record);
273 // Allow them to specify a CellEditor in the Column
274 if (!(editor instanceof Ext.grid.CellEditor)) {
275 editor = Ext.create('Ext.grid.CellEditor', {
280 editor.parentEl = this.grid.getEditorParent();
281 // editor.parentEl should be set here.
284 specialkey: this.onSpecialKey,
285 complete: this.onEditComplete,
286 canceledit: this.cancelEdit
293 <span id='Ext-grid.plugin.CellEditing-method-getCell'> /**
294 </span> * Get the cell (td) for a particular record and column.
295 * @param {Ext.data.Model} record
296 * @param {Ext.grid.column.Colunm} column
299 getCell: function(record, column) {
300 return this.grid.getView().getCell(record, column);
303 onSpecialKey: function(ed, field, e) {
304 var grid = this.grid,
306 if (e.getKey() === e.TAB) {
308 sm = grid.getSelectionModel();
309 if (sm.onEditorTab) {
310 sm.onEditorTab(this, e);
315 onEditComplete : function(ed, value, startValue) {
318 sm = grid.getSelectionModel(),
319 dataIndex = me.getActiveColumn().dataIndex;
321 me.setActiveEditor(null);
322 me.setActiveColumn(null);
323 me.setActiveRecord(null);
324 delete sm.wasEditing;
326 if (!me.validateEdit()) {
329 me.context.record.set(dataIndex, value);
330 me.fireEvent('edit', me, me.context);
333 <span id='Ext-grid.plugin.CellEditing-method-cancelEdit'> /**
334 </span> * Cancel any active editing.
336 cancelEdit: function() {
338 activeEd = me.getActiveEditor(),
339 viewEl = me.grid.getView().el;
341 me.setActiveEditor(null);
342 me.setActiveColumn(null);
343 me.setActiveRecord(null);
345 activeEd.cancelEdit();
350 <span id='Ext-grid.plugin.CellEditing-method-startEditByPosition'> /**
351 </span> * Starts editing by position (row/column)
352 * @param {Object} position A position with keys of row and column.
354 startEditByPosition: function(position) {
357 sm = grid.getSelectionModel(),
358 editRecord = grid.store.getAt(position.row),
359 editColumnHeader = grid.headerCt.getHeaderAtIndex(position.column);
361 if (sm.selectByPosition) {
362 sm.selectByPosition(position);
364 me.startEdit(editRecord, editColumnHeader);
366 });</pre></pre></body></html>