3 * @extends Roo.bootstrap.Table.AbstractSelectionModel
4 * @class Roo.bootstrap.Table.RowSelectionModel
5 * The default SelectionModel used by {@link Roo.bootstrap.Table}.
6 * It supports multiple selections and keyboard selection/navigation.
8 * @param {Object} config
11 Roo.bootstrap.Table.RowSelectionModel = function(config){
12 Roo.apply(this, config);
13 this.selections = new Roo.util.MixedCollection(false, function(o){
18 this.lastActive = false;
22 * @event selectionchange
23 * Fires when the selection changes
24 * @param {SelectionModel} this
26 "selectionchange" : true,
28 * @event afterselectionchange
29 * Fires after the selection changes (eg. by key press or clicking)
30 * @param {SelectionModel} this
32 "afterselectionchange" : true,
34 * @event beforerowselect
35 * Fires when a row is selected being selected, return false to cancel.
36 * @param {SelectionModel} this
37 * @param {Number} rowIndex The selected index
38 * @param {Boolean} keepExisting False if other selections will be cleared
40 "beforerowselect" : true,
43 * Fires when a row is selected.
44 * @param {SelectionModel} this
45 * @param {Number} rowIndex The selected index
46 * @param {Roo.data.Record} r The record
51 * Fires when a row is deselected.
52 * @param {SelectionModel} this
53 * @param {Number} rowIndex The selected index
57 Roo.bootstrap.Table.RowSelectionModel.superclass.constructor.call(this);
61 Roo.extend(Roo.bootstrap.Table.RowSelectionModel, Roo.bootstrap.Table.AbstractSelectionModel, {
63 * @cfg {Boolean} singleSelect
64 * True to allow selection of only one row at a time (defaults to false)
69 initEvents : function(){
71 if(!this.grid.enableDragDrop && !this.grid.enableDrag){
72 this.grid.on("mousedown", this.handleMouseDown, this);
73 }else{ // allow click to work like normal
74 this.grid.on("rowclick", this.handleDragableRowClick, this);
77 this.rowNav = new Roo.KeyNav(this.grid.getGridEl(), {
80 this.selectPrevious(e.shiftKey);
81 }else if(this.last !== false && this.lastActive !== false){
83 this.selectRange(this.last, this.lastActive-1);
84 this.grid.getView().focusRow(this.lastActive);
89 this.selectFirstRow();
91 this.fireEvent("afterselectionchange", this);
95 this.selectNext(e.shiftKey);
96 }else if(this.last !== false && this.lastActive !== false){
98 this.selectRange(this.last, this.lastActive+1);
99 this.grid.getView().focusRow(this.lastActive);
104 this.selectFirstRow();
106 this.fireEvent("afterselectionchange", this);
111 var view = this.grid.view;
112 view.on("refresh", this.onRefresh, this);
113 view.on("rowupdated", this.onRowUpdated, this);
114 view.on("rowremoved", this.onRemove, this);
118 onRefresh : function(){
119 var ds = this.grid.dataSource, i, v = this.grid.view;
120 var s = this.selections;
122 if((i = ds.indexOfId(r.id)) != -1){
131 onRemove : function(v, index, r){
132 this.selections.remove(r);
136 onRowUpdated : function(v, index, r){
137 if(this.isSelected(r)){
138 v.onRowSelect(index);
144 * @param {Array} records The records to select
145 * @param {Boolean} keepExisting (optional) True to keep existing selections
147 selectRecords : function(records, keepExisting){
149 this.clearSelections();
151 var ds = this.grid.dataSource;
152 for(var i = 0, len = records.length; i < len; i++){
153 this.selectRow(ds.indexOf(records[i]), true);
158 * Gets the number of selected rows.
161 getCount : function(){
162 return this.selections.length;
166 * Selects the first row in the grid.
168 selectFirstRow : function(){
173 * Select the last row.
174 * @param {Boolean} keepExisting (optional) True to keep existing selections
176 selectLastRow : function(keepExisting){
177 this.selectRow(this.grid.dataSource.getCount() - 1, keepExisting);
181 * Selects the row immediately following the last selected row.
182 * @param {Boolean} keepExisting (optional) True to keep existing selections
184 selectNext : function(keepExisting){
185 if(this.last !== false && (this.last+1) < this.grid.dataSource.getCount()){
186 this.selectRow(this.last+1, keepExisting);
187 this.grid.getView().focusRow(this.last);
192 * Selects the row that precedes the last selected row.
193 * @param {Boolean} keepExisting (optional) True to keep existing selections
195 selectPrevious : function(keepExisting){
197 this.selectRow(this.last-1, keepExisting);
198 this.grid.getView().focusRow(this.last);
203 * Returns the selected records
204 * @return {Array} Array of selected records
206 getSelections : function(){
207 return [].concat(this.selections.items);
211 * Returns the first selected record.
214 getSelected : function(){
215 return this.selections.itemAt(0);
220 * Clears all selections.
222 clearSelections : function(fast){
227 var ds = this.grid.dataSource;
228 var s = this.selections;
230 this.deselectRow(ds.indexOfId(r.id));
234 this.selections.clear();
243 selectAll : function(){
247 this.selections.clear();
248 for(var i = 0, len = this.grid.dataSource.getCount(); i < len; i++){
249 this.selectRow(i, true);
254 * Returns True if there is a selection.
257 hasSelection : function(){
258 return this.selections.length > 0;
262 * Returns True if the specified row is selected.
263 * @param {Number/Record} record The record or index of the record to check
266 isSelected : function(index){
267 var r = typeof index == "number" ? this.grid.dataSource.getAt(index) : index;
268 return (r && this.selections.key(r.id) ? true : false);
272 * Returns True if the specified record id is selected.
273 * @param {String} id The id of record to check
276 isIdSelected : function(id){
277 return (this.selections.key(id) ? true : false);
281 handleMouseDown : function(e, t){
282 var view = this.grid.getView(), rowIndex;
283 if(this.isLocked() || (rowIndex = view.findRowIndex(t)) === false){
286 if(e.shiftKey && this.last !== false){
287 var last = this.last;
288 this.selectRange(last, rowIndex, e.ctrlKey);
289 this.last = last; // reset the last
290 view.focusRow(rowIndex);
292 var isSelected = this.isSelected(rowIndex);
293 if(e.button !== 0 && isSelected){
294 view.focusRow(rowIndex);
295 }else if(e.ctrlKey && isSelected){
296 this.deselectRow(rowIndex);
297 }else if(!isSelected){
298 this.selectRow(rowIndex, e.button === 0 && (e.ctrlKey || e.shiftKey));
299 view.focusRow(rowIndex);
302 this.fireEvent("afterselectionchange", this);
305 handleDragableRowClick : function(grid, rowIndex, e)
307 if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
308 this.selectRow(rowIndex, false);
309 grid.view.focusRow(rowIndex);
310 this.fireEvent("afterselectionchange", this);
315 * Selects multiple rows.
316 * @param {Array} rows Array of the indexes of the row to select
317 * @param {Boolean} keepExisting (optional) True to keep existing selections
319 selectRows : function(rows, keepExisting){
321 this.clearSelections();
323 for(var i = 0, len = rows.length; i < len; i++){
324 this.selectRow(rows[i], true);
329 * Selects a range of rows. All rows in between startRow and endRow are also selected.
330 * @param {Number} startRow The index of the first row in the range
331 * @param {Number} endRow The index of the last row in the range
332 * @param {Boolean} keepExisting (optional) True to retain existing selections
334 selectRange : function(startRow, endRow, keepExisting){
339 this.clearSelections();
341 if(startRow <= endRow){
342 for(var i = startRow; i <= endRow; i++){
343 this.selectRow(i, true);
346 for(var i = startRow; i >= endRow; i--){
347 this.selectRow(i, true);
353 * Deselects a range of rows. All rows in between startRow and endRow are also deselected.
354 * @param {Number} startRow The index of the first row in the range
355 * @param {Number} endRow The index of the last row in the range
357 deselectRange : function(startRow, endRow, preventViewNotify){
361 for(var i = startRow; i <= endRow; i++){
362 this.deselectRow(i, preventViewNotify);
368 * @param {Number} row The index of the row to select
369 * @param {Boolean} keepExisting (optional) True to keep existing selections
371 selectRow : function(index, keepExisting, preventViewNotify){
372 if(this.locked || (index < 0 || index >= this.grid.dataSource.getCount())) return;
373 if(this.fireEvent("beforerowselect", this, index, keepExisting) !== false){
374 if(!keepExisting || this.singleSelect){
375 this.clearSelections();
377 var r = this.grid.dataSource.getAt(index);
378 this.selections.add(r);
379 this.last = this.lastActive = index;
380 if(!preventViewNotify){
381 this.grid.getView().onRowSelect(index);
383 this.fireEvent("rowselect", this, index, r);
384 this.fireEvent("selectionchange", this);
390 * @param {Number} row The index of the row to deselect
392 deselectRow : function(index, preventViewNotify){
396 if(this.last == index){
399 if(this.lastActive == index){
400 this.lastActive = false;
402 var r = this.grid.dataSource.getAt(index);
403 this.selections.remove(r);
404 if(!preventViewNotify){
405 this.grid.getView().onRowDeselect(index);
407 this.fireEvent("rowdeselect", this, index);
408 this.fireEvent("selectionchange", this);
412 restoreLast : function(){
414 this.last = this._last;
419 acceptsNav : function(row, col, cm){
420 return !cm.isHidden(col) && cm.isCellEditable(col, row);
424 onEditorKey : function(field, e){
425 var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
430 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
432 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
434 }else if(k == e.ENTER && !e.ctrlKey){
438 newCell = g.walkCells(ed.row-1, ed.col, -1, this.acceptsNav, this);
440 newCell = g.walkCells(ed.row+1, ed.col, 1, this.acceptsNav, this);
442 }else if(k == e.ESC){
446 g.startEditing(newCell[0], newCell[1]);