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.growclickrid.on("mousedown", this.handleMouseDown, this);
74 //}else{ // allow click to work like normal
75 // this.grid.on("rowclick", this.handleDragableRowClick, this);
77 this.grid.on("mousedown", this.handleMouseDown, this);
79 this.rowNav = new Roo.KeyNav(this.grid.getGridEl(), {
82 this.selectPrevious(e.shiftKey);
83 }else if(this.last !== false && this.lastActive !== false){
85 this.selectRange(this.last, this.lastActive-1);
86 this.grid.getView().focusRow(this.lastActive);
91 this.selectFirstRow();
93 this.fireEvent("afterselectionchange", this);
97 this.selectNext(e.shiftKey);
98 }else if(this.last !== false && this.lastActive !== false){
100 this.selectRange(this.last, this.lastActive+1);
101 this.grid.getView().focusRow(this.lastActive);
106 this.selectFirstRow();
108 this.fireEvent("afterselectionchange", this);
113 var view = this.grid.view;
114 view.on("refresh", this.onRefresh, this);
115 view.on("rowupdated", this.onRowUpdated, this);
116 view.on("rowremoved", this.onRemove, this);
121 onRefresh : function(){
122 var ds = this.grid.dataSource, i, v = this.grid.view;
123 var s = this.selections;
125 if((i = ds.indexOfId(r.id)) != -1){
134 onRemove : function(v, index, r){
135 this.selections.remove(r);
139 onRowUpdated : function(v, index, r){
140 if(this.isSelected(r)){
141 v.onRowSelect(index);
147 * @param {Array} records The records to select
148 * @param {Boolean} keepExisting (optional) True to keep existing selections
150 selectRecords : function(records, keepExisting){
152 this.clearSelections();
154 var ds = this.grid.dataSource;
155 for(var i = 0, len = records.length; i < len; i++){
156 this.selectRow(ds.indexOf(records[i]), true);
161 * Gets the number of selected rows.
164 getCount : function(){
165 return this.selections.length;
169 * Selects the first row in the grid.
171 selectFirstRow : function(){
176 * Select the last row.
177 * @param {Boolean} keepExisting (optional) True to keep existing selections
179 selectLastRow : function(keepExisting){
180 this.selectRow(this.grid.dataSource.getCount() - 1, keepExisting);
184 * Selects the row immediately following the last selected row.
185 * @param {Boolean} keepExisting (optional) True to keep existing selections
187 selectNext : function(keepExisting){
188 if(this.last !== false && (this.last+1) < this.grid.dataSource.getCount()){
189 this.selectRow(this.last+1, keepExisting);
190 this.grid.getView().focusRow(this.last);
195 * Selects the row that precedes the last selected row.
196 * @param {Boolean} keepExisting (optional) True to keep existing selections
198 selectPrevious : function(keepExisting){
200 this.selectRow(this.last-1, keepExisting);
201 this.grid.getView().focusRow(this.last);
206 * Returns the selected records
207 * @return {Array} Array of selected records
209 getSelections : function(){
210 return [].concat(this.selections.items);
214 * Returns the first selected record.
217 getSelected : function(){
218 return this.selections.itemAt(0);
223 * Clears all selections.
225 clearSelections : function(fast){
230 var ds = this.grid.dataSource;
231 var s = this.selections;
233 this.deselectRow(ds.indexOfId(r.id));
237 this.selections.clear();
246 selectAll : function(){
250 this.selections.clear();
251 for(var i = 0, len = this.grid.dataSource.getCount(); i < len; i++){
252 this.selectRow(i, true);
257 * Returns True if there is a selection.
260 hasSelection : function(){
261 return this.selections.length > 0;
265 * Returns True if the specified row is selected.
266 * @param {Number/Record} record The record or index of the record to check
269 isSelected : function(index){
270 var r = typeof index == "number" ? this.grid.dataSource.getAt(index) : index;
271 return (r && this.selections.key(r.id) ? true : false);
275 * Returns True if the specified record id is selected.
276 * @param {String} id The id of record to check
279 isIdSelected : function(id){
280 return (this.selections.key(id) ? true : false);
284 handleMouseDown : function(e, t){
285 var view = this.grid.getView(), rowIndex;
286 if(this.isLocked() || (rowIndex = view.findRowIndex(t)) === false){
289 if(e.shiftKey && this.last !== false){
290 var last = this.last;
291 this.selectRange(last, rowIndex, e.ctrlKey);
292 this.last = last; // reset the last
293 view.focusRow(rowIndex);
295 var isSelected = this.isSelected(rowIndex);
296 if(e.button !== 0 && isSelected){
297 view.focusRow(rowIndex);
298 }else if(e.ctrlKey && isSelected){
299 this.deselectRow(rowIndex);
300 }else if(!isSelected){
301 this.selectRow(rowIndex, e.button === 0 && (e.ctrlKey || e.shiftKey));
302 view.focusRow(rowIndex);
305 this.fireEvent("afterselectionchange", this);
308 handleDragableRowClick : function(grid, rowIndex, e)
310 if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
311 this.selectRow(rowIndex, false);
312 grid.view.focusRow(rowIndex);
313 this.fireEvent("afterselectionchange", this);
318 * Selects multiple rows.
319 * @param {Array} rows Array of the indexes of the row to select
320 * @param {Boolean} keepExisting (optional) True to keep existing selections
322 selectRows : function(rows, keepExisting){
324 this.clearSelections();
326 for(var i = 0, len = rows.length; i < len; i++){
327 this.selectRow(rows[i], true);
332 * Selects a range of rows. All rows in between startRow and endRow are also selected.
333 * @param {Number} startRow The index of the first row in the range
334 * @param {Number} endRow The index of the last row in the range
335 * @param {Boolean} keepExisting (optional) True to retain existing selections
337 selectRange : function(startRow, endRow, keepExisting){
342 this.clearSelections();
344 if(startRow <= endRow){
345 for(var i = startRow; i <= endRow; i++){
346 this.selectRow(i, true);
349 for(var i = startRow; i >= endRow; i--){
350 this.selectRow(i, true);
356 * Deselects a range of rows. All rows in between startRow and endRow are also deselected.
357 * @param {Number} startRow The index of the first row in the range
358 * @param {Number} endRow The index of the last row in the range
360 deselectRange : function(startRow, endRow, preventViewNotify){
364 for(var i = startRow; i <= endRow; i++){
365 this.deselectRow(i, preventViewNotify);
371 * @param {Number} row The index of the row to select
372 * @param {Boolean} keepExisting (optional) True to keep existing selections
374 selectRow : function(index, keepExisting, preventViewNotify){
375 if(this.locked || (index < 0 || index >= this.grid.dataSource.getCount())) {
378 if(this.fireEvent("beforerowselect", this, index, keepExisting) !== false){
379 if(!keepExisting || this.singleSelect){
380 this.clearSelections();
382 var r = this.grid.dataSource.getAt(index);
383 this.selections.add(r);
384 this.last = this.lastActive = index;
385 if(!preventViewNotify){
386 this.grid.getView().onRowSelect(index);
388 this.fireEvent("rowselect", this, index, r);
389 this.fireEvent("selectionchange", this);
395 * @param {Number} row The index of the row to deselect
397 deselectRow : function(index, preventViewNotify){
401 if(this.last == index){
404 if(this.lastActive == index){
405 this.lastActive = false;
407 var r = this.grid.dataSource.getAt(index);
408 this.selections.remove(r);
409 if(!preventViewNotify){
410 this.grid.getView().onRowDeselect(index);
412 this.fireEvent("rowdeselect", this, index);
413 this.fireEvent("selectionchange", this);
417 restoreLast : function(){
419 this.last = this._last;
424 acceptsNav : function(row, col, cm){
425 return !cm.isHidden(col) && cm.isCellEditable(col, row);
429 onEditorKey : function(field, e){
430 var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
435 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
437 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
439 }else if(k == e.ENTER && !e.ctrlKey){
443 newCell = g.walkCells(ed.row-1, ed.col, -1, this.acceptsNav, this);
445 newCell = g.walkCells(ed.row+1, ed.col, 1, this.acceptsNav, this);
447 }else if(k == e.ESC){
451 g.startEditing(newCell[0], newCell[1]);