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.RowSelectionModel"></div>/**
9 * @class Ext.grid.RowSelectionModel
10 * @extends Ext.grid.AbstractSelectionModel
11 * The default SelectionModel used by {@link Ext.grid.GridPanel}.
12 * It supports multiple selections and keyboard selection/navigation. The objects stored
13 * as selections and returned by {@link #getSelected}, and {@link #getSelections} are
14 * the {@link Ext.data.Record Record}s which provide the data for the selected rows.
16 * @param {Object} config
18 Ext.grid.RowSelectionModel = function(config){
19 Ext.apply(this, config);
20 this.selections = new Ext.util.MixedCollection(false, function(o){
25 this.lastActive = false;
28 <div id="event-Ext.grid.RowSelectionModel-selectionchange"></div>/**
29 * @event selectionchange
30 * Fires when the selection changes
31 * @param {SelectionModel} this
34 <div id="event-Ext.grid.RowSelectionModel-beforerowselect"></div>/**
35 * @event beforerowselect
36 * Fires before a row is selected, return false to cancel the selection.
37 * @param {SelectionModel} this
38 * @param {Number} rowIndex The index to be selected
39 * @param {Boolean} keepExisting False if other selections will be cleared
40 * @param {Record} record The record to be selected
43 <div id="event-Ext.grid.RowSelectionModel-rowselect"></div>/**
45 * Fires when a row is selected.
46 * @param {SelectionModel} this
47 * @param {Number} rowIndex The selected index
48 * @param {Ext.data.Record} r The selected record
51 <div id="event-Ext.grid.RowSelectionModel-rowdeselect"></div>/**
53 * Fires when a row is deselected. To prevent deselection
54 * {@link Ext.grid.AbstractSelectionModel#lock lock the selections}.
55 * @param {SelectionModel} this
56 * @param {Number} rowIndex
57 * @param {Record} record
62 Ext.grid.RowSelectionModel.superclass.constructor.call(this);
65 Ext.extend(Ext.grid.RowSelectionModel, Ext.grid.AbstractSelectionModel, {
66 <div id="cfg-Ext.grid.RowSelectionModel-singleSelect"></div>/**
67 * @cfg {Boolean} singleSelect
68 * <tt>true</tt> to allow selection of only one row at a time (defaults to <tt>false</tt>
69 * allowing multiple selections)
73 <div id="cfg-Ext.grid.RowSelectionModel-moveEditorOnEnter"></div>/**
74 * @cfg {Boolean} moveEditorOnEnter
75 * <tt>false</tt> to turn off moving the editor to the next row down when the enter key is pressed
76 * or the next row up when shift + enter keys are pressed.
79 initEvents : function(){
81 if(!this.grid.enableDragDrop && !this.grid.enableDrag){
82 this.grid.on("rowmousedown", this.handleMouseDown, this);
85 this.rowNav = new Ext.KeyNav(this.grid.getGridEl(), {
87 if(!e.shiftKey || this.singleSelect){
88 this.selectPrevious(false);
89 }else if(this.last !== false && this.lastActive !== false){
91 this.selectRange(this.last, this.lastActive-1);
92 this.grid.getView().focusRow(this.lastActive);
97 this.selectFirstRow();
100 "down" : function(e){
101 if(!e.shiftKey || this.singleSelect){
102 this.selectNext(false);
103 }else if(this.last !== false && this.lastActive !== false){
104 var last = this.last;
105 this.selectRange(this.last, this.lastActive+1);
106 this.grid.getView().focusRow(this.lastActive);
111 this.selectFirstRow();
117 var view = this.grid.view;
118 view.on("refresh", this.onRefresh, this);
119 view.on("rowupdated", this.onRowUpdated, this);
120 view.on("rowremoved", this.onRemove, this);
124 onRefresh : function(){
125 var ds = this.grid.store, index;
126 var s = this.getSelections();
127 this.clearSelections(true);
128 for(var i = 0, len = s.length; i < len; i++){
130 if((index = ds.indexOfId(r.id)) != -1){
131 this.selectRow(index, true);
134 if(s.length != this.selections.getCount()){
135 this.fireEvent("selectionchange", this);
140 onRemove : function(v, index, r){
141 if(this.selections.remove(r) !== false){
142 this.fireEvent('selectionchange', this);
147 onRowUpdated : function(v, index, r){
148 if(this.isSelected(r)){
149 v.onRowSelect(index);
153 <div id="method-Ext.grid.RowSelectionModel-selectRecords"></div>/**
155 * @param {Array} records The records to select
156 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
158 selectRecords : function(records, keepExisting){
160 this.clearSelections();
162 var ds = this.grid.store;
163 for(var i = 0, len = records.length; i < len; i++){
164 this.selectRow(ds.indexOf(records[i]), true);
168 <div id="method-Ext.grid.RowSelectionModel-getCount"></div>/**
169 * Gets the number of selected rows.
172 getCount : function(){
173 return this.selections.length;
176 <div id="method-Ext.grid.RowSelectionModel-selectFirstRow"></div>/**
177 * Selects the first row in the grid.
179 selectFirstRow : function(){
183 <div id="method-Ext.grid.RowSelectionModel-selectLastRow"></div>/**
184 * Select the last row.
185 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
187 selectLastRow : function(keepExisting){
188 this.selectRow(this.grid.store.getCount() - 1, keepExisting);
191 <div id="method-Ext.grid.RowSelectionModel-selectNext"></div>/**
192 * Selects the row immediately following the last selected row.
193 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
194 * @return {Boolean} <tt>true</tt> if there is a next row, else <tt>false</tt>
196 selectNext : function(keepExisting){
198 this.selectRow(this.last+1, keepExisting);
199 this.grid.getView().focusRow(this.last);
205 <div id="method-Ext.grid.RowSelectionModel-selectPrevious"></div>/**
206 * Selects the row that precedes the last selected row.
207 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
208 * @return {Boolean} <tt>true</tt> if there is a previous row, else <tt>false</tt>
210 selectPrevious : function(keepExisting){
211 if(this.hasPrevious()){
212 this.selectRow(this.last-1, keepExisting);
213 this.grid.getView().focusRow(this.last);
219 <div id="method-Ext.grid.RowSelectionModel-hasNext"></div>/**
220 * Returns true if there is a next record to select
223 hasNext : function(){
224 return this.last !== false && (this.last+1) < this.grid.store.getCount();
227 <div id="method-Ext.grid.RowSelectionModel-hasPrevious"></div>/**
228 * Returns true if there is a previous record to select
231 hasPrevious : function(){
236 <div id="method-Ext.grid.RowSelectionModel-getSelections"></div>/**
237 * Returns the selected records
238 * @return {Array} Array of selected records
240 getSelections : function(){
241 return [].concat(this.selections.items);
244 <div id="method-Ext.grid.RowSelectionModel-getSelected"></div>/**
245 * Returns the first selected record.
248 getSelected : function(){
249 return this.selections.itemAt(0);
252 <div id="method-Ext.grid.RowSelectionModel-each"></div>/**
253 * Calls the passed function with each selection. If the function returns
254 * <tt>false</tt>, iteration is stopped and this function returns
255 * <tt>false</tt>. Otherwise it returns <tt>true</tt>.
256 * @param {Function} fn
257 * @param {Object} scope (optional)
258 * @return {Boolean} true if all selections were iterated
260 each : function(fn, scope){
261 var s = this.getSelections();
262 for(var i = 0, len = s.length; i < len; i++){
263 if(fn.call(scope || this, s[i], i) === false){
270 <div id="method-Ext.grid.RowSelectionModel-clearSelections"></div>/**
271 * Clears all selections if the selection model
272 * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
273 * @param {Boolean} fast (optional) <tt>true</tt> to bypass the
274 * conditional checks and events described in {@link #deselectRow}.
276 clearSelections : function(fast){
281 var ds = this.grid.store;
282 var s = this.selections;
284 this.deselectRow(ds.indexOfId(r.id));
288 this.selections.clear();
294 <div id="method-Ext.grid.RowSelectionModel-selectAll"></div>/**
295 * Selects all rows if the selection model
296 * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
298 selectAll : function(){
302 this.selections.clear();
303 for(var i = 0, len = this.grid.store.getCount(); i < len; i++){
304 this.selectRow(i, true);
308 <div id="method-Ext.grid.RowSelectionModel-hasSelection"></div>/**
309 * Returns <tt>true</tt> if there is a selection.
312 hasSelection : function(){
313 return this.selections.length > 0;
316 <div id="method-Ext.grid.RowSelectionModel-isSelected"></div>/**
317 * Returns <tt>true</tt> if the specified row is selected.
318 * @param {Number/Record} index The record or index of the record to check
321 isSelected : function(index){
322 var r = Ext.isNumber(index) ? this.grid.store.getAt(index) : index;
323 return (r && this.selections.key(r.id) ? true : false);
326 <div id="method-Ext.grid.RowSelectionModel-isIdSelected"></div>/**
327 * Returns <tt>true</tt> if the specified record id is selected.
328 * @param {String} id The id of record to check
331 isIdSelected : function(id){
332 return (this.selections.key(id) ? true : false);
336 handleMouseDown : function(g, rowIndex, e){
337 if(e.button !== 0 || this.isLocked()){
340 var view = this.grid.getView();
341 if(e.shiftKey && !this.singleSelect && this.last !== false){
342 var last = this.last;
343 this.selectRange(last, rowIndex, e.ctrlKey);
344 this.last = last; // reset the last
345 view.focusRow(rowIndex);
347 var isSelected = this.isSelected(rowIndex);
348 if(e.ctrlKey && isSelected){
349 this.deselectRow(rowIndex);
350 }else if(!isSelected || this.getCount() > 1){
351 this.selectRow(rowIndex, e.ctrlKey || e.shiftKey);
352 view.focusRow(rowIndex);
357 <div id="method-Ext.grid.RowSelectionModel-selectRows"></div>/**
358 * Selects multiple rows.
359 * @param {Array} rows Array of the indexes of the row to select
360 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep
361 * existing selections (defaults to <tt>false</tt>)
363 selectRows : function(rows, keepExisting){
365 this.clearSelections();
367 for(var i = 0, len = rows.length; i < len; i++){
368 this.selectRow(rows[i], true);
372 <div id="method-Ext.grid.RowSelectionModel-selectRange"></div>/**
373 * Selects a range of rows if the selection model
374 * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
375 * All rows in between startRow and endRow are also selected.
376 * @param {Number} startRow The index of the first row in the range
377 * @param {Number} endRow The index of the last row in the range
378 * @param {Boolean} keepExisting (optional) True to retain existing selections
380 selectRange : function(startRow, endRow, keepExisting){
386 this.clearSelections();
388 if(startRow <= endRow){
389 for(i = startRow; i <= endRow; i++){
390 this.selectRow(i, true);
393 for(i = startRow; i >= endRow; i--){
394 this.selectRow(i, true);
399 <div id="method-Ext.grid.RowSelectionModel-deselectRange"></div>/**
400 * Deselects a range of rows if the selection model
401 * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
402 * All rows in between startRow and endRow are also deselected.
403 * @param {Number} startRow The index of the first row in the range
404 * @param {Number} endRow The index of the last row in the range
406 deselectRange : function(startRow, endRow, preventViewNotify){
410 for(var i = startRow; i <= endRow; i++){
411 this.deselectRow(i, preventViewNotify);
415 <div id="method-Ext.grid.RowSelectionModel-selectRow"></div>/**
416 * Selects a row. Before selecting a row, checks if the selection model
417 * {@link Ext.grid.AbstractSelectionModel#isLocked is locked} and fires the
418 * {@link #beforerowselect} event. If these checks are satisfied the row
419 * will be selected and followed up by firing the {@link #rowselect} and
420 * {@link #selectionchange} events.
421 * @param {Number} row The index of the row to select
422 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
423 * @param {Boolean} preventViewNotify (optional) Specify <tt>true</tt> to
424 * prevent notifying the view (disables updating the selected appearance)
426 selectRow : function(index, keepExisting, preventViewNotify){
427 if(this.isLocked() || (index < 0 || index >= this.grid.store.getCount()) || (keepExisting && this.isSelected(index))){
430 var r = this.grid.store.getAt(index);
431 if(r && this.fireEvent("beforerowselect", this, index, keepExisting, r) !== false){
432 if(!keepExisting || this.singleSelect){
433 this.clearSelections();
435 this.selections.add(r);
436 this.last = this.lastActive = index;
437 if(!preventViewNotify){
438 this.grid.getView().onRowSelect(index);
440 this.fireEvent("rowselect", this, index, r);
441 this.fireEvent("selectionchange", this);
445 <div id="method-Ext.grid.RowSelectionModel-deselectRow"></div>/**
446 * Deselects a row. Before deselecting a row, checks if the selection model
447 * {@link Ext.grid.AbstractSelectionModel#isLocked is locked}.
448 * If this check is satisfied the row will be deselected and followed up by
449 * firing the {@link #rowdeselect} and {@link #selectionchange} events.
450 * @param {Number} row The index of the row to deselect
451 * @param {Boolean} preventViewNotify (optional) Specify <tt>true</tt> to
452 * prevent notifying the view (disables updating the selected appearance)
454 deselectRow : function(index, preventViewNotify){
458 if(this.last == index){
461 if(this.lastActive == index){
462 this.lastActive = false;
464 var r = this.grid.store.getAt(index);
466 this.selections.remove(r);
467 if(!preventViewNotify){
468 this.grid.getView().onRowDeselect(index);
470 this.fireEvent("rowdeselect", this, index, r);
471 this.fireEvent("selectionchange", this);
476 restoreLast : function(){
478 this.last = this._last;
483 acceptsNav : function(row, col, cm){
484 return !cm.isHidden(col) && cm.isCellEditable(col, row);
488 onEditorKey : function(field, e){
489 var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
490 var shift = e.shiftKey;
495 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
497 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
499 }else if(k == e.ENTER){
502 if(this.moveEditorOnEnter !== false){
504 newCell = g.walkCells(ed.row - 1, ed.col, -1, this.acceptsNav, this);
506 newCell = g.walkCells(ed.row + 1, ed.col, 1, this.acceptsNav, this);
509 }else if(k == e.ESC){
513 g.startEditing(newCell[0], newCell[1]);
519 this.rowNav.disable();
522 Ext.grid.RowSelectionModel.superclass.destroy.call(this);