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("rowdblclick", this.handleMouseDBClick, this);
78 this.grid.on("rowclick", this.handleMouseDown, this);
80 this.rowNav = new Roo.KeyNav(this.grid.getGridEl(), {
83 this.selectPrevious(e.shiftKey);
84 }else if(this.last !== false && this.lastActive !== false){
86 this.selectRange(this.last, this.lastActive-1);
87 this.grid.getView().focusRow(this.lastActive);
92 this.selectFirstRow();
94 this.fireEvent("afterselectionchange", this);
98 this.selectNext(e.shiftKey);
99 }else if(this.last !== false && this.lastActive !== false){
100 var last = this.last;
101 this.selectRange(this.last, this.lastActive+1);
102 this.grid.getView().focusRow(this.lastActive);
107 this.selectFirstRow();
109 this.fireEvent("afterselectionchange", this);
113 this.grid.store.on('load', function(){
114 this.selections.clear();
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);
125 onRefresh : function()
127 var ds = this.grid.store, i, v = this.grid.view;
128 var s = this.selections;
130 if((i = ds.indexOfId(r.id)) != -1){
139 onRemove : function(v, index, r){
140 this.selections.remove(r);
144 onRowUpdated : function(v, index, r){
145 if(this.isSelected(r)){
146 v.onRowSelect(index);
152 * @param {Array} records The records to select
153 * @param {Boolean} keepExisting (optional) True to keep existing selections
155 selectRecords : function(records, keepExisting)
158 this.clearSelections();
160 var ds = this.grid.store;
161 for(var i = 0, len = records.length; i < len; i++){
162 this.selectRow(ds.indexOf(records[i]), true);
167 * Gets the number of selected rows.
170 getCount : function(){
171 return this.selections.length;
175 * Selects the first row in the grid.
177 selectFirstRow : function(){
182 * Select the last row.
183 * @param {Boolean} keepExisting (optional) True to keep existing selections
185 selectLastRow : function(keepExisting){
186 //this.selectRow(this.grid.dataSource.getCount() - 1, keepExisting);
187 this.selectRow(this.grid.store.getCount() - 1, keepExisting);
191 * Selects the row immediately following the last selected row.
192 * @param {Boolean} keepExisting (optional) True to keep existing selections
194 selectNext : function(keepExisting)
196 if(this.last !== false && (this.last+1) < this.grid.store.getCount()){
197 this.selectRow(this.last+1, keepExisting);
198 this.grid.getView().focusRow(this.last);
203 * Selects the row that precedes the last selected row.
204 * @param {Boolean} keepExisting (optional) True to keep existing selections
206 selectPrevious : function(keepExisting){
208 this.selectRow(this.last-1, keepExisting);
209 this.grid.getView().focusRow(this.last);
214 * Returns the selected records
215 * @return {Array} Array of selected records
217 getSelections : function(){
218 return [].concat(this.selections.items);
222 * Returns the first selected record.
225 getSelected : function(){
226 return this.selections.itemAt(0);
231 * Clears all selections.
233 clearSelections : function(fast)
239 var ds = this.grid.store;
240 var s = this.selections;
242 this.deselectRow(ds.indexOfId(r.id));
246 this.selections.clear();
255 selectAll : function(){
259 this.selections.clear();
260 for(var i = 0, len = this.grid.store.getCount(); i < len; i++){
261 this.selectRow(i, true);
266 * Returns True if there is a selection.
269 hasSelection : function(){
270 return this.selections.length > 0;
274 * Returns True if the specified row is selected.
275 * @param {Number/Record} record The record or index of the record to check
278 isSelected : function(index){
279 var r = typeof index == "number" ? this.grid.store.getAt(index) : index;
280 return (r && this.selections.key(r.id) ? true : false);
284 * Returns True if the specified record id is selected.
285 * @param {String} id The id of record to check
288 isIdSelected : function(id){
289 return (this.selections.key(id) ? true : false);
294 handleMouseDBClick : function(e, t){
298 handleMouseDown : function(e, t)
300 var rowIndex = this.grid.headerShow ? t.dom.rowIndex - 1 : t.dom.rowIndex ; // first row is header???
301 if(this.isLocked() || rowIndex < 0 ){
304 if(e.shiftKey && this.last !== false){
305 var last = this.last;
306 this.selectRange(last, rowIndex, e.ctrlKey);
307 this.last = last; // reset the last
311 var isSelected = this.isSelected(rowIndex);
312 //Roo.log("select row:" + rowIndex);
314 this.deselectRow(rowIndex);
316 this.selectRow(rowIndex, true);
320 if(e.button !== 0 && isSelected){
321 alert('rowIndex 2: ' + rowIndex);
322 view.focusRow(rowIndex);
323 }else if(e.ctrlKey && isSelected){
324 this.deselectRow(rowIndex);
325 }else if(!isSelected){
326 this.selectRow(rowIndex, e.button === 0 && (e.ctrlKey || e.shiftKey));
327 view.focusRow(rowIndex);
331 this.fireEvent("afterselectionchange", this);
334 handleDragableRowClick : function(grid, rowIndex, e)
336 if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
337 this.selectRow(rowIndex, false);
338 grid.view.focusRow(rowIndex);
339 this.fireEvent("afterselectionchange", this);
344 * Selects multiple rows.
345 * @param {Array} rows Array of the indexes of the row to select
346 * @param {Boolean} keepExisting (optional) True to keep existing selections
348 selectRows : function(rows, keepExisting){
350 this.clearSelections();
352 for(var i = 0, len = rows.length; i < len; i++){
353 this.selectRow(rows[i], true);
358 * Selects a range of rows. All rows in between startRow and endRow are also selected.
359 * @param {Number} startRow The index of the first row in the range
360 * @param {Number} endRow The index of the last row in the range
361 * @param {Boolean} keepExisting (optional) True to retain existing selections
363 selectRange : function(startRow, endRow, keepExisting){
368 this.clearSelections();
370 if(startRow <= endRow){
371 for(var i = startRow; i <= endRow; i++){
372 this.selectRow(i, true);
375 for(var i = startRow; i >= endRow; i--){
376 this.selectRow(i, true);
382 * Deselects a range of rows. All rows in between startRow and endRow are also deselected.
383 * @param {Number} startRow The index of the first row in the range
384 * @param {Number} endRow The index of the last row in the range
386 deselectRange : function(startRow, endRow, preventViewNotify){
390 for(var i = startRow; i <= endRow; i++){
391 this.deselectRow(i, preventViewNotify);
397 * @param {Number} row The index of the row to select
398 * @param {Boolean} keepExisting (optional) True to keep existing selections
400 selectRow : function(index, keepExisting, preventViewNotify)
402 if(this.locked || (index < 0 || index > this.grid.store.getCount())) {
405 if(this.fireEvent("beforerowselect", this, index, keepExisting) !== false){
406 if(!keepExisting || this.singleSelect){
407 this.clearSelections();
410 var r = this.grid.store.getAt(index);
411 //console.log('selectRow - record id :' + r.id);
413 this.selections.add(r);
414 this.last = this.lastActive = index;
415 if(!preventViewNotify){
416 var proxy = new Roo.Element(
417 this.grid.getRowDom(index)
419 proxy.addClass('bg-info info');
421 this.fireEvent("rowselect", this, index, r);
422 this.fireEvent("selectionchange", this);
428 * @param {Number} row The index of the row to deselect
430 deselectRow : function(index, preventViewNotify)
435 if(this.last == index){
438 if(this.lastActive == index){
439 this.lastActive = false;
442 var r = this.grid.store.getAt(index);
447 this.selections.remove(r);
448 //.console.log('deselectRow - record id :' + r.id);
449 if(!preventViewNotify){
451 var proxy = new Roo.Element(
452 this.grid.getRowDom(index)
454 proxy.removeClass('bg-info info');
456 this.fireEvent("rowdeselect", this, index);
457 this.fireEvent("selectionchange", this);
461 restoreLast : function(){
463 this.last = this._last;
468 acceptsNav : function(row, col, cm){
469 return !cm.isHidden(col) && cm.isCellEditable(col, row);
473 onEditorKey : function(field, e){
474 var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
479 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
481 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
483 }else if(k == e.ENTER && !e.ctrlKey){
487 newCell = g.walkCells(ed.row-1, ed.col, -1, this.acceptsNav, this);
489 newCell = g.walkCells(ed.row+1, ed.col, 1, this.acceptsNav, this);
491 }else if(k == e.ESC){
495 g.startEditing(newCell[0], newCell[1]);