3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.grid.EditorGridPanel"></div>/**
15 * @class Ext.grid.EditorGridPanel
16 * @extends Ext.grid.GridPanel
17 * <p>This class extends the {@link Ext.grid.GridPanel GridPanel Class} to provide cell editing
18 * on selected {@link Ext.grid.Column columns}. The editable columns are specified by providing
19 * an {@link Ext.grid.ColumnModel#editor editor} in the {@link Ext.grid.Column column configuration}.</p>
20 * <p>Editability of columns may be controlled programatically by inserting an implementation
21 * of {@link Ext.grid.ColumnModel#isCellEditable isCellEditable} into the
22 * {@link Ext.grid.ColumnModel ColumnModel}.</p>
23 * <p>Editing is performed on the value of the <i>field</i> specified by the column's
24 * <tt>{@link Ext.grid.ColumnModel#dataIndex dataIndex}</tt> in the backing {@link Ext.data.Store Store}
25 * (so if you are using a {@link Ext.grid.ColumnModel#setRenderer renderer} in order to display
26 * transformed data, this must be accounted for).</p>
27 * <p>If a value-to-description mapping is used to render a column, then a {@link Ext.form.Field#ComboBox ComboBox}
28 * which uses the same {@link Ext.form.Field#valueField value}-to-{@link Ext.form.Field#displayFieldField description}
29 * mapping would be an appropriate editor.</p>
30 * If there is a more complex mismatch between the visible data in the grid, and the editable data in
31 * the {@link Edt.data.Store Store}, then code to transform the data both before and after editing can be
32 * injected using the {@link #beforeedit} and {@link #afteredit} events.
34 * @param {Object} config The config object
37 Ext.grid.EditorGridPanel = Ext.extend(Ext.grid.GridPanel, {
38 <div id="cfg-Ext.grid.EditorGridPanel-clicksToEdit"></div>/**
39 * @cfg {Number} clicksToEdit
40 * <p>The number of clicks on a cell required to display the cell's editor (defaults to 2).</p>
41 * <p>Setting this option to 'auto' means that mousedown <i>on the selected cell</i> starts
42 * editing that cell.</p>
46 <div id="cfg-Ext.grid.EditorGridPanel-forceValidation"></div>/**
47 * @cfg {Boolean} forceValidation
48 * True to force validation even if the value is unmodified (defaults to false)
50 forceValidation: false,
57 <div id="cfg-Ext.grid.EditorGridPanel-autoEncode"></div>/**
58 * @cfg {Boolean} autoEncode
59 * True to automatically HTML encode and decode values pre and post edit (defaults to false)
63 <div id="cfg-Ext.grid.EditorGridPanel-trackMouseOver"></div>/**
64 * @cfg {Boolean} trackMouseOver @hide
67 trackMouseOver: false, // causes very odd FF errors
70 initComponent : function(){
71 Ext.grid.EditorGridPanel.superclass.initComponent.call(this);
74 <div id="cfg-Ext.grid.EditorGridPanel-selModel"></div>/**
75 * @cfg {Object} selModel Any subclass of AbstractSelectionModel that will provide the selection model for
76 * the grid (defaults to {@link Ext.grid.CellSelectionModel} if not specified).
78 this.selModel = new Ext.grid.CellSelectionModel();
81 this.activeEditor = null;
84 <div id="event-Ext.grid.EditorGridPanel-beforeedit"></div>/**
86 * Fires before cell editing is triggered. The edit event object has the following properties <br />
87 * <ul style="padding:5px;padding-left:16px;">
88 * <li>grid - This grid</li>
89 * <li>record - The record being edited</li>
90 * <li>field - The field name being edited</li>
91 * <li>value - The value for the field being edited.</li>
92 * <li>row - The grid row index</li>
93 * <li>column - The grid column index</li>
94 * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
96 * @param {Object} e An edit event (see above for description)
99 <div id="event-Ext.grid.EditorGridPanel-afteredit"></div>/**
101 * Fires after a cell is edited. The edit event object has the following properties <br />
102 * <ul style="padding:5px;padding-left:16px;">
103 * <li>grid - This grid</li>
104 * <li>record - The record being edited</li>
105 * <li>field - The field name being edited</li>
106 * <li>value - The value being set</li>
107 * <li>originalValue - The original value for the field, before the edit.</li>
108 * <li>row - The grid row index</li>
109 * <li>column - The grid column index</li>
113 grid.on('afteredit', afterEdit, this );
115 function afterEdit(e) {
116 // execute an XHR to send/commit data to the server, in callback do (if successful):
120 * @param {Object} e An edit event (see above for description)
123 <div id="event-Ext.grid.EditorGridPanel-validateedit"></div>/**
124 * @event validateedit
125 * Fires after a cell is edited, but before the value is set in the record. Return false
126 * to cancel the change. The edit event object has the following properties <br />
127 * <ul style="padding:5px;padding-left:16px;">
128 * <li>grid - This grid</li>
129 * <li>record - The record being edited</li>
130 * <li>field - The field name being edited</li>
131 * <li>value - The value being set</li>
132 * <li>originalValue - The original value for the field, before the edit.</li>
133 * <li>row - The grid row index</li>
134 * <li>column - The grid column index</li>
135 * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
137 * Usage example showing how to remove the red triangle (dirty record indicator) from some
138 * records (not all). By observing the grid's validateedit event, it can be cancelled if
139 * the edit occurs on a targeted row (for example) and then setting the field's new value
140 * in the Record directly:
142 grid.on('validateedit', function(e) {
145 if (e.row == myTargetRow) {
147 e.record.data[e.field] = e.value;
151 * @param {Object} e An edit event (see above for description)
158 initEvents : function(){
159 Ext.grid.EditorGridPanel.superclass.initEvents.call(this);
161 this.getGridEl().on('mousewheel', this.stopEditing.createDelegate(this, [true]), this);
162 this.on('columnresize', this.stopEditing, this, [true]);
164 if(this.clicksToEdit == 1){
165 this.on("cellclick", this.onCellDblClick, this);
167 var view = this.getView();
168 if(this.clicksToEdit == 'auto' && view.mainBody){
169 view.mainBody.on('mousedown', this.onAutoEditClick, this);
171 this.on('celldblclick', this.onCellDblClick, this);
176 onCellDblClick : function(g, row, col){
177 this.startEditing(row, col);
181 onAutoEditClick : function(e, t){
185 var row = this.view.findRowIndex(t);
186 var col = this.view.findCellIndex(t);
187 if(row !== false && col !== false){
189 if(this.selModel.getSelectedCell){ // cell sm
190 var sc = this.selModel.getSelectedCell();
191 if(sc && sc[0] === row && sc[1] === col){
192 this.startEditing(row, col);
195 if(this.selModel.isSelected(row)){
196 this.startEditing(row, col);
203 onEditComplete : function(ed, value, startValue){
204 this.editing = false;
205 this.activeEditor = null;
208 var field = this.colModel.getDataIndex(ed.col);
209 value = this.postEditValue(value, startValue, r, field);
210 if(this.forceValidation === true || String(value) !== String(startValue)){
215 originalValue: startValue,
221 if(this.fireEvent("validateedit", e) !== false && !e.cancel && String(value) !== String(startValue)){
222 r.set(field, e.value);
224 this.fireEvent("afteredit", e);
227 this.view.focusCell(ed.row, ed.col);
230 <div id="method-Ext.grid.EditorGridPanel-startEditing"></div>/**
231 * Starts editing the specified for the specified row/column
232 * @param {Number} rowIndex
233 * @param {Number} colIndex
235 startEditing : function(row, col){
237 if(this.colModel.isCellEditable(col, row)){
238 this.view.ensureVisible(row, col, true);
239 var r = this.store.getAt(row);
240 var field = this.colModel.getDataIndex(col);
245 value: r.data[field],
250 if(this.fireEvent("beforeedit", e) !== false && !e.cancel){
252 var ed = this.colModel.getCellEditor(col, row);
257 ed.parentEl = this.view.getEditorParent(ed);
262 c.field.focus(false, true);
267 specialkey: function(field, e){
268 this.getSelectionModel().onEditorKey(field, e);
270 complete: this.onEditComplete,
271 canceledit: this.stopEditing.createDelegate(this, [true])
283 this.activeEditor = ed;
284 var v = this.preEditValue(r, field);
285 ed.startEdit(this.view.getCell(row, col).firstChild, Ext.isDefined(v) ? v : '');
291 preEditValue : function(r, field){
292 var value = r.data[field];
293 return this.autoEncode && Ext.isString(value) ? Ext.util.Format.htmlDecode(value) : value;
297 postEditValue : function(value, originalValue, r, field){
298 return this.autoEncode && Ext.isString(value) ? Ext.util.Format.htmlEncode(value) : value;
301 <div id="method-Ext.grid.EditorGridPanel-stopEditing"></div>/**
302 * Stops any active editing
303 * @param {Boolean} cancel (optional) True to cancel any changes
305 stopEditing : function(cancel){
307 var ae = this.activeEditor;
309 ae[cancel === true ? 'cancelEdit' : 'completeEdit']();
310 this.view.focusCell(ae.row, ae.col);
312 this.activeEditor = null;
314 this.editing = false;
317 Ext.reg('editorgrid', Ext.grid.EditorGridPanel);</pre>