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.CellSelectionModel"></div>/**
15 * @class Ext.grid.CellSelectionModel
16 * @extends Ext.grid.AbstractSelectionModel
17 * This class provides the basic implementation for <i>single</i> <b>cell</b> selection in a grid.
18 * The object stored as the selection contains the following properties:
19 * <div class="mdetail-params"><ul>
20 * <li><b>cell</b> : see {@link #getSelectedCell}
21 * <li><b>record</b> : Ext.data.record The {@link Ext.data.Record Record}
22 * which provides the data for the row containing the selection</li>
25 * @param {Object} config The object containing the configuration of this model.
27 Ext.grid.CellSelectionModel = function(config){
28 Ext.apply(this, config);
30 this.selection = null;
33 <div id="event-Ext.grid.CellSelectionModel-beforecellselect"></div>/**
34 * @event beforecellselect
35 * Fires before a cell is selected, return false to cancel the selection.
36 * @param {SelectionModel} this
37 * @param {Number} rowIndex The selected row index
38 * @param {Number} colIndex The selected cell index
41 <div id="event-Ext.grid.CellSelectionModel-cellselect"></div>/**
43 * Fires when a cell is selected.
44 * @param {SelectionModel} this
45 * @param {Number} rowIndex The selected row index
46 * @param {Number} colIndex The selected cell index
49 <div id="event-Ext.grid.CellSelectionModel-selectionchange"></div>/**
50 * @event selectionchange
51 * Fires when the active selection changes.
52 * @param {SelectionModel} this
53 * @param {Object} selection null for no selection or an object with two properties
54 * <div class="mdetail-params"><ul>
55 * <li><b>cell</b> : see {@link #getSelectedCell}
56 * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record Record}
57 * which provides the data for the row containing the selection</p></li>
63 Ext.grid.CellSelectionModel.superclass.constructor.call(this);
66 Ext.extend(Ext.grid.CellSelectionModel, Ext.grid.AbstractSelectionModel, {
69 initEvents : function(){
70 this.grid.on('cellmousedown', this.handleMouseDown, this);
71 this.grid.on(Ext.EventManager.useKeydown ? 'keydown' : 'keypress', this.handleKeyDown, this);
72 this.grid.getView().on({
74 refresh: this.onViewChange,
75 rowupdated: this.onRowUpdated,
76 beforerowremoved: this.clearSelections,
77 beforerowsinserted: this.clearSelections
79 if(this.grid.isEditor){
80 this.grid.on('beforeedit', this.beforeEdit, this);
85 beforeEdit : function(e){
86 this.select(e.row, e.column, false, true, e.record);
90 onRowUpdated : function(v, index, r){
91 if(this.selection && this.selection.record == r){
92 v.onCellSelect(index, this.selection.cell[1]);
97 onViewChange : function(){
98 this.clearSelections(true);
101 <div id="method-Ext.grid.CellSelectionModel-getSelectedCell"></div>/**
102 * Returns an array containing the row and column indexes of the currently selected cell
103 * (e.g., [0, 0]), or null if none selected. The array has elements:
104 * <div class="mdetail-params"><ul>
105 * <li><b>rowIndex</b> : Number<p class="sub-desc">The index of the selected row</p></li>
106 * <li><b>cellIndex</b> : Number<p class="sub-desc">The index of the selected cell.
107 * Due to possible column reordering, the cellIndex should <b>not</b> be used as an
108 * index into the Record's data. Instead, use the cellIndex to determine the <i>name</i>
109 * of the selected cell and use the field name to retrieve the data value from the record:<pre><code>
111 var fieldName = grid.getColumnModel().getDataIndex(cellIndex);
112 // get data value based on name
113 var data = record.get(fieldName);
114 * </code></pre></p></li>
116 * @return {Array} An array containing the row and column indexes of the selected cell, or null if none selected.
118 getSelectedCell : function(){
119 return this.selection ? this.selection.cell : null;
122 <div id="method-Ext.grid.CellSelectionModel-clearSelections"></div>/**
123 * If anything is selected, clears all selections and fires the selectionchange event.
124 * @param {Boolean} preventNotify <tt>true</tt> to prevent the gridview from
125 * being notified about the change.
127 clearSelections : function(preventNotify){
128 var s = this.selection;
130 if(preventNotify !== true){
131 this.grid.view.onCellDeselect(s.cell[0], s.cell[1]);
133 this.selection = null;
134 this.fireEvent("selectionchange", this, null);
138 <div id="method-Ext.grid.CellSelectionModel-hasSelection"></div>/**
139 * Returns <tt>true</tt> if there is a selection.
142 hasSelection : function(){
143 return this.selection ? true : false;
147 handleMouseDown : function(g, row, cell, e){
148 if(e.button !== 0 || this.isLocked()){
151 this.select(row, cell);
154 <div id="method-Ext.grid.CellSelectionModel-select"></div>/**
155 * Selects a cell. Before selecting a cell, fires the
156 * {@link #beforecellselect} event. If this check is satisfied the cell
157 * will be selected and followed up by firing the {@link #cellselect} and
158 * {@link #selectionchange} events.
159 * @param {Number} rowIndex The index of the row to select
160 * @param {Number} colIndex The index of the column to select
161 * @param {Boolean} preventViewNotify (optional) Specify <tt>true</tt> to
162 * prevent notifying the view (disables updating the selected appearance)
163 * @param {Boolean} preventFocus (optional) Whether to prevent the cell at
164 * the specified rowIndex / colIndex from being focused.
165 * @param {Ext.data.Record} r (optional) The record to select
167 select : function(rowIndex, colIndex, preventViewNotify, preventFocus, /*internal*/ r){
168 if(this.fireEvent("beforecellselect", this, rowIndex, colIndex) !== false){
169 this.clearSelections();
170 r = r || this.grid.store.getAt(rowIndex);
173 cell : [rowIndex, colIndex]
175 if(!preventViewNotify){
176 var v = this.grid.getView();
177 v.onCellSelect(rowIndex, colIndex);
178 if(preventFocus !== true){
179 v.focusCell(rowIndex, colIndex);
182 this.fireEvent("cellselect", this, rowIndex, colIndex);
183 this.fireEvent("selectionchange", this, this.selection);
188 isSelectable : function(rowIndex, colIndex, cm){
189 return !cm.isHidden(colIndex);
193 onEditorKey: function(field, e){
194 if(e.getKey() == e.TAB){
195 this.handleKeyDown(e);
200 handleKeyDown : function(e){
201 if(!e.isNavKeyPress()){
209 walk = function(row, col, step){
214 g.isEditor && g.editing ? sm.acceptsNav : sm.isSelectable, // *** handle tabbing while editorgrid is in edit mode
218 cell, newCell, r, c, ae;
227 // *** call e.stopEvent() only for non ESC, PAGE UP/DOWN KEYS
233 cell = walk(0, 0, 1); // *** use private walk() function defined above
235 this.select(cell[0], cell[1]);
240 cell = s.cell; // currently selected cell
241 r = cell[0]; // current row
242 c = cell[1]; // current column
247 newCell = walk(r, c - 1, -1);
249 newCell = walk(r, c + 1, 1);
253 newCell = walk(r + 1, c, 1);
256 newCell = walk(r - 1, c, -1);
259 newCell = walk(r, c + 1, 1);
262 newCell = walk(r, c - 1, -1);
265 if (g.isEditor && !g.editing) {
266 g.startEditing(r, c);
273 // *** reassign r & c variables to newly-selected cell's row and column
277 this.select(r, c); // *** highlight newly-selected cell and update selection
279 if(g.isEditor && g.editing){ // *** handle tabbing while editorgrid is in edit mode
281 if(ae && ae.field.triggerBlur){
282 // *** if activeEditor is a TriggerField, explicitly call its triggerBlur() method
283 ae.field.triggerBlur();
285 g.startEditing(r, c);
290 acceptsNav : function(row, col, cm){
291 return !cm.isHidden(col) && cm.isCellEditable(col, row);