Upgrade to ExtJS 3.3.1 - Released 11/30/2010
[extjs.git] / docs / source / RowSelectionModel.html
1 <html>
2 <head>
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
4   <title>The source code</title>
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 </head>
8 <body  onload="prettyPrint();">
9     <pre class="prettyprint lang-js">/*!
10  * Ext JS Library 3.3.1
11  * Copyright(c) 2006-2010 Sencha Inc.
12  * licensing@sencha.com
13  * http://www.sencha.com/license
14  */
15 <div id="cls-Ext.grid.RowSelectionModel"></div>/**
16  * @class Ext.grid.RowSelectionModel
17  * @extends Ext.grid.AbstractSelectionModel
18  * The default SelectionModel used by {@link Ext.grid.GridPanel}.
19  * It supports multiple selections and keyboard selection/navigation. The objects stored
20  * as selections and returned by {@link #getSelected}, and {@link #getSelections} are
21  * the {@link Ext.data.Record Record}s which provide the data for the selected rows.
22  * @constructor
23  * @param {Object} config
24  */
25 Ext.grid.RowSelectionModel = Ext.extend(Ext.grid.AbstractSelectionModel,  {
26     <div id="cfg-Ext.grid.RowSelectionModel-singleSelect"></div>/**
27      * @cfg {Boolean} singleSelect
28      * <tt>true</tt> to allow selection of only one row at a time (defaults to <tt>false</tt>
29      * allowing multiple selections)
30      */
31     singleSelect : false,
32     
33     constructor : function(config){
34         Ext.apply(this, config);
35         this.selections = new Ext.util.MixedCollection(false, function(o){
36             return o.id;
37         });
38
39         this.last = false;
40         this.lastActive = false;
41
42         this.addEvents(
43                 <div id="event-Ext.grid.RowSelectionModel-selectionchange"></div>/**
44                  * @event selectionchange
45                  * Fires when the selection changes
46                  * @param {SelectionModel} this
47                  */
48                 'selectionchange',
49                 <div id="event-Ext.grid.RowSelectionModel-beforerowselect"></div>/**
50                  * @event beforerowselect
51                  * Fires before a row is selected, return false to cancel the selection.
52                  * @param {SelectionModel} this
53                  * @param {Number} rowIndex The index to be selected
54                  * @param {Boolean} keepExisting False if other selections will be cleared
55                  * @param {Record} record The record to be selected
56                  */
57                 'beforerowselect',
58                 <div id="event-Ext.grid.RowSelectionModel-rowselect"></div>/**
59                  * @event rowselect
60                  * Fires when a row is selected.
61                  * @param {SelectionModel} this
62                  * @param {Number} rowIndex The selected index
63                  * @param {Ext.data.Record} r The selected record
64                  */
65                 'rowselect',
66                 <div id="event-Ext.grid.RowSelectionModel-rowdeselect"></div>/**
67                  * @event rowdeselect
68                  * Fires when a row is deselected.  To prevent deselection
69                  * {@link Ext.grid.AbstractSelectionModel#lock lock the selections}. 
70                  * @param {SelectionModel} this
71                  * @param {Number} rowIndex
72                  * @param {Record} record
73                  */
74                 'rowdeselect'
75         );
76         Ext.grid.RowSelectionModel.superclass.constructor.call(this);
77     },
78
79     <div id="cfg-Ext.grid.RowSelectionModel-moveEditorOnEnter"></div>/**
80      * @cfg {Boolean} moveEditorOnEnter
81      * <tt>false</tt> to turn off moving the editor to the next row down when the enter key is pressed
82      * or the next row up when shift + enter keys are pressed.
83      */
84     // private
85     initEvents : function(){
86
87         if(!this.grid.enableDragDrop && !this.grid.enableDrag){
88             this.grid.on('rowmousedown', this.handleMouseDown, this);
89         }
90
91         this.rowNav = new Ext.KeyNav(this.grid.getGridEl(), {
92             up: this.onKeyPress, 
93             down: this.onKeyPress,
94             scope: this
95         });
96
97         this.grid.getView().on({
98             scope: this,
99             refresh: this.onRefresh,
100             rowupdated: this.onRowUpdated,
101             rowremoved: this.onRemove
102         });
103     },
104     
105     onKeyPress : function(e, name){
106         var up = name == 'up',
107             method = up ? 'selectPrevious' : 'selectNext',
108             add = up ? -1 : 1,
109             last;
110         if(!e.shiftKey || this.singleSelect){
111             this[method](false);
112         }else if(this.last !== false && this.lastActive !== false){
113             last = this.last;
114             this.selectRange(this.last,  this.lastActive + add);
115             this.grid.getView().focusRow(this.lastActive);
116             if(last !== false){
117                 this.last = last;
118             }
119         }else{
120            this.selectFirstRow();
121         }
122     },
123
124     // private
125     onRefresh : function(){
126         var ds = this.grid.store,
127             s = this.getSelections(),
128             i = 0,
129             len = s.length, 
130             index, r;
131             
132         this.silent = true;
133         this.clearSelections(true);
134         for(; i < len; i++){
135             r = s[i];
136             if((index = ds.indexOfId(r.id)) != -1){
137                 this.selectRow(index, true);
138             }
139         }
140         if(s.length != this.selections.getCount()){
141             this.fireEvent('selectionchange', this);
142         }
143         this.silent = false;
144     },
145
146     // private
147     onRemove : function(v, index, r){
148         if(this.selections.remove(r) !== false){
149             this.fireEvent('selectionchange', this);
150         }
151     },
152
153     // private
154     onRowUpdated : function(v, index, r){
155         if(this.isSelected(r)){
156             v.onRowSelect(index);
157         }
158     },
159
160     <div id="method-Ext.grid.RowSelectionModel-selectRecords"></div>/**
161      * Select records.
162      * @param {Array} records The records to select
163      * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
164      */
165     selectRecords : function(records, keepExisting){
166         if(!keepExisting){
167             this.clearSelections();
168         }
169         var ds = this.grid.store,
170             i = 0,
171             len = records.length;
172         for(; i < len; i++){
173             this.selectRow(ds.indexOf(records[i]), true);
174         }
175     },
176
177     <div id="method-Ext.grid.RowSelectionModel-getCount"></div>/**
178      * Gets the number of selected rows.
179      * @return {Number}
180      */
181     getCount : function(){
182         return this.selections.length;
183     },
184
185     <div id="method-Ext.grid.RowSelectionModel-selectFirstRow"></div>/**
186      * Selects the first row in the grid.
187      */
188     selectFirstRow : function(){
189         this.selectRow(0);
190     },
191
192     <div id="method-Ext.grid.RowSelectionModel-selectLastRow"></div>/**
193      * Select the last row.
194      * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
195      */
196     selectLastRow : function(keepExisting){
197         this.selectRow(this.grid.store.getCount() - 1, keepExisting);
198     },
199
200     <div id="method-Ext.grid.RowSelectionModel-selectNext"></div>/**
201      * Selects the row immediately following the last selected row.
202      * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
203      * @return {Boolean} <tt>true</tt> if there is a next row, else <tt>false</tt>
204      */
205     selectNext : function(keepExisting){
206         if(this.hasNext()){
207             this.selectRow(this.last+1, keepExisting);
208             this.grid.getView().focusRow(this.last);
209             return true;
210         }
211         return false;
212     },
213
214     <div id="method-Ext.grid.RowSelectionModel-selectPrevious"></div>/**
215      * Selects the row that precedes the last selected row.
216      * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
217      * @return {Boolean} <tt>true</tt> if there is a previous row, else <tt>false</tt>
218      */
219     selectPrevious : function(keepExisting){
220         if(this.hasPrevious()){
221             this.selectRow(this.last-1, keepExisting);
222             this.grid.getView().focusRow(this.last);
223             return true;
224         }
225         return false;
226     },
227
228     <div id="method-Ext.grid.RowSelectionModel-hasNext"></div>/**
229      * Returns true if there is a next record to select
230      * @return {Boolean}
231      */
232     hasNext : function(){
233         return this.last !== false && (this.last+1) < this.grid.store.getCount();
234     },
235
236     <div id="method-Ext.grid.RowSelectionModel-hasPrevious"></div>/**
237      * Returns true if there is a previous record to select
238      * @return {Boolean}
239      */
240     hasPrevious : function(){
241         return !!this.last;
242     },
243
244
245     <div id="method-Ext.grid.RowSelectionModel-getSelections"></div>/**
246      * Returns the selected records
247      * @return {Array} Array of selected records
248      */
249     getSelections : function(){
250         return [].concat(this.selections.items);
251     },
252
253     <div id="method-Ext.grid.RowSelectionModel-getSelected"></div>/**
254      * Returns the first selected record.
255      * @return {Record}
256      */
257     getSelected : function(){
258         return this.selections.itemAt(0);
259     },
260
261     <div id="method-Ext.grid.RowSelectionModel-each"></div>/**
262      * Calls the passed function with each selection. If the function returns
263      * <tt>false</tt>, iteration is stopped and this function returns
264      * <tt>false</tt>. Otherwise it returns <tt>true</tt>.
265      * @param {Function} fn The function to call upon each iteration. It is passed the selected {@link Ext.data.Record Record}.
266      * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function is executed. Defaults to this RowSelectionModel.
267      * @return {Boolean} true if all selections were iterated
268      */
269     each : function(fn, scope){
270         var s = this.getSelections(),
271             i = 0,
272             len = s.length;
273             
274         for(; i < len; i++){
275             if(fn.call(scope || this, s[i], i) === false){
276                 return false;
277             }
278         }
279         return true;
280     },
281
282     <div id="method-Ext.grid.RowSelectionModel-clearSelections"></div>/**
283      * Clears all selections if the selection model
284      * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
285      * @param {Boolean} fast (optional) <tt>true</tt> to bypass the
286      * conditional checks and events described in {@link #deselectRow}.
287      */
288     clearSelections : function(fast){
289         if(this.isLocked()){
290             return;
291         }
292         if(fast !== true){
293             var ds = this.grid.store,
294                 s = this.selections;
295             s.each(function(r){
296                 this.deselectRow(ds.indexOfId(r.id));
297             }, this);
298             s.clear();
299         }else{
300             this.selections.clear();
301         }
302         this.last = false;
303     },
304
305
306     <div id="method-Ext.grid.RowSelectionModel-selectAll"></div>/**
307      * Selects all rows if the selection model
308      * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}. 
309      */
310     selectAll : function(){
311         if(this.isLocked()){
312             return;
313         }
314         this.selections.clear();
315         for(var i = 0, len = this.grid.store.getCount(); i < len; i++){
316             this.selectRow(i, true);
317         }
318     },
319
320     <div id="method-Ext.grid.RowSelectionModel-hasSelection"></div>/**
321      * Returns <tt>true</tt> if there is a selection.
322      * @return {Boolean}
323      */
324     hasSelection : function(){
325         return this.selections.length > 0;
326     },
327
328     <div id="method-Ext.grid.RowSelectionModel-isSelected"></div>/**
329      * Returns <tt>true</tt> if the specified row is selected.
330      * @param {Number/Record} index The record or index of the record to check
331      * @return {Boolean}
332      */
333     isSelected : function(index){
334         var r = Ext.isNumber(index) ? this.grid.store.getAt(index) : index;
335         return (r && this.selections.key(r.id) ? true : false);
336     },
337
338     <div id="method-Ext.grid.RowSelectionModel-isIdSelected"></div>/**
339      * Returns <tt>true</tt> if the specified record id is selected.
340      * @param {String} id The id of record to check
341      * @return {Boolean}
342      */
343     isIdSelected : function(id){
344         return (this.selections.key(id) ? true : false);
345     },
346
347     // private
348     handleMouseDown : function(g, rowIndex, e){
349         if(e.button !== 0 || this.isLocked()){
350             return;
351         }
352         var view = this.grid.getView();
353         if(e.shiftKey && !this.singleSelect && this.last !== false){
354             var last = this.last;
355             this.selectRange(last, rowIndex, e.ctrlKey);
356             this.last = last; // reset the last
357             view.focusRow(rowIndex);
358         }else{
359             var isSelected = this.isSelected(rowIndex);
360             if(e.ctrlKey && isSelected){
361                 this.deselectRow(rowIndex);
362             }else if(!isSelected || this.getCount() > 1){
363                 this.selectRow(rowIndex, e.ctrlKey || e.shiftKey);
364                 view.focusRow(rowIndex);
365             }
366         }
367     },
368
369     <div id="method-Ext.grid.RowSelectionModel-selectRows"></div>/**
370      * Selects multiple rows.
371      * @param {Array} rows Array of the indexes of the row to select
372      * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep
373      * existing selections (defaults to <tt>false</tt>)
374      */
375     selectRows : function(rows, keepExisting){
376         if(!keepExisting){
377             this.clearSelections();
378         }
379         for(var i = 0, len = rows.length; i < len; i++){
380             this.selectRow(rows[i], true);
381         }
382     },
383
384     <div id="method-Ext.grid.RowSelectionModel-selectRange"></div>/**
385      * Selects a range of rows if the selection model
386      * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.
387      * All rows in between startRow and endRow are also selected.
388      * @param {Number} startRow The index of the first row in the range
389      * @param {Number} endRow The index of the last row in the range
390      * @param {Boolean} keepExisting (optional) True to retain existing selections
391      */
392     selectRange : function(startRow, endRow, keepExisting){
393         var i;
394         if(this.isLocked()){
395             return;
396         }
397         if(!keepExisting){
398             this.clearSelections();
399         }
400         if(startRow <= endRow){
401             for(i = startRow; i <= endRow; i++){
402                 this.selectRow(i, true);
403             }
404         }else{
405             for(i = startRow; i >= endRow; i--){
406                 this.selectRow(i, true);
407             }
408         }
409     },
410
411     <div id="method-Ext.grid.RowSelectionModel-deselectRange"></div>/**
412      * Deselects a range of rows if the selection model
413      * {@link Ext.grid.AbstractSelectionModel#isLocked is not locked}.  
414      * All rows in between startRow and endRow are also deselected.
415      * @param {Number} startRow The index of the first row in the range
416      * @param {Number} endRow The index of the last row in the range
417      */
418     deselectRange : function(startRow, endRow, preventViewNotify){
419         if(this.isLocked()){
420             return;
421         }
422         for(var i = startRow; i <= endRow; i++){
423             this.deselectRow(i, preventViewNotify);
424         }
425     },
426
427     <div id="method-Ext.grid.RowSelectionModel-selectRow"></div>/**
428      * Selects a row.  Before selecting a row, checks if the selection model
429      * {@link Ext.grid.AbstractSelectionModel#isLocked is locked} and fires the
430      * {@link #beforerowselect} event.  If these checks are satisfied the row
431      * will be selected and followed up by  firing the {@link #rowselect} and
432      * {@link #selectionchange} events.
433      * @param {Number} row The index of the row to select
434      * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
435      * @param {Boolean} preventViewNotify (optional) Specify <tt>true</tt> to
436      * prevent notifying the view (disables updating the selected appearance)
437      */
438     selectRow : function(index, keepExisting, preventViewNotify){
439         if(this.isLocked() || (index < 0 || index >= this.grid.store.getCount()) || (keepExisting && this.isSelected(index))){
440             return;
441         }
442         var r = this.grid.store.getAt(index);
443         if(r && this.fireEvent('beforerowselect', this, index, keepExisting, r) !== false){
444             if(!keepExisting || this.singleSelect){
445                 this.clearSelections();
446             }
447             this.selections.add(r);
448             this.last = this.lastActive = index;
449             if(!preventViewNotify){
450                 this.grid.getView().onRowSelect(index);
451             }
452             if(!this.silent){
453                 this.fireEvent('rowselect', this, index, r);
454                 this.fireEvent('selectionchange', this);
455             }
456         }
457     },
458
459     <div id="method-Ext.grid.RowSelectionModel-deselectRow"></div>/**
460      * Deselects a row.  Before deselecting a row, checks if the selection model
461      * {@link Ext.grid.AbstractSelectionModel#isLocked is locked}.
462      * If this check is satisfied the row will be deselected and followed up by
463      * firing the {@link #rowdeselect} and {@link #selectionchange} events.
464      * @param {Number} row The index of the row to deselect
465      * @param {Boolean} preventViewNotify (optional) Specify <tt>true</tt> to
466      * prevent notifying the view (disables updating the selected appearance)
467      */
468     deselectRow : function(index, preventViewNotify){
469         if(this.isLocked()){
470             return;
471         }
472         if(this.last == index){
473             this.last = false;
474         }
475         if(this.lastActive == index){
476             this.lastActive = false;
477         }
478         var r = this.grid.store.getAt(index);
479         if(r){
480             this.selections.remove(r);
481             if(!preventViewNotify){
482                 this.grid.getView().onRowDeselect(index);
483             }
484             this.fireEvent('rowdeselect', this, index, r);
485             this.fireEvent('selectionchange', this);
486         }
487     },
488
489     // private
490     acceptsNav : function(row, col, cm){
491         return !cm.isHidden(col) && cm.isCellEditable(col, row);
492     },
493
494     // private
495     onEditorKey : function(field, e){
496         var k = e.getKey(), 
497             newCell, 
498             g = this.grid, 
499             last = g.lastEdit,
500             ed = g.activeEditor,
501             shift = e.shiftKey,
502             ae, last, r, c;
503             
504         if(k == e.TAB){
505             e.stopEvent();
506             ed.completeEdit();
507             if(shift){
508                 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
509             }else{
510                 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
511             }
512         }else if(k == e.ENTER){
513             if(this.moveEditorOnEnter !== false){
514                 if(shift){
515                     newCell = g.walkCells(last.row - 1, last.col, -1, this.acceptsNav, this);
516                 }else{
517                     newCell = g.walkCells(last.row + 1, last.col, 1, this.acceptsNav, this);
518                 }
519             }
520         }
521         if(newCell){
522             r = newCell[0];
523             c = newCell[1];
524
525             this.onEditorSelect(r, last.row);
526
527             if(g.isEditor && g.editing){ // *** handle tabbing while editorgrid is in edit mode
528                 ae = g.activeEditor;
529                 if(ae && ae.field.triggerBlur){
530                     // *** if activeEditor is a TriggerField, explicitly call its triggerBlur() method
531                     ae.field.triggerBlur();
532                 }
533             }
534             g.startEditing(r, c);
535         }
536     },
537     
538     onEditorSelect: function(row, lastRow){
539         if(lastRow != row){
540             this.selectRow(row); // *** highlight newly-selected cell and update selection
541         }
542     },
543     
544     destroy : function(){
545         Ext.destroy(this.rowNav);
546         this.rowNav = null;
547         Ext.grid.RowSelectionModel.superclass.destroy.call(this);
548     }
549 });
550 </pre>    
551 </body>
552 </html>