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.2.1
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.grid.ColumnModel"></div>/**
16 * @class Ext.grid.ColumnModel
17 * @extends Ext.util.Observable
18 * <p>After the data has been read into the client side cache (<b>{@link Ext.data.Store Store}</b>),
19 * the ColumnModel is used to configure how and what parts of that data will be displayed in the
20 * vertical slices (columns) of the grid. The Ext.grid.ColumnModel Class is the default implementation
21 * of a ColumnModel used by implentations of {@link Ext.grid.GridPanel GridPanel}.</p>
22 * <p>Data is mapped into the store's records and then indexed into the ColumnModel using the
23 * <tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt>:</p>
25 {data source} == mapping ==> {data store} == <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b> ==> {ColumnModel}
27 * <p>Each {@link Ext.grid.Column Column} in the grid's ColumnModel is configured with a
28 * <tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt> to specify how the data within
29 * each record in the store is indexed into the ColumnModel.</p>
30 * <p>There are two ways to initialize the ColumnModel class:</p>
31 * <p><u>Initialization Method 1: an Array</u></p>
33 var colModel = new Ext.grid.ColumnModel([
34 { header: "Ticker", width: 60, sortable: true},
35 { header: "Company Name", width: 150, sortable: true, id: 'company'},
36 { header: "Market Cap.", width: 100, sortable: true},
37 { header: "$ Sales", width: 100, sortable: true, renderer: money},
38 { header: "Employees", width: 100, sortable: true, resizable: false}
41 * <p>The ColumnModel may be initialized with an Array of {@link Ext.grid.Column} column configuration
42 * objects to define the initial layout / display of the columns in the Grid. The order of each
43 * {@link Ext.grid.Column} column configuration object within the specified Array defines the initial
44 * order of the column display. A Column's display may be initially hidden using the
45 * <tt>{@link Ext.grid.Column#hidden hidden}</tt></b> config property (and then shown using the column
46 * header menu). Fields that are not included in the ColumnModel will not be displayable at all.</p>
47 * <p>How each column in the grid correlates (maps) to the {@link Ext.data.Record} field in the
48 * {@link Ext.data.Store Store} the column draws its data from is configured through the
49 * <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b>. If the
50 * <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b> is not explicitly defined (as shown in the
51 * example above) it will use the column configuration's index in the Array as the index.</p>
52 * <p>See <b><tt>{@link Ext.grid.Column}</tt></b> for additional configuration options for each column.</p>
53 * <p><u>Initialization Method 2: an Object</u></p>
54 * <p>In order to use configuration options from <tt>Ext.grid.ColumnModel</tt>, an Object may be used to
55 * initialize the ColumnModel. The column configuration Array will be specified in the <tt><b>{@link #columns}</b></tt>
56 * config property. The <tt><b>{@link #defaults}</b></tt> config property can be used to apply defaults
57 * for all columns, e.g.:</p><pre><code>
58 var colModel = new Ext.grid.ColumnModel({
60 { header: "Ticker", width: 60, menuDisabled: false},
61 { header: "Company Name", width: 150, id: 'company'},
62 { header: "Market Cap."},
63 { header: "$ Sales", renderer: money},
64 { header: "Employees", resizable: false}
72 {@link #hiddenchange}: function(cm, colIndex, hidden) {
73 saveConfig(colIndex, hidden);
78 * <p>In both examples above, the ability to apply a CSS class to all cells in a column (including the
79 * header) is demonstrated through the use of the <b><tt>{@link Ext.grid.Column#id id}</tt></b> config
80 * option. This column could be styled by including the following css:</p><pre><code>
81 //add this css *after* the core css is loaded
83 color: red; // entire column will have red font
85 // modify the header row only, adding an icon to the column header
87 background: transparent
88 url(../../resources/images/icons/silk/building.png)
89 no-repeat 3px 3px ! important;
93 * Note that the "Company Name" column could be specified as the
94 * <b><tt>{@link Ext.grid.GridPanel}.{@link Ext.grid.GridPanel#autoExpandColumn autoExpandColumn}</tt></b>.
96 * @param {Mixed} config Specify either an Array of {@link Ext.grid.Column} configuration objects or specify
97 * a configuration Object (see introductory section discussion utilizing Initialization Method 2 above).
99 Ext.grid.ColumnModel = Ext.extend(Ext.util.Observable, {
100 <div id="cfg-Ext.grid.ColumnModel-defaultWidth"></div>/**
101 * @cfg {Number} defaultWidth (optional) The width of columns which have no <tt>{@link #width}</tt>
102 * specified (defaults to <tt>100</tt>). This property shall preferably be configured through the
103 * <tt><b>{@link #defaults}</b></tt> config property.
106 <div id="cfg-Ext.grid.ColumnModel-defaultSortable"></div>/**
107 * @cfg {Boolean} defaultSortable (optional) Default sortable of columns which have no
108 * sortable specified (defaults to <tt>false</tt>). This property shall preferably be configured
109 * through the <tt><b>{@link #defaults}</b></tt> config property.
111 defaultSortable: false,
112 <div id="cfg-Ext.grid.ColumnModel-columns"></div>/**
113 * @cfg {Array} columns An Array of object literals. The config options defined by
114 * <b>{@link Ext.grid.Column}</b> are the options which may appear in the object literal for each
115 * individual column definition.
117 <div id="cfg-Ext.grid.ColumnModel-defaults"></div>/**
118 * @cfg {Object} defaults Object literal which will be used to apply {@link Ext.grid.Column}
119 * configuration options to all <tt><b>{@link #columns}</b></tt>. Configuration options specified with
120 * individual {@link Ext.grid.Column column} configs will supersede these <tt><b>{@link #defaults}</b></tt>.
123 constructor : function(config){
124 <div id="prop-Ext.grid.ColumnModel-config"></div>/**
125 * An Array of {@link Ext.grid.Column Column definition} objects representing the configuration
126 * of this ColumnModel. See {@link Ext.grid.Column} for the configuration properties that may
132 Ext.apply(this, config);
133 this.setConfig(config.columns, true);
135 this.setConfig(config, true);
138 <div id="event-Ext.grid.ColumnModel-widthchange"></div>/**
140 * Fires when the width of a column is programmaticially changed using
141 * <code>{@link #setColumnWidth}</code>.
142 * Note internal resizing suppresses the event from firing. See also
143 * {@link Ext.grid.GridPanel}.<code>{@link #columnresize}</code>.
144 * @param {ColumnModel} this
145 * @param {Number} columnIndex The column index
146 * @param {Number} newWidth The new width
149 <div id="event-Ext.grid.ColumnModel-headerchange"></div>/**
150 * @event headerchange
151 * Fires when the text of a header changes.
152 * @param {ColumnModel} this
153 * @param {Number} columnIndex The column index
154 * @param {String} newText The new header text
157 <div id="event-Ext.grid.ColumnModel-hiddenchange"></div>/**
158 * @event hiddenchange
159 * Fires when a column is hidden or "unhidden".
160 * @param {ColumnModel} this
161 * @param {Number} columnIndex The column index
162 * @param {Boolean} hidden true if hidden, false otherwise
165 <div id="event-Ext.grid.ColumnModel-columnmoved"></div>/**
167 * Fires when a column is moved.
168 * @param {ColumnModel} this
169 * @param {Number} oldIndex
170 * @param {Number} newIndex
173 <div id="event-Ext.grid.ColumnModel-configchange"></div>/**
174 * @event configchange
175 * Fires when the configuration is changed
176 * @param {ColumnModel} this
180 Ext.grid.ColumnModel.superclass.constructor.call(this);
183 <div id="method-Ext.grid.ColumnModel-getColumnId"></div>/**
184 * Returns the id of the column at the specified index.
185 * @param {Number} index The column index
186 * @return {String} the id
188 getColumnId : function(index){
189 return this.config[index].id;
192 getColumnAt : function(index){
193 return this.config[index];
196 <div id="method-Ext.grid.ColumnModel-setConfig"></div>/**
197 * <p>Reconfigures this column model according to the passed Array of column definition objects.
198 * For a description of the individual properties of a column definition object, see the
199 * <a href="#Ext.grid.ColumnModel-configs">Config Options</a>.</p>
200 * <p>Causes the {@link #configchange} event to be fired. A {@link Ext.grid.GridPanel GridPanel}
201 * using this ColumnModel will listen for this event and refresh its UI automatically.</p>
202 * @param {Array} config Array of Column definition objects.
203 * @param {Boolean} initial Specify <tt>true</tt> to bypass cleanup which deletes the <tt>totalWidth</tt>
204 * and destroys existing editors.
206 setConfig : function(config, initial){
208 if(!initial){ // cleanup
209 delete this.totalWidth;
210 for(i = 0, len = this.config.length; i < len; i++){
213 //check here, in case we have a special column like a CheckboxSelectionModel
219 // backward compatibility
220 this.defaults = Ext.apply({
221 width: this.defaultWidth,
222 sortable: this.defaultSortable
225 this.config = config;
228 for(i = 0, len = config.length; i < len; i++){
229 c = Ext.applyIf(config[i], this.defaults);
230 // if no id, create one using column's ordinal position
231 if(Ext.isEmpty(c.id)){
235 var Cls = Ext.grid.Column.types[c.xtype || 'gridcolumn'];
239 this.lookup[c.id] = c;
242 this.fireEvent('configchange', this);
246 <div id="method-Ext.grid.ColumnModel-getColumnById"></div>/**
247 * Returns the column for a specified id.
248 * @param {String} id The column id
249 * @return {Object} the column
251 getColumnById : function(id){
252 return this.lookup[id];
255 <div id="method-Ext.grid.ColumnModel-getIndexById"></div>/**
256 * Returns the index for a specified column id.
257 * @param {String} id The column id
258 * @return {Number} the index, or -1 if not found
260 getIndexById : function(id){
261 for(var i = 0, len = this.config.length; i < len; i++){
262 if(this.config[i].id == id){
269 <div id="method-Ext.grid.ColumnModel-moveColumn"></div>/**
270 * Moves a column from one position to another.
271 * @param {Number} oldIndex The index of the column to move.
272 * @param {Number} newIndex The position at which to reinsert the coolumn.
274 moveColumn : function(oldIndex, newIndex){
275 var c = this.config[oldIndex];
276 this.config.splice(oldIndex, 1);
277 this.config.splice(newIndex, 0, c);
279 this.fireEvent("columnmoved", this, oldIndex, newIndex);
282 <div id="method-Ext.grid.ColumnModel-getColumnCount"></div>/**
283 * Returns the number of columns.
284 * @param {Boolean} visibleOnly Optional. Pass as true to only include visible columns.
287 getColumnCount : function(visibleOnly){
288 if(visibleOnly === true){
290 for(var i = 0, len = this.config.length; i < len; i++){
291 if(!this.isHidden(i)){
297 return this.config.length;
300 <div id="method-Ext.grid.ColumnModel-getColumnsBy"></div>/**
301 * Returns the column configs that return true by the passed function that is called
302 * with (columnConfig, index)
304 // returns an array of column config objects for all hidden columns
305 var columns = grid.getColumnModel().getColumnsBy(function(c){
309 * @param {Function} fn A function which, when passed a {@link Ext.grid.Column Column} object, must
310 * return <code>true</code> if the column is to be included in the returned Array.
311 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function
312 * is executed. Defaults to this ColumnModel.
313 * @return {Array} result
315 getColumnsBy : function(fn, scope){
317 for(var i = 0, len = this.config.length; i < len; i++){
318 var c = this.config[i];
319 if(fn.call(scope||this, c, i) === true){
326 <div id="method-Ext.grid.ColumnModel-isSortable"></div>/**
327 * Returns true if the specified column is sortable.
328 * @param {Number} col The column index
331 isSortable : function(col){
332 return !!this.config[col].sortable;
335 <div id="method-Ext.grid.ColumnModel-isMenuDisabled"></div>/**
336 * Returns true if the specified column menu is disabled.
337 * @param {Number} col The column index
340 isMenuDisabled : function(col){
341 return !!this.config[col].menuDisabled;
344 <div id="method-Ext.grid.ColumnModel-getRenderer"></div>/**
345 * Returns the rendering (formatting) function defined for the column.
346 * @param {Number} col The column index.
347 * @return {Function} The function used to render the cell. See {@link #setRenderer}.
349 getRenderer : function(col){
350 if(!this.config[col].renderer){
351 return Ext.grid.ColumnModel.defaultRenderer;
353 return this.config[col].renderer;
356 getRendererScope : function(col){
357 return this.config[col].scope;
360 <div id="method-Ext.grid.ColumnModel-setRenderer"></div>/**
361 * Sets the rendering (formatting) function for a column. See {@link Ext.util.Format} for some
362 * default formatting functions.
363 * @param {Number} col The column index
364 * @param {Function} fn The function to use to process the cell's raw data
365 * to return HTML markup for the grid view. The render function is called with
366 * the following parameters:<ul>
367 * <li><b>value</b> : Object<p class="sub-desc">The data value for the cell.</p></li>
368 * <li><b>metadata</b> : Object<p class="sub-desc">An object in which you may set the following attributes:<ul>
369 * <li><b>css</b> : String<p class="sub-desc">A CSS class name to add to the cell's TD element.</p></li>
370 * <li><b>attr</b> : String<p class="sub-desc">An HTML attribute definition string to apply to the data container element <i>within</i> the table cell
371 * (e.g. 'style="color:red;"').</p></li></ul></p></li>
372 * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record} from which the data was extracted.</p></li>
373 * <li><b>rowIndex</b> : Number<p class="sub-desc">Row index</p></li>
374 * <li><b>colIndex</b> : Number<p class="sub-desc">Column index</p></li>
375 * <li><b>store</b> : Ext.data.Store<p class="sub-desc">The {@link Ext.data.Store} object from which the Record was extracted.</p></li></ul>
377 setRenderer : function(col, fn){
378 this.config[col].renderer = fn;
381 <div id="method-Ext.grid.ColumnModel-getColumnWidth"></div>/**
382 * Returns the width for the specified column.
383 * @param {Number} col The column index
386 getColumnWidth : function(col){
387 return this.config[col].width;
390 <div id="method-Ext.grid.ColumnModel-setColumnWidth"></div>/**
391 * Sets the width for a column.
392 * @param {Number} col The column index
393 * @param {Number} width The new width
394 * @param {Boolean} suppressEvent True to suppress firing the <code>{@link #widthchange}</code>
395 * event. Defaults to false.
397 setColumnWidth : function(col, width, suppressEvent){
398 this.config[col].width = width;
399 this.totalWidth = null;
401 this.fireEvent("widthchange", this, col, width);
405 <div id="method-Ext.grid.ColumnModel-getTotalWidth"></div>/**
406 * Returns the total width of all columns.
407 * @param {Boolean} includeHidden True to include hidden column widths
410 getTotalWidth : function(includeHidden){
411 if(!this.totalWidth){
413 for(var i = 0, len = this.config.length; i < len; i++){
414 if(includeHidden || !this.isHidden(i)){
415 this.totalWidth += this.getColumnWidth(i);
419 return this.totalWidth;
422 <div id="method-Ext.grid.ColumnModel-getColumnHeader"></div>/**
423 * Returns the header for the specified column.
424 * @param {Number} col The column index
427 getColumnHeader : function(col){
428 return this.config[col].header;
431 <div id="method-Ext.grid.ColumnModel-setColumnHeader"></div>/**
432 * Sets the header for a column.
433 * @param {Number} col The column index
434 * @param {String} header The new header
436 setColumnHeader : function(col, header){
437 this.config[col].header = header;
438 this.fireEvent("headerchange", this, col, header);
441 <div id="method-Ext.grid.ColumnModel-getColumnTooltip"></div>/**
442 * Returns the tooltip for the specified column.
443 * @param {Number} col The column index
446 getColumnTooltip : function(col){
447 return this.config[col].tooltip;
449 <div id="method-Ext.grid.ColumnModel-setColumnTooltip"></div>/**
450 * Sets the tooltip for a column.
451 * @param {Number} col The column index
452 * @param {String} tooltip The new tooltip
454 setColumnTooltip : function(col, tooltip){
455 this.config[col].tooltip = tooltip;
458 <div id="method-Ext.grid.ColumnModel-getDataIndex"></div>/**
459 * Returns the dataIndex for the specified column.
461 // Get field name for the column
462 var fieldName = grid.getColumnModel().getDataIndex(columnIndex);
464 * @param {Number} col The column index
465 * @return {String} The column's dataIndex
467 getDataIndex : function(col){
468 return this.config[col].dataIndex;
471 <div id="method-Ext.grid.ColumnModel-setDataIndex"></div>/**
472 * Sets the dataIndex for a column.
473 * @param {Number} col The column index
474 * @param {String} dataIndex The new dataIndex
476 setDataIndex : function(col, dataIndex){
477 this.config[col].dataIndex = dataIndex;
480 <div id="method-Ext.grid.ColumnModel-findColumnIndex"></div>/**
481 * Finds the index of the first matching column for the given dataIndex.
482 * @param {String} col The dataIndex to find
483 * @return {Number} The column index, or -1 if no match was found
485 findColumnIndex : function(dataIndex){
487 for(var i = 0, len = c.length; i < len; i++){
488 if(c[i].dataIndex == dataIndex){
495 <div id="method-Ext.grid.ColumnModel-isCellEditable"></div>/**
496 * Returns true if the cell is editable.
498 var store = new Ext.data.Store({...});
499 var colModel = new Ext.grid.ColumnModel({
501 isCellEditable: function(col, row) {
502 var record = store.getAt(row);
503 if (record.get('readonly')) { // replace with your condition
506 return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, col, row);
509 var grid = new Ext.grid.GridPanel({
515 * @param {Number} colIndex The column index
516 * @param {Number} rowIndex The row index
519 isCellEditable : function(colIndex, rowIndex){
520 var c = this.config[colIndex],
524 return !!(ed || (!Ext.isDefined(ed) && c.editor));
527 <div id="method-Ext.grid.ColumnModel-getCellEditor"></div>/**
528 * Returns the editor defined for the cell/column.
529 * @param {Number} colIndex The column index
530 * @param {Number} rowIndex The row index
531 * @return {Ext.Editor} The {@link Ext.Editor Editor} that was created to wrap
532 * the {@link Ext.form.Field Field} used to edit the cell.
534 getCellEditor : function(colIndex, rowIndex){
535 return this.config[colIndex].getCellEditor(rowIndex);
538 <div id="method-Ext.grid.ColumnModel-setEditable"></div>/**
539 * Sets if a column is editable.
540 * @param {Number} col The column index
541 * @param {Boolean} editable True if the column is editable
543 setEditable : function(col, editable){
544 this.config[col].editable = editable;
547 <div id="method-Ext.grid.ColumnModel-isHidden"></div>/**
548 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#hidden hidden}</code>,
549 * <tt>false</tt> otherwise.
550 * @param {Number} colIndex The column index
553 isHidden : function(colIndex){
554 return !!this.config[colIndex].hidden; // ensure returns boolean
557 <div id="method-Ext.grid.ColumnModel-isFixed"></div>/**
558 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#fixed fixed}</code>,
559 * <tt>false</tt> otherwise.
560 * @param {Number} colIndex The column index
563 isFixed : function(colIndex){
564 return !!this.config[colIndex].fixed;
567 <div id="method-Ext.grid.ColumnModel-isResizable"></div>/**
568 * Returns true if the column can be resized
571 isResizable : function(colIndex){
572 return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
574 <div id="method-Ext.grid.ColumnModel-setHidden"></div>/**
575 * Sets if a column is hidden.
577 myGrid.getColumnModel().setHidden(0, true); // hide column 0 (0 = the first column).
579 * @param {Number} colIndex The column index
580 * @param {Boolean} hidden True if the column is hidden
582 setHidden : function(colIndex, hidden){
583 var c = this.config[colIndex];
584 if(c.hidden !== hidden){
586 this.totalWidth = null;
587 this.fireEvent("hiddenchange", this, colIndex, hidden);
591 <div id="method-Ext.grid.ColumnModel-setEditor"></div>/**
592 * Sets the editor for a column and destroys the prior editor.
593 * @param {Number} col The column index
594 * @param {Object} editor The editor object
596 setEditor : function(col, editor){
597 this.config[col].setEditor(editor);
600 <div id="method-Ext.grid.ColumnModel-destroy"></div>/**
601 * Destroys this column model by purging any event listeners, and removing any editors.
603 destroy : function(){
605 for(var i = 0, len = this.config.length; i < len; i++){
611 this.purgeListeners();
616 Ext.grid.ColumnModel.defaultRenderer = function(value){
617 if(typeof value == "string" && value.length < 1){