3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.grid.RowSelectionModel
9 * @extends Ext.grid.AbstractSelectionModel
10 * The default SelectionModel used by {@link Ext.grid.GridPanel}.
11 * It supports multiple selections and keyboard selection/navigation. The objects stored
12 * as selections and returned by {@link #getSelected}, and {@link #getSelections} are
13 * the {@link Ext.data.Record Record}s which provide the data for the selected rows.
15 * @param {Object} config
17 Ext.grid.RowSelectionModel = function(config){
18 Ext.apply(this, config);
19 this.selections = new Ext.util.MixedCollection(false, function(o){
24 this.lastActive = false;
28 * @event selectionchange
29 * Fires when the selection changes
30 * @param {SelectionModel} this
34 * @event beforerowselect
35 * Fires before a row is selected, return false to cancel the selection.
36 * @param {SelectionModel} this
37 * @param {Number} rowIndex The index to be selected
38 * @param {Boolean} keepExisting False if other selections will be cleared
39 * @param {Record} record The record to be selected
44 * Fires when a row is selected.
45 * @param {SelectionModel} this
46 * @param {Number} rowIndex The selected index
47 * @param {Ext.data.Record} r The selected record
52 * Fires when a row is deselected. To prevent deselection
53 * {@link Ext.grid.AbstractSelectionModel#lock lock the selections}.
54 * @param {SelectionModel} this
55 * @param {Number} rowIndex
56 * @param {Record} record
61 Ext.grid.RowSelectionModel.superclass.constructor.call(this);
64 Ext.extend(Ext.grid.RowSelectionModel, Ext.grid.AbstractSelectionModel, {
66 * @cfg {Boolean} singleSelect
67 * <tt>true</tt> to allow selection of only one row at a time (defaults to <tt>false</tt>
68 * allowing multiple selections)
73 * @cfg {Boolean} moveEditorOnEnter
74 * <tt>false</tt> to turn off moving the editor to the next row down when the enter key is pressed
75 * or the next row up when shift + enter keys are pressed.
78 initEvents : function(){
80 if(!this.grid.enableDragDrop && !this.grid.enableDrag){
81 this.grid.on('rowmousedown', this.handleMouseDown, this);
84 this.rowNav = new Ext.KeyNav(this.grid.getGridEl(), {
86 if(!e.shiftKey || this.singleSelect){
87 this.selectPrevious(false);
88 }else if(this.last !== false && this.lastActive !== false){
90 this.selectRange(this.last, this.lastActive-1);
91 this.grid.getView().focusRow(this.lastActive);
96 this.selectFirstRow();
100 if(!e.shiftKey || this.singleSelect){
101 this.selectNext(false);
102 }else if(this.last !== false && this.lastActive !== false){
103 var last = this.last;
104 this.selectRange(this.last, this.lastActive+1);
105 this.grid.getView().focusRow(this.lastActive);
110 this.selectFirstRow();
116 this.grid.getView().on({
118 refresh: this.onRefresh,
119 rowupdated: this.onRowUpdated,
120 rowremoved: this.onRemove
125 onRefresh : function(){
126 var ds = this.grid.store, index;
127 var s = this.getSelections();
128 this.clearSelections(true);
129 for(var i = 0, len = s.length; i < len; i++){
131 if((index = ds.indexOfId(r.id)) != -1){
132 this.selectRow(index, true);
135 if(s.length != this.selections.getCount()){
136 this.fireEvent('selectionchange', this);
141 onRemove : function(v, index, r){
142 if(this.selections.remove(r) !== false){
143 this.fireEvent('selectionchange', this);
148 onRowUpdated : function(v, index, r){
149 if(this.isSelected(r)){
150 v.onRowSelect(index);
156 * @param {Array} records The records to select
157 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
159 selectRecords : function(records, keepExisting){
161 this.clearSelections();
163 var ds = this.grid.store;
164 for(var i = 0, len = records.length; i < len; i++){
165 this.selectRow(ds.indexOf(records[i]), true);
170 * Gets the number of selected rows.
173 getCount : function(){
174 return this.selections.length;
178 * Selects the first row in the grid.
180 selectFirstRow : function(){
185 * Select the last row.
186 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
188 selectLastRow : function(keepExisting){
189 this.selectRow(this.grid.store.getCount() - 1, keepExisting);
193 * Selects the row immediately following the last selected row.
194 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
195 * @return {Boolean} <tt>true</tt> if there is a next row, else <tt>false</tt>
197 selectNext : function(keepExisting){
199 this.selectRow(this.last+1, keepExisting);
200 this.grid.getView().focusRow(this.last);
207 * Selects the row that precedes the last selected row.
208 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
209 * @return {Boolean} <tt>true</tt> if there is a previous row, else <tt>false</tt>
211 selectPrevious : function(keepExisting){
212 if(this.hasPrevious()){
213 this.selectRow(this.last-1, keepExisting);
214 this.grid.getView().focusRow(this.last);
221 * Returns true if there is a next record to select
224 hasNext : function(){
225 return this.last !== false && (this.last+1) < this.grid.store.getCount();
229 * Returns true if there is a previous record to select
232 hasPrevious : function(){
238 * Returns the selected records
239 * @return {Array} Array of selected records
241 getSelections : function(){
242 return [].concat(this.selections.items);
246 * Returns the first selected record.
249 getSelected : function(){
250 return this.selections.itemAt(0);
254 * Calls the passed function with each selection. If the function returns
255 * <tt>false</tt>, iteration is stopped and this function returns
256 * <tt>false</tt>. Otherwise it returns <tt>true</tt>.
257 * @param {Function} fn
258 * @param {Object} scope (optional)
259 * @return {Boolean} true if all selections were iterated
261 each : function(fn, scope){
262 var s = this.getSelections();
263 for(var i = 0, len = s.length; i < len; i++){
264 if(fn.call(scope || this, s[i], i) === false){
272 * Clears all selections if the selection model
273 * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
274 * @param {Boolean} fast (optional) <tt>true</tt> to bypass the
275 * conditional checks and events described in {@link #deselectRow}.
277 clearSelections : function(fast){
282 var ds = this.grid.store;
283 var s = this.selections;
285 this.deselectRow(ds.indexOfId(r.id));
289 this.selections.clear();
296 * Selects all rows if the selection model
297 * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
299 selectAll : function(){
303 this.selections.clear();
304 for(var i = 0, len = this.grid.store.getCount(); i < len; i++){
305 this.selectRow(i, true);
310 * Returns <tt>true</tt> if there is a selection.
313 hasSelection : function(){
314 return this.selections.length > 0;
318 * Returns <tt>true</tt> if the specified row is selected.
319 * @param {Number/Record} index The record or index of the record to check
322 isSelected : function(index){
323 var r = Ext.isNumber(index) ? this.grid.store.getAt(index) : index;
324 return (r && this.selections.key(r.id) ? true : false);
328 * Returns <tt>true</tt> if the specified record id is selected.
329 * @param {String} id The id of record to check
332 isIdSelected : function(id){
333 return (this.selections.key(id) ? true : false);
337 handleMouseDown : function(g, rowIndex, e){
338 if(e.button !== 0 || this.isLocked()){
341 var view = this.grid.getView();
342 if(e.shiftKey && !this.singleSelect && this.last !== false){
343 var last = this.last;
344 this.selectRange(last, rowIndex, e.ctrlKey);
345 this.last = last; // reset the last
346 view.focusRow(rowIndex);
348 var isSelected = this.isSelected(rowIndex);
349 if(e.ctrlKey && isSelected){
350 this.deselectRow(rowIndex);
351 }else if(!isSelected || this.getCount() > 1){
352 this.selectRow(rowIndex, e.ctrlKey || e.shiftKey);
353 view.focusRow(rowIndex);
359 * Selects multiple rows.
360 * @param {Array} rows Array of the indexes of the row to select
361 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep
362 * existing selections (defaults to <tt>false</tt>)
364 selectRows : function(rows, keepExisting){
366 this.clearSelections();
368 for(var i = 0, len = rows.length; i < len; i++){
369 this.selectRow(rows[i], true);
374 * Selects a range of rows if the selection model
375 * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
376 * All rows in between startRow and endRow are also selected.
377 * @param {Number} startRow The index of the first row in the range
378 * @param {Number} endRow The index of the last row in the range
379 * @param {Boolean} keepExisting (optional) True to retain existing selections
381 selectRange : function(startRow, endRow, keepExisting){
387 this.clearSelections();
389 if(startRow <= endRow){
390 for(i = startRow; i <= endRow; i++){
391 this.selectRow(i, true);
394 for(i = startRow; i >= endRow; i--){
395 this.selectRow(i, true);
401 * Deselects a range of rows if the selection model
402 * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
403 * All rows in between startRow and endRow are also deselected.
404 * @param {Number} startRow The index of the first row in the range
405 * @param {Number} endRow The index of the last row in the range
407 deselectRange : function(startRow, endRow, preventViewNotify){
411 for(var i = startRow; i <= endRow; i++){
412 this.deselectRow(i, preventViewNotify);
417 * Selects a row. Before selecting a row, checks if the selection model
418 * {@link Ext.grid.AbstractSelectionModel#isLocked is locked} and fires the
419 * {@link #beforerowselect} event. If these checks are satisfied the row
420 * will be selected and followed up by firing the {@link #rowselect} and
421 * {@link #selectionchange} events.
422 * @param {Number} row The index of the row to select
423 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
424 * @param {Boolean} preventViewNotify (optional) Specify <tt>true</tt> to
425 * prevent notifying the view (disables updating the selected appearance)
427 selectRow : function(index, keepExisting, preventViewNotify){
428 if(this.isLocked() || (index < 0 || index >= this.grid.store.getCount()) || (keepExisting && this.isSelected(index))){
431 var r = this.grid.store.getAt(index);
432 if(r && this.fireEvent('beforerowselect', this, index, keepExisting, r) !== false){
433 if(!keepExisting || this.singleSelect){
434 this.clearSelections();
436 this.selections.add(r);
437 this.last = this.lastActive = index;
438 if(!preventViewNotify){
439 this.grid.getView().onRowSelect(index);
441 this.fireEvent('rowselect', this, index, r);
442 this.fireEvent('selectionchange', this);
447 * Deselects a row. Before deselecting a row, checks if the selection model
448 * {@link Ext.grid.AbstractSelectionModel#isLocked is locked}.
449 * If this check is satisfied the row will be deselected and followed up by
450 * firing the {@link #rowdeselect} and {@link #selectionchange} events.
451 * @param {Number} row The index of the row to deselect
452 * @param {Boolean} preventViewNotify (optional) Specify <tt>true</tt> to
453 * prevent notifying the view (disables updating the selected appearance)
455 deselectRow : function(index, preventViewNotify){
459 if(this.last == index){
462 if(this.lastActive == index){
463 this.lastActive = false;
465 var r = this.grid.store.getAt(index);
467 this.selections.remove(r);
468 if(!preventViewNotify){
469 this.grid.getView().onRowDeselect(index);
471 this.fireEvent('rowdeselect', this, index, r);
472 this.fireEvent('selectionchange', this);
477 restoreLast : function(){
479 this.last = this._last;
484 acceptsNav : function(row, col, cm){
485 return !cm.isHidden(col) && cm.isCellEditable(col, row);
489 onEditorKey : function(field, e){
496 var shift = e.shiftKey;
501 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
503 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
505 }else if(k == e.ENTER){
506 if(this.moveEditorOnEnter !== false){
508 newCell = g.walkCells(last.row - 1, last.col, -1, this.acceptsNav, this);
510 newCell = g.walkCells(last.row + 1, last.col, 1, this.acceptsNav, this);
519 this.selectRow(r); // *** highlight newly-selected cell and update selection
522 if(g.isEditor && g.editing){ // *** handle tabbing while editorgrid is in edit mode
524 if(ae && ae.field.triggerBlur){
525 // *** if activeEditor is a TriggerField, explicitly call its triggerBlur() method
526 ae.field.triggerBlur();
529 g.startEditing(r, c);
533 destroy : function(){
535 this.rowNav.disable();
538 Ext.grid.RowSelectionModel.superclass.destroy.call(this);