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>
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
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.
23 * @param {Object} config
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)
33 constructor : function(config){
34 Ext.apply(this, config);
35 this.selections = new Ext.util.MixedCollection(false, function(o){
40 this.lastActive = false;
43 <div id="event-Ext.grid.RowSelectionModel-selectionchange"></div>/**
44 * @event selectionchange
45 * Fires when the selection changes
46 * @param {SelectionModel} this
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
58 <div id="event-Ext.grid.RowSelectionModel-rowselect"></div>/**
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
66 <div id="event-Ext.grid.RowSelectionModel-rowdeselect"></div>/**
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
76 Ext.grid.RowSelectionModel.superclass.constructor.call(this);
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.
85 initEvents : function(){
87 if(!this.grid.enableDragDrop && !this.grid.enableDrag){
88 this.grid.on('rowmousedown', this.handleMouseDown, this);
91 this.rowNav = new Ext.KeyNav(this.grid.getGridEl(), {
93 down: this.onKeyPress,
97 this.grid.getView().on({
99 refresh: this.onRefresh,
100 rowupdated: this.onRowUpdated,
101 rowremoved: this.onRemove
105 onKeyPress : function(e, name){
106 var up = name == 'up',
107 method = up ? 'selectPrevious' : 'selectNext',
110 if(!e.shiftKey || this.singleSelect){
112 }else if(this.last !== false && this.lastActive !== false){
114 this.selectRange(this.last, this.lastActive + add);
115 this.grid.getView().focusRow(this.lastActive);
120 this.selectFirstRow();
125 onRefresh : function(){
126 var ds = this.grid.store,
127 s = this.getSelections(),
133 this.clearSelections(true);
136 if((index = ds.indexOfId(r.id)) != -1){
137 this.selectRow(index, true);
140 if(s.length != this.selections.getCount()){
141 this.fireEvent('selectionchange', this);
147 onRemove : function(v, index, r){
148 if(this.selections.remove(r) !== false){
149 this.fireEvent('selectionchange', this);
154 onRowUpdated : function(v, index, r){
155 if(this.isSelected(r)){
156 v.onRowSelect(index);
160 <div id="method-Ext.grid.RowSelectionModel-selectRecords"></div>/**
162 * @param {Array} records The records to select
163 * @param {Boolean} keepExisting (optional) <tt>true</tt> to keep existing selections
165 selectRecords : function(records, keepExisting){
167 this.clearSelections();
169 var ds = this.grid.store,
171 len = records.length;
173 this.selectRow(ds.indexOf(records[i]), true);
177 <div id="method-Ext.grid.RowSelectionModel-getCount"></div>/**
178 * Gets the number of selected rows.
181 getCount : function(){
182 return this.selections.length;
185 <div id="method-Ext.grid.RowSelectionModel-selectFirstRow"></div>/**
186 * Selects the first row in the grid.
188 selectFirstRow : function(){
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
196 selectLastRow : function(keepExisting){
197 this.selectRow(this.grid.store.getCount() - 1, keepExisting);
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>
205 selectNext : function(keepExisting){
207 this.selectRow(this.last+1, keepExisting);
208 this.grid.getView().focusRow(this.last);
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>
219 selectPrevious : function(keepExisting){
220 if(this.hasPrevious()){
221 this.selectRow(this.last-1, keepExisting);
222 this.grid.getView().focusRow(this.last);
228 <div id="method-Ext.grid.RowSelectionModel-hasNext"></div>/**
229 * Returns true if there is a next record to select
232 hasNext : function(){
233 return this.last !== false && (this.last+1) < this.grid.store.getCount();
236 <div id="method-Ext.grid.RowSelectionModel-hasPrevious"></div>/**
237 * Returns true if there is a previous record to select
240 hasPrevious : function(){
245 <div id="method-Ext.grid.RowSelectionModel-getSelections"></div>/**
246 * Returns the selected records
247 * @return {Array} Array of selected records
249 getSelections : function(){
250 return [].concat(this.selections.items);
253 <div id="method-Ext.grid.RowSelectionModel-getSelected"></div>/**
254 * Returns the first selected record.
257 getSelected : function(){
258 return this.selections.itemAt(0);
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
269 each : function(fn, scope){
270 var s = this.getSelections(),
275 if(fn.call(scope || this, s[i], i) === false){
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}.
288 clearSelections : function(fast){
293 var ds = this.grid.store,
296 this.deselectRow(ds.indexOfId(r.id));
300 this.selections.clear();
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}.
310 selectAll : function(){
314 this.selections.clear();
315 for(var i = 0, len = this.grid.store.getCount(); i < len; i++){
316 this.selectRow(i, true);
320 <div id="method-Ext.grid.RowSelectionModel-hasSelection"></div>/**
321 * Returns <tt>true</tt> if there is a selection.
324 hasSelection : function(){
325 return this.selections.length > 0;
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
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);
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
343 isIdSelected : function(id){
344 return (this.selections.key(id) ? true : false);
348 handleMouseDown : function(g, rowIndex, e){
349 if(e.button !== 0 || this.isLocked()){
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);
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);
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>)
375 selectRows : function(rows, keepExisting){
377 this.clearSelections();
379 for(var i = 0, len = rows.length; i < len; i++){
380 this.selectRow(rows[i], true);
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
392 selectRange : function(startRow, endRow, keepExisting){
398 this.clearSelections();
400 if(startRow <= endRow){
401 for(i = startRow; i <= endRow; i++){
402 this.selectRow(i, true);
405 for(i = startRow; i >= endRow; i--){
406 this.selectRow(i, true);
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
418 deselectRange : function(startRow, endRow, preventViewNotify){
422 for(var i = startRow; i <= endRow; i++){
423 this.deselectRow(i, preventViewNotify);
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)
438 selectRow : function(index, keepExisting, preventViewNotify){
439 if(this.isLocked() || (index < 0 || index >= this.grid.store.getCount()) || (keepExisting && this.isSelected(index))){
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();
447 this.selections.add(r);
448 this.last = this.lastActive = index;
449 if(!preventViewNotify){
450 this.grid.getView().onRowSelect(index);
453 this.fireEvent('rowselect', this, index, r);
454 this.fireEvent('selectionchange', this);
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)
468 deselectRow : function(index, preventViewNotify){
472 if(this.last == index){
475 if(this.lastActive == index){
476 this.lastActive = false;
478 var r = this.grid.store.getAt(index);
480 this.selections.remove(r);
481 if(!preventViewNotify){
482 this.grid.getView().onRowDeselect(index);
484 this.fireEvent('rowdeselect', this, index, r);
485 this.fireEvent('selectionchange', this);
490 acceptsNav : function(row, col, cm){
491 return !cm.isHidden(col) && cm.isCellEditable(col, row);
495 onEditorKey : function(field, e){
508 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
510 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
512 }else if(k == e.ENTER){
513 if(this.moveEditorOnEnter !== false){
515 newCell = g.walkCells(last.row - 1, last.col, -1, this.acceptsNav, this);
517 newCell = g.walkCells(last.row + 1, last.col, 1, this.acceptsNav, this);
525 this.onEditorSelect(r, last.row);
527 if(g.isEditor && g.editing){ // *** handle tabbing while editorgrid is in edit mode
529 if(ae && ae.field.triggerBlur){
530 // *** if activeEditor is a TriggerField, explicitly call its triggerBlur() method
531 ae.field.triggerBlur();
534 g.startEditing(r, c);
538 onEditorSelect: function(row, lastRow){
540 this.selectRow(row); // *** highlight newly-selected cell and update selection
544 destroy : function(){
545 Ext.destroy(this.rowNav);
547 Ext.grid.RowSelectionModel.superclass.destroy.call(this);