3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.grid.CellSelectionModel"></div>/**
9 * @class Ext.grid.CellSelectionModel
10 * @extends Ext.grid.AbstractSelectionModel
11 * This class provides the basic implementation for <i>single</i> <b>cell</b> selection in a grid.
12 * The object stored as the selection contains the following properties:
13 * <div class="mdetail-params"><ul>
14 * <li><b>cell</b> : see {@link #getSelectedCell}
15 * <li><b>record</b> : Ext.data.record The {@link Ext.data.Record Record}
16 * which provides the data for the row containing the selection</li>
19 * @param {Object} config The object containing the configuration of this model.
21 Ext.grid.CellSelectionModel = function(config){
22 Ext.apply(this, config);
24 this.selection = null;
27 <div id="event-Ext.grid.CellSelectionModel-beforecellselect"></div>/**
28 * @event beforecellselect
29 * Fires before a cell is selected, return false to cancel the selection.
30 * @param {SelectionModel} this
31 * @param {Number} rowIndex The selected row index
32 * @param {Number} colIndex The selected cell index
35 <div id="event-Ext.grid.CellSelectionModel-cellselect"></div>/**
37 * Fires when a cell is selected.
38 * @param {SelectionModel} this
39 * @param {Number} rowIndex The selected row index
40 * @param {Number} colIndex The selected cell index
43 <div id="event-Ext.grid.CellSelectionModel-selectionchange"></div>/**
44 * @event selectionchange
45 * Fires when the active selection changes.
46 * @param {SelectionModel} this
47 * @param {Object} selection null for no selection or an object with two properties
48 * <div class="mdetail-params"><ul>
49 * <li><b>cell</b> : see {@link #getSelectedCell}
50 * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record Record}
51 * which provides the data for the row containing the selection</p></li>
57 Ext.grid.CellSelectionModel.superclass.constructor.call(this);
60 Ext.extend(Ext.grid.CellSelectionModel, Ext.grid.AbstractSelectionModel, {
63 initEvents : function(){
64 this.grid.on("cellmousedown", this.handleMouseDown, this);
65 this.grid.getGridEl().on(Ext.EventManager.useKeydown ? "keydown" : "keypress", this.handleKeyDown, this);
66 var view = this.grid.view;
67 view.on("refresh", this.onViewChange, this);
68 view.on("rowupdated", this.onRowUpdated, this);
69 view.on("beforerowremoved", this.clearSelections, this);
70 view.on("beforerowsinserted", this.clearSelections, this);
71 if(this.grid.isEditor){
72 this.grid.on("beforeedit", this.beforeEdit, this);
77 beforeEdit : function(e){
78 this.select(e.row, e.column, false, true, e.record);
82 onRowUpdated : function(v, index, r){
83 if(this.selection && this.selection.record == r){
84 v.onCellSelect(index, this.selection.cell[1]);
89 onViewChange : function(){
90 this.clearSelections(true);
93 <div id="method-Ext.grid.CellSelectionModel-getSelectedCell"></div>/**
94 * Returns an array containing the row and column indexes of the currently selected cell
95 * (e.g., [0, 0]), or null if none selected. The array has elements:
96 * <div class="mdetail-params"><ul>
97 * <li><b>rowIndex</b> : Number<p class="sub-desc">The index of the selected row</p></li>
98 * <li><b>cellIndex</b> : Number<p class="sub-desc">The index of the selected cell.
99 * Due to possible column reordering, the cellIndex should <b>not</b> be used as an
100 * index into the Record's data. Instead, use the cellIndex to determine the <i>name</i>
101 * of the selected cell and use the field name to retrieve the data value from the record:<pre><code>
103 var fieldName = grid.getColumnModel().getDataIndex(cellIndex);
104 // get data value based on name
105 var data = record.get(fieldName);
106 * </code></pre></p></li>
108 * @return {Array} An array containing the row and column indexes of the selected cell, or null if none selected.
110 getSelectedCell : function(){
111 return this.selection ? this.selection.cell : null;
114 <div id="method-Ext.grid.CellSelectionModel-clearSelections"></div>/**
115 * If anything is selected, clears all selections and fires the selectionchange event.
116 * @param {Boolean} preventNotify <tt>true</tt> to prevent the gridview from
117 * being notified about the change.
119 clearSelections : function(preventNotify){
120 var s = this.selection;
122 if(preventNotify !== true){
123 this.grid.view.onCellDeselect(s.cell[0], s.cell[1]);
125 this.selection = null;
126 this.fireEvent("selectionchange", this, null);
130 <div id="method-Ext.grid.CellSelectionModel-hasSelection"></div>/**
131 * Returns <tt>true</tt> if there is a selection.
134 hasSelection : function(){
135 return this.selection ? true : false;
139 handleMouseDown : function(g, row, cell, e){
140 if(e.button !== 0 || this.isLocked()){
143 this.select(row, cell);
146 <div id="method-Ext.grid.CellSelectionModel-select"></div>/**
147 * Selects a cell. Before selecting a cell, fires the
148 * {@link #beforecellselect} event. If this check is satisfied the cell
149 * will be selected and followed up by firing the {@link #cellselect} and
150 * {@link #selectionchange} events.
151 * @param {Number} rowIndex The index of the row to select
152 * @param {Number} colIndex The index of the column to select
153 * @param {Boolean} preventViewNotify (optional) Specify <tt>true</tt> to
154 * prevent notifying the view (disables updating the selected appearance)
155 * @param {Boolean} preventFocus (optional) Whether to prevent the cell at
156 * the specified rowIndex / colIndex from being focused.
157 * @param {Ext.data.Record} r (optional) The record to select
159 select : function(rowIndex, colIndex, preventViewNotify, preventFocus, /*internal*/ r){
160 if(this.fireEvent("beforecellselect", this, rowIndex, colIndex) !== false){
161 this.clearSelections();
162 r = r || this.grid.store.getAt(rowIndex);
165 cell : [rowIndex, colIndex]
167 if(!preventViewNotify){
168 var v = this.grid.getView();
169 v.onCellSelect(rowIndex, colIndex);
170 if(preventFocus !== true){
171 v.focusCell(rowIndex, colIndex);
174 this.fireEvent("cellselect", this, rowIndex, colIndex);
175 this.fireEvent("selectionchange", this, this.selection);
180 isSelectable : function(rowIndex, colIndex, cm){
181 return !cm.isHidden(colIndex);
185 handleKeyDown : function(e){
186 if(!e.isNavKeyPress()){
189 var g = this.grid, s = this.selection;
192 var cell = g.walkCells(0, 0, 1, this.isSelectable, this);
194 this.select(cell[0], cell[1]);
199 var walk = function(row, col, step){
200 return g.walkCells(row, col, step, sm.isSelectable, sm);
202 var k = e.getKey(), r = s.cell[0], c = s.cell[1];
208 newCell = walk(r, c-1, -1);
210 newCell = walk(r, c+1, 1);
214 newCell = walk(r+1, c, 1);
217 newCell = walk(r-1, c, -1);
220 newCell = walk(r, c+1, 1);
223 newCell = walk(r, c-1, -1);
226 if(g.isEditor && !g.editing){
227 g.startEditing(r, c);
234 this.select(newCell[0], newCell[1]);
239 acceptsNav : function(row, col, cm){
240 return !cm.isHidden(col) && cm.isCellEditable(col, row);
243 onEditorKey : function(field, e){
244 var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
247 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
249 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
252 }else if(k == e.ENTER){
255 }else if(k == e.ESC){
260 g.startEditing(newCell[0], newCell[1]);