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()
72 if(!this.grid.enableDragDrop && !this.grid.enableDrag){
73 this.grid.on("mousedown", this.handleMouseDown, this);
74 }else{ // allow click to work like normal
75 this.grid.on("rowclick", this.handleDragableRowClick, this);
78 this.rowNav = new Roo.KeyNav(this.grid.getGridEl(), {
81 this.selectPrevious(e.shiftKey);
82 }else if(this.last !== false && this.lastActive !== false){
84 this.selectRange(this.last, this.lastActive-1);
85 this.grid.getView().focusRow(this.lastActive);
90 this.selectFirstRow();
92 this.fireEvent("afterselectionchange", this);
96 this.selectNext(e.shiftKey);
97 }else if(this.last !== false && this.lastActive !== false){
99 this.selectRange(this.last, this.lastActive+1);
100 this.grid.getView().focusRow(this.lastActive);
105 this.selectFirstRow();
107 this.fireEvent("afterselectionchange", this);
112 var view = this.grid.view;
113 view.on("refresh", this.onRefresh, this);
114 view.on("rowupdated", this.onRowUpdated, this);
115 view.on("rowremoved", this.onRemove, this);
119 onRefresh : function(){
120 var ds = this.grid.dataSource, i, v = this.grid.view;
121 var s = this.selections;
123 if((i = ds.indexOfId(r.id)) != -1){
132 onRemove : function(v, index, r){
133 this.selections.remove(r);
137 onRowUpdated : function(v, index, r){
138 if(this.isSelected(r)){
139 v.onRowSelect(index);
145 * @param {Array} records The records to select
146 * @param {Boolean} keepExisting (optional) True to keep existing selections
148 selectRecords : function(records, keepExisting){
150 this.clearSelections();
152 var ds = this.grid.dataSource;
153 for(var i = 0, len = records.length; i < len; i++){
154 this.selectRow(ds.indexOf(records[i]), true);
159 * Gets the number of selected rows.
162 getCount : function(){
163 return this.selections.length;
167 * Selects the first row in the grid.
169 selectFirstRow : function(){
174 * Select the last row.
175 * @param {Boolean} keepExisting (optional) True to keep existing selections
177 selectLastRow : function(keepExisting){
178 this.selectRow(this.grid.dataSource.getCount() - 1, keepExisting);
182 * Selects the row immediately following the last selected row.
183 * @param {Boolean} keepExisting (optional) True to keep existing selections
185 selectNext : function(keepExisting){
186 if(this.last !== false && (this.last+1) < this.grid.dataSource.getCount()){
187 this.selectRow(this.last+1, keepExisting);
188 this.grid.getView().focusRow(this.last);
193 * Selects the row that precedes the last selected row.
194 * @param {Boolean} keepExisting (optional) True to keep existing selections
196 selectPrevious : function(keepExisting){
198 this.selectRow(this.last-1, keepExisting);
199 this.grid.getView().focusRow(this.last);
204 * Returns the selected records
205 * @return {Array} Array of selected records
207 getSelections : function(){
208 return [].concat(this.selections.items);
212 * Returns the first selected record.
215 getSelected : function(){
216 return this.selections.itemAt(0);
221 * Clears all selections.
223 clearSelections : function(fast){
228 var ds = this.grid.dataSource;
229 var s = this.selections;
231 this.deselectRow(ds.indexOfId(r.id));
235 this.selections.clear();
244 selectAll : function(){
248 this.selections.clear();
249 for(var i = 0, len = this.grid.dataSource.getCount(); i < len; i++){
250 this.selectRow(i, true);
255 * Returns True if there is a selection.
258 hasSelection : function(){
259 return this.selections.length > 0;
263 * Returns True if the specified row is selected.
264 * @param {Number/Record} record The record or index of the record to check
267 isSelected : function(index){
268 var r = typeof index == "number" ? this.grid.dataSource.getAt(index) : index;
269 return (r && this.selections.key(r.id) ? true : false);
273 * Returns True if the specified record id is selected.
274 * @param {String} id The id of record to check
277 isIdSelected : function(id){
278 return (this.selections.key(id) ? true : false);
282 handleMouseDown : function(e, t){
283 var view = this.grid.getView(), rowIndex;
284 if(this.isLocked() || (rowIndex = view.findRowIndex(t)) === false){
287 if(e.shiftKey && this.last !== false){
288 var last = this.last;
289 this.selectRange(last, rowIndex, e.ctrlKey);
290 this.last = last; // reset the last
291 view.focusRow(rowIndex);
293 var isSelected = this.isSelected(rowIndex);
294 if(e.button !== 0 && isSelected){
295 view.focusRow(rowIndex);
296 }else if(e.ctrlKey && isSelected){
297 this.deselectRow(rowIndex);
298 }else if(!isSelected){
299 this.selectRow(rowIndex, e.button === 0 && (e.ctrlKey || e.shiftKey));
300 view.focusRow(rowIndex);
303 this.fireEvent("afterselectionchange", this);
306 handleDragableRowClick : function(grid, rowIndex, e)
308 if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
309 this.selectRow(rowIndex, false);
310 grid.view.focusRow(rowIndex);
311 this.fireEvent("afterselectionchange", this);
316 * Selects multiple rows.
317 * @param {Array} rows Array of the indexes of the row to select
318 * @param {Boolean} keepExisting (optional) True to keep existing selections
320 selectRows : function(rows, keepExisting){
322 this.clearSelections();
324 for(var i = 0, len = rows.length; i < len; i++){
325 this.selectRow(rows[i], true);
330 * Selects a range of rows. All rows in between startRow and endRow are also selected.
331 * @param {Number} startRow The index of the first row in the range
332 * @param {Number} endRow The index of the last row in the range
333 * @param {Boolean} keepExisting (optional) True to retain existing selections
335 selectRange : function(startRow, endRow, keepExisting){
340 this.clearSelections();
342 if(startRow <= endRow){
343 for(var i = startRow; i <= endRow; i++){
344 this.selectRow(i, true);
347 for(var i = startRow; i >= endRow; i--){
348 this.selectRow(i, true);
354 * Deselects a range of rows. All rows in between startRow and endRow are also deselected.
355 * @param {Number} startRow The index of the first row in the range
356 * @param {Number} endRow The index of the last row in the range
358 deselectRange : function(startRow, endRow, preventViewNotify){
362 for(var i = startRow; i <= endRow; i++){
363 this.deselectRow(i, preventViewNotify);
369 * @param {Number} row The index of the row to select
370 * @param {Boolean} keepExisting (optional) True to keep existing selections
372 selectRow : function(index, keepExisting, preventViewNotify){
373 if(this.locked || (index < 0 || index >= this.grid.dataSource.getCount())) {
376 if(this.fireEvent("beforerowselect", this, index, keepExisting) !== false){
377 if(!keepExisting || this.singleSelect){
378 this.clearSelections();
380 var r = this.grid.dataSource.getAt(index);
381 this.selections.add(r);
382 this.last = this.lastActive = index;
383 if(!preventViewNotify){
384 this.grid.getView().onRowSelect(index);
386 this.fireEvent("rowselect", this, index, r);
387 this.fireEvent("selectionchange", this);
393 * @param {Number} row The index of the row to deselect
395 deselectRow : function(index, preventViewNotify){
399 if(this.last == index){
402 if(this.lastActive == index){
403 this.lastActive = false;
405 var r = this.grid.dataSource.getAt(index);
406 this.selections.remove(r);
407 if(!preventViewNotify){
408 this.grid.getView().onRowDeselect(index);
410 this.fireEvent("rowdeselect", this, index);
411 this.fireEvent("selectionchange", this);
415 restoreLast : function(){
417 this.last = this._last;
422 acceptsNav : function(row, col, cm){
423 return !cm.isHidden(col) && cm.isCellEditable(col, row);
427 onEditorKey : function(field, e){
428 var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
433 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
435 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
437 }else if(k == e.ENTER && !e.ctrlKey){
441 newCell = g.walkCells(ed.row-1, ed.col, -1, this.acceptsNav, this);
443 newCell = g.walkCells(ed.row+1, ed.col, 1, this.acceptsNav, this);
445 }else if(k == e.ESC){
449 g.startEditing(newCell[0], newCell[1]);