8 Roo.bootstrap.Table.RowSelectionModel = function(config){
9 Roo.apply(this, config);
10 this.selections = new Roo.util.MixedCollection(false, function(o){
15 this.lastActive = false;
19 * @event selectionchange
20 * Fires when the selection changes
21 * @param {SelectionModel} this
23 "selectionchange" : true,
25 * @event afterselectionchange
26 * Fires after the selection changes (eg. by key press or clicking)
27 * @param {SelectionModel} this
29 "afterselectionchange" : true,
31 * @event beforerowselect
32 * Fires when a row is selected being selected, return false to cancel.
33 * @param {SelectionModel} this
34 * @param {Number} rowIndex The selected index
35 * @param {Boolean} keepExisting False if other selections will be cleared
37 "beforerowselect" : true,
40 * Fires when a row is selected.
41 * @param {SelectionModel} this
42 * @param {Number} rowIndex The selected index
43 * @param {Roo.data.Record} r The record
48 * Fires when a row is deselected.
49 * @param {SelectionModel} this
50 * @param {Number} rowIndex The selected index
54 Roo.bootstrap.Table.RowSelectionModel.superclass.constructor.call(this);
58 Roo.extend(Roo.bootstrap.Table.RowSelectionModel, Roo.bootstrap.Table.AbstractSelectionModel, {
60 * @cfg {Boolean} singleSelect
61 * True to allow selection of only one row at a time (defaults to false)
66 initEvents : function()
69 //if(!this.grid.enableDragDrop && !this.grid.enableDrag){
70 // this.growclickrid.on("mousedown", this.handleMouseDown, this);
71 //}else{ // allow click to work like normal
72 // this.grid.on("rowclick", this.handleDragableRowClick, this);
74 //this.grid.on("rowdblclick", this.handleMouseDBClick, this);
75 this.grid.on("rowclick", this.handleMouseDown, 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);
110 this.grid.store.on('load', function(){
111 this.selections.clear();
114 var view = this.grid.view;
115 view.on("refresh", this.onRefresh, this);
116 view.on("rowupdated", this.onRowUpdated, this);
117 view.on("rowremoved", this.onRemove, this);
122 onRefresh : function()
124 var ds = this.grid.store, i, v = this.grid.view;
125 var s = this.selections;
127 if((i = ds.indexOfId(r.id)) != -1){
136 onRemove : function(v, index, r){
137 this.selections.remove(r);
141 onRowUpdated : function(v, index, r){
142 if(this.isSelected(r)){
143 v.onRowSelect(index);
149 * @param {Array} records The records to select
150 * @param {Boolean} keepExisting (optional) True to keep existing selections
152 selectRecords : function(records, keepExisting)
155 this.clearSelections();
157 var ds = this.grid.store;
158 for(var i = 0, len = records.length; i < len; i++){
159 this.selectRow(ds.indexOf(records[i]), true);
164 * Gets the number of selected rows.
167 getCount : function(){
168 return this.selections.length;
172 * Selects the first row in the grid.
174 selectFirstRow : function(){
179 * Select the last row.
180 * @param {Boolean} keepExisting (optional) True to keep existing selections
182 selectLastRow : function(keepExisting){
183 //this.selectRow(this.grid.dataSource.getCount() - 1, keepExisting);
184 this.selectRow(this.grid.store.getCount() - 1, keepExisting);
188 * Selects the row immediately following the last selected row.
189 * @param {Boolean} keepExisting (optional) True to keep existing selections
191 selectNext : function(keepExisting)
193 if(this.last !== false && (this.last+1) < this.grid.store.getCount()){
194 this.selectRow(this.last+1, keepExisting);
195 this.grid.getView().focusRow(this.last);
200 * Selects the row that precedes the last selected row.
201 * @param {Boolean} keepExisting (optional) True to keep existing selections
203 selectPrevious : function(keepExisting){
205 this.selectRow(this.last-1, keepExisting);
206 this.grid.getView().focusRow(this.last);
211 * Returns the selected records
212 * @return {Array} Array of selected records
214 getSelections : function(){
215 return [].concat(this.selections.items);
219 * Returns the first selected record.
222 getSelected : function(){
223 return this.selections.itemAt(0);
228 * Clears all selections.
230 clearSelections : function(fast)
236 var ds = this.grid.store;
237 var s = this.selections;
239 this.deselectRow(ds.indexOfId(r.id));
243 this.selections.clear();
252 selectAll : function(){
256 this.selections.clear();
257 for(var i = 0, len = this.grid.store.getCount(); i < len; i++){
258 this.selectRow(i, true);
263 * Returns True if there is a selection.
266 hasSelection : function(){
267 return this.selections.length > 0;
271 * Returns True if the specified row is selected.
272 * @param {Number/Record} record The record or index of the record to check
275 isSelected : function(index){
276 var r = typeof index == "number" ? this.grid.store.getAt(index) : index;
277 return (r && this.selections.key(r.id) ? true : false);
281 * Returns True if the specified record id is selected.
282 * @param {String} id The id of record to check
285 isIdSelected : function(id){
286 return (this.selections.key(id) ? true : false);
291 handleMouseDBClick : function(e, t){
295 handleMouseDown : function(e, t)
297 var rowIndex = this.grid.headerShow ? t.dom.rowIndex - 1 : t.dom.rowIndex ; // first row is header???
298 if(this.isLocked() || rowIndex < 0 ){
301 if(e.shiftKey && this.last !== false){
302 var last = this.last;
303 this.selectRange(last, rowIndex, e.ctrlKey);
304 this.last = last; // reset the last
308 var isSelected = this.isSelected(rowIndex);
309 //Roo.log("select row:" + rowIndex);
311 this.deselectRow(rowIndex);
313 this.selectRow(rowIndex, true);
317 if(e.button !== 0 && isSelected){
318 alert('rowIndex 2: ' + rowIndex);
319 view.focusRow(rowIndex);
320 }else if(e.ctrlKey && isSelected){
321 this.deselectRow(rowIndex);
322 }else if(!isSelected){
323 this.selectRow(rowIndex, e.button === 0 && (e.ctrlKey || e.shiftKey));
324 view.focusRow(rowIndex);
328 this.fireEvent("afterselectionchange", this);
331 handleDragableRowClick : function(grid, rowIndex, e)
333 if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
334 this.selectRow(rowIndex, false);
335 grid.view.focusRow(rowIndex);
336 this.fireEvent("afterselectionchange", this);
341 * Selects multiple rows.
342 * @param {Array} rows Array of the indexes of the row to select
343 * @param {Boolean} keepExisting (optional) True to keep existing selections
345 selectRows : function(rows, keepExisting){
347 this.clearSelections();
349 for(var i = 0, len = rows.length; i < len; i++){
350 this.selectRow(rows[i], true);
355 * Selects a range of rows. All rows in between startRow and endRow are also selected.
356 * @param {Number} startRow The index of the first row in the range
357 * @param {Number} endRow The index of the last row in the range
358 * @param {Boolean} keepExisting (optional) True to retain existing selections
360 selectRange : function(startRow, endRow, keepExisting){
365 this.clearSelections();
367 if(startRow <= endRow){
368 for(var i = startRow; i <= endRow; i++){
369 this.selectRow(i, true);
372 for(var i = startRow; i >= endRow; i--){
373 this.selectRow(i, true);
379 * Deselects a range of rows. All rows in between startRow and endRow are also deselected.
380 * @param {Number} startRow The index of the first row in the range
381 * @param {Number} endRow The index of the last row in the range
383 deselectRange : function(startRow, endRow, preventViewNotify){
387 for(var i = startRow; i <= endRow; i++){
388 this.deselectRow(i, preventViewNotify);
394 * @param {Number} row The index of the row to select
395 * @param {Boolean} keepExisting (optional) True to keep existing selections
397 selectRow : function(index, keepExisting, preventViewNotify)
399 if(this.locked || (index < 0 || index > this.grid.store.getCount())) {
402 if(this.fireEvent("beforerowselect", this, index, keepExisting) !== false){
403 if(!keepExisting || this.singleSelect){
404 this.clearSelections();
407 var r = this.grid.store.getAt(index);
408 //console.log('selectRow - record id :' + r.id);
410 this.selections.add(r);
411 this.last = this.lastActive = index;
412 if(!preventViewNotify){
413 var proxy = new Roo.Element(
414 this.grid.getRowDom(index)
416 proxy.addClass('bg-info info');
418 this.fireEvent("rowselect", this, index, r);
419 this.fireEvent("selectionchange", this);
425 * @param {Number} row The index of the row to deselect
427 deselectRow : function(index, preventViewNotify)
432 if(this.last == index){
435 if(this.lastActive == index){
436 this.lastActive = false;
439 var r = this.grid.store.getAt(index);
444 this.selections.remove(r);
445 //.console.log('deselectRow - record id :' + r.id);
446 if(!preventViewNotify){
448 var proxy = new Roo.Element(
449 this.grid.getRowDom(index)
451 proxy.removeClass('bg-info info');
453 this.fireEvent("rowdeselect", this, index);
454 this.fireEvent("selectionchange", this);
458 restoreLast : function(){
460 this.last = this._last;
465 acceptsNav : function(row, col, cm){
466 return !cm.isHidden(col) && cm.isCellEditable(col, row);
470 onEditorKey : function(field, e){
471 var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
476 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
478 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
480 }else if(k == e.ENTER && !e.ctrlKey){
484 newCell = g.walkCells(ed.row-1, ed.col, -1, this.acceptsNav, this);
486 newCell = g.walkCells(ed.row+1, ed.col, 1, this.acceptsNav, this);
488 }else if(k == e.ESC){
492 g.startEditing(newCell[0], newCell[1]);