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