3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.grid.EditorGridPanel
9 * @extends Ext.grid.GridPanel
10 * <p>This class extends the {@link Ext.grid.GridPanel GridPanel Class} to provide cell editing
11 * on selected {@link Ext.grid.Column columns}. The editable columns are specified by providing
12 * an {@link Ext.grid.ColumnModel#editor editor} in the {@link Ext.grid.Column column configuration}.</p>
13 * <p>Editability of columns may be controlled programatically by inserting an implementation
14 * of {@link Ext.grid.ColumnModel#isCellEditable isCellEditable} into the
15 * {@link Ext.grid.ColumnModel ColumnModel}.</p>
16 * <p>Editing is performed on the value of the <i>field</i> specified by the column's
17 * <tt>{@link Ext.grid.ColumnModel#dataIndex dataIndex}</tt> in the backing {@link Ext.data.Store Store}
18 * (so if you are using a {@link Ext.grid.ColumnModel#setRenderer renderer} in order to display
19 * transformed data, this must be accounted for).</p>
20 * <p>If a value-to-description mapping is used to render a column, then a {@link Ext.form.Field#ComboBox ComboBox}
21 * which uses the same {@link Ext.form.Field#valueField value}-to-{@link Ext.form.Field#displayFieldField description}
22 * mapping would be an appropriate editor.</p>
23 * If there is a more complex mismatch between the visible data in the grid, and the editable data in
24 * the {@link Edt.data.Store Store}, then code to transform the data both before and after editing can be
25 * injected using the {@link #beforeedit} and {@link #afteredit} events.
27 * @param {Object} config The config object
30 Ext.grid.EditorGridPanel = Ext.extend(Ext.grid.GridPanel, {
32 * @cfg {Number} clicksToEdit
33 * <p>The number of clicks on a cell required to display the cell's editor (defaults to 2).</p>
34 * <p>Setting this option to 'auto' means that mousedown <i>on the selected cell</i> starts
35 * editing that cell.</p>
40 * @cfg {Boolean} forceValidation
41 * True to force validation even if the value is unmodified (defaults to false)
43 forceValidation: false,
51 * @cfg {Boolean} autoEncode
52 * True to automatically HTML encode and decode values pre and post edit (defaults to false)
57 * @cfg {Boolean} trackMouseOver @hide
60 trackMouseOver: false, // causes very odd FF errors
63 initComponent : function(){
64 Ext.grid.EditorGridPanel.superclass.initComponent.call(this);
68 * @cfg {Object} selModel Any subclass of AbstractSelectionModel that will provide the selection model for
69 * the grid (defaults to {@link Ext.grid.CellSelectionModel} if not specified).
71 this.selModel = new Ext.grid.CellSelectionModel();
74 this.activeEditor = null;
79 * Fires before cell editing is triggered. The edit event object has the following properties <br />
80 * <ul style="padding:5px;padding-left:16px;">
81 * <li>grid - This grid</li>
82 * <li>record - The record being edited</li>
83 * <li>field - The field name being edited</li>
84 * <li>value - The value for the field being edited.</li>
85 * <li>row - The grid row index</li>
86 * <li>column - The grid column index</li>
87 * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
89 * @param {Object} e An edit event (see above for description)
94 * Fires after a cell is edited. The edit event object has the following properties <br />
95 * <ul style="padding:5px;padding-left:16px;">
96 * <li>grid - This grid</li>
97 * <li>record - The record being edited</li>
98 * <li>field - The field name being edited</li>
99 * <li>value - The value being set</li>
100 * <li>originalValue - The original value for the field, before the edit.</li>
101 * <li>row - The grid row index</li>
102 * <li>column - The grid column index</li>
106 grid.on('afteredit', afterEdit, this );
108 function afterEdit(e) {
109 // execute an XHR to send/commit data to the server, in callback do (if successful):
113 * @param {Object} e An edit event (see above for description)
117 * @event validateedit
118 * Fires after a cell is edited, but before the value is set in the record. Return false
119 * to cancel the change. The edit event object has the following properties <br />
120 * <ul style="padding:5px;padding-left:16px;">
121 * <li>grid - This grid</li>
122 * <li>record - The record being edited</li>
123 * <li>field - The field name being edited</li>
124 * <li>value - The value being set</li>
125 * <li>originalValue - The original value for the field, before the edit.</li>
126 * <li>row - The grid row index</li>
127 * <li>column - The grid column index</li>
128 * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
130 * Usage example showing how to remove the red triangle (dirty record indicator) from some
131 * records (not all). By observing the grid's validateedit event, it can be cancelled if
132 * the edit occurs on a targeted row (for example) and then setting the field's new value
133 * in the Record directly:
135 grid.on('validateedit', function(e) {
138 if (e.row == myTargetRow) {
140 e.record.data[e.field] = e.value;
144 * @param {Object} e An edit event (see above for description)
151 initEvents : function(){
152 Ext.grid.EditorGridPanel.superclass.initEvents.call(this);
154 this.on("bodyscroll", this.stopEditing, this, [true]);
155 this.on("columnresize", this.stopEditing, this, [true]);
157 if(this.clicksToEdit == 1){
158 this.on("cellclick", this.onCellDblClick, this);
160 if(this.clicksToEdit == 'auto' && this.view.mainBody){
161 this.view.mainBody.on("mousedown", this.onAutoEditClick, this);
163 this.on("celldblclick", this.onCellDblClick, this);
168 onCellDblClick : function(g, row, col){
169 this.startEditing(row, col);
173 onAutoEditClick : function(e, t){
177 var row = this.view.findRowIndex(t);
178 var col = this.view.findCellIndex(t);
179 if(row !== false && col !== false){
181 if(this.selModel.getSelectedCell){ // cell sm
182 var sc = this.selModel.getSelectedCell();
183 if(sc && sc[0] === row && sc[1] === col){
184 this.startEditing(row, col);
187 if(this.selModel.isSelected(row)){
188 this.startEditing(row, col);
195 onEditComplete : function(ed, value, startValue){
196 this.editing = false;
197 this.activeEditor = null;
198 ed.un("specialkey", this.selModel.onEditorKey, this.selModel);
200 var field = this.colModel.getDataIndex(ed.col);
201 value = this.postEditValue(value, startValue, r, field);
202 if(this.forceValidation === true || String(value) !== String(startValue)){
207 originalValue: startValue,
213 if(this.fireEvent("validateedit", e) !== false && !e.cancel && String(value) !== String(startValue)){
214 r.set(field, e.value);
216 this.fireEvent("afteredit", e);
219 this.view.focusCell(ed.row, ed.col);
223 * Starts editing the specified for the specified row/column
224 * @param {Number} rowIndex
225 * @param {Number} colIndex
227 startEditing : function(row, col){
229 if(this.colModel.isCellEditable(col, row)){
230 this.view.ensureVisible(row, col, true);
231 var r = this.store.getAt(row);
232 var field = this.colModel.getDataIndex(col);
237 value: r.data[field],
242 if(this.fireEvent("beforeedit", e) !== false && !e.cancel){
244 var ed = this.colModel.getCellEditor(col, row);
249 ed.render(this.view.getEditorParent(ed));
251 (function(){ // complex but required for focus issues in safari, ie and opera
255 ed.on("complete", this.onEditComplete, this, {single: true});
256 ed.on("specialkey", this.selModel.onEditorKey, this.selModel);
258 * The currently active editor or null
261 this.activeEditor = ed;
262 var v = this.preEditValue(r, field);
263 ed.startEdit(this.view.getCell(row, col).firstChild, v === undefined ? '' : v);
270 preEditValue : function(r, field){
271 var value = r.data[field];
272 return this.autoEncode && typeof value == 'string' ? Ext.util.Format.htmlDecode(value) : value;
276 postEditValue : function(value, originalValue, r, field){
277 return this.autoEncode && typeof value == 'string' ? Ext.util.Format.htmlEncode(value) : value;
281 * Stops any active editing
282 * @param {Boolean} cancel (optional) True to cancel any changes
284 stopEditing : function(cancel){
285 if(this.activeEditor){
286 this.activeEditor[cancel === true ? 'cancelEdit' : 'completeEdit']();
288 this.activeEditor = null;
291 Ext.reg('editorgrid', Ext.grid.EditorGridPanel);