3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.grid.ColumnModel"></div>/**
15 * @class Ext.grid.ColumnModel
16 * @extends Ext.util.Observable
17 * <p>After the data has been read into the client side cache (<b>{@link Ext.data.Store Store}</b>),
18 * the ColumnModel is used to configure how and what parts of that data will be displayed in the
19 * vertical slices (columns) of the grid. The Ext.grid.ColumnModel Class is the default implementation
20 * of a ColumnModel used by implentations of {@link Ext.grid.GridPanel GridPanel}.</p>
21 * <p>Data is mapped into the store's records and then indexed into the ColumnModel using the
22 * <tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt>:</p>
24 {data source} == mapping ==> {data store} == <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b> ==> {ColumnModel}
26 * <p>Each {@link Ext.grid.Column Column} in the grid's ColumnModel is configured with a
27 * <tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt> to specify how the data within
28 * each record in the store is indexed into the ColumnModel.</p>
29 * <p>There are two ways to initialize the ColumnModel class:</p>
30 * <p><u>Initialization Method 1: an Array</u></p>
32 var colModel = new Ext.grid.ColumnModel([
33 { header: "Ticker", width: 60, sortable: true},
34 { header: "Company Name", width: 150, sortable: true, id: 'company'},
35 { header: "Market Cap.", width: 100, sortable: true},
36 { header: "$ Sales", width: 100, sortable: true, renderer: money},
37 { header: "Employees", width: 100, sortable: true, resizable: false}
40 * <p>The ColumnModel may be initialized with an Array of {@link Ext.grid.Column} column configuration
41 * objects to define the initial layout / display of the columns in the Grid. The order of each
42 * {@link Ext.grid.Column} column configuration object within the specified Array defines the initial
43 * order of the column display. A Column's display may be initially hidden using the
44 * <tt>{@link Ext.grid.Column#hidden hidden}</tt></b> config property (and then shown using the column
45 * header menu). Fields that are not included in the ColumnModel will not be displayable at all.</p>
46 * <p>How each column in the grid correlates (maps) to the {@link Ext.data.Record} field in the
47 * {@link Ext.data.Store Store} the column draws its data from is configured through the
48 * <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b>. If the
49 * <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b> is not explicitly defined (as shown in the
50 * example above) it will use the column configuration's index in the Array as the index.</p>
51 * <p>See <b><tt>{@link Ext.grid.Column}</tt></b> for additional configuration options for each column.</p>
52 * <p><u>Initialization Method 2: an Object</u></p>
53 * <p>In order to use configuration options from <tt>Ext.grid.ColumnModel</tt>, an Object may be used to
54 * initialize the ColumnModel. The column configuration Array will be specified in the <tt><b>{@link #columns}</b></tt>
55 * config property. The <tt><b>{@link #defaults}</b></tt> config property can be used to apply defaults
56 * for all columns, e.g.:</p><pre><code>
57 var colModel = new Ext.grid.ColumnModel({
59 { header: "Ticker", width: 60, menuDisabled: false},
60 { header: "Company Name", width: 150, id: 'company'},
61 { header: "Market Cap."},
62 { header: "$ Sales", renderer: money},
63 { header: "Employees", resizable: false}
71 {@link #hiddenchange}: function(cm, colIndex, hidden) {
72 saveConfig(colIndex, hidden);
77 * <p>In both examples above, the ability to apply a CSS class to all cells in a column (including the
78 * header) is demonstrated through the use of the <b><tt>{@link Ext.grid.Column#id id}</tt></b> config
79 * option. This column could be styled by including the following css:</p><pre><code>
80 //add this css *after* the core css is loaded
82 color: red; // entire column will have red font
84 // modify the header row only, adding an icon to the column header
86 background: transparent
87 url(../../resources/images/icons/silk/building.png)
88 no-repeat 3px 3px ! important;
92 * Note that the "Company Name" column could be specified as the
93 * <b><tt>{@link Ext.grid.GridPanel}.{@link Ext.grid.GridPanel#autoExpandColumn autoExpandColumn}</tt></b>.
95 * @param {Mixed} config Specify either an Array of {@link Ext.grid.Column} configuration objects or specify
96 * a configuration Object (see introductory section discussion utilizing Initialization Method 2 above).
98 Ext.grid.ColumnModel = function(config){
99 <div id="prop-Ext.grid.ColumnModel-config"></div>/**
100 * An Array of {@link Ext.grid.Column Column definition} objects representing the configuration
101 * of this ColumnModel. See {@link Ext.grid.Column} for the configuration properties that may
107 Ext.apply(this, config);
108 this.setConfig(config.columns, true);
110 this.setConfig(config, true);
113 <div id="event-Ext.grid.ColumnModel-widthchange"></div>/**
115 * Fires when the width of a column is programmaticially changed using
116 * <code>{@link #setColumnWidth}</code>.
117 * Note internal resizing suppresses the event from firing. See also
118 * {@link Ext.grid.GridPanel}.<code>{@link #columnresize}</code>.
119 * @param {ColumnModel} this
120 * @param {Number} columnIndex The column index
121 * @param {Number} newWidth The new width
124 <div id="event-Ext.grid.ColumnModel-headerchange"></div>/**
125 * @event headerchange
126 * Fires when the text of a header changes.
127 * @param {ColumnModel} this
128 * @param {Number} columnIndex The column index
129 * @param {String} newText The new header text
132 <div id="event-Ext.grid.ColumnModel-hiddenchange"></div>/**
133 * @event hiddenchange
134 * Fires when a column is hidden or "unhidden".
135 * @param {ColumnModel} this
136 * @param {Number} columnIndex The column index
137 * @param {Boolean} hidden true if hidden, false otherwise
140 <div id="event-Ext.grid.ColumnModel-columnmoved"></div>/**
142 * Fires when a column is moved.
143 * @param {ColumnModel} this
144 * @param {Number} oldIndex
145 * @param {Number} newIndex
148 <div id="event-Ext.grid.ColumnModel-configchange"></div>/**
149 * @event configchange
150 * Fires when the configuration is changed
151 * @param {ColumnModel} this
155 Ext.grid.ColumnModel.superclass.constructor.call(this);
157 Ext.extend(Ext.grid.ColumnModel, Ext.util.Observable, {
158 <div id="cfg-Ext.grid.ColumnModel-defaultWidth"></div>/**
159 * @cfg {Number} defaultWidth (optional) The width of columns which have no <tt>{@link #width}</tt>
160 * specified (defaults to <tt>100</tt>). This property shall preferably be configured through the
161 * <tt><b>{@link #defaults}</b></tt> config property.
164 <div id="cfg-Ext.grid.ColumnModel-defaultSortable"></div>/**
165 * @cfg {Boolean} defaultSortable (optional) Default sortable of columns which have no
166 * sortable specified (defaults to <tt>false</tt>). This property shall preferably be configured
167 * through the <tt><b>{@link #defaults}</b></tt> config property.
169 defaultSortable: false,
170 <div id="cfg-Ext.grid.ColumnModel-columns"></div>/**
171 * @cfg {Array} columns An Array of object literals. The config options defined by
172 * <b>{@link Ext.grid.Column}</b> are the options which may appear in the object literal for each
173 * individual column definition.
175 <div id="cfg-Ext.grid.ColumnModel-defaults"></div>/**
176 * @cfg {Object} defaults Object literal which will be used to apply {@link Ext.grid.Column}
177 * configuration options to all <tt><b>{@link #columns}</b></tt>. Configuration options specified with
178 * individual {@link Ext.grid.Column column} configs will supersede these <tt><b>{@link #defaults}</b></tt>.
181 <div id="method-Ext.grid.ColumnModel-getColumnId"></div>/**
182 * Returns the id of the column at the specified index.
183 * @param {Number} index The column index
184 * @return {String} the id
186 getColumnId : function(index){
187 return this.config[index].id;
190 getColumnAt : function(index){
191 return this.config[index];
194 <div id="method-Ext.grid.ColumnModel-setConfig"></div>/**
195 * <p>Reconfigures this column model according to the passed Array of column definition objects.
196 * For a description of the individual properties of a column definition object, see the
197 * <a href="#Ext.grid.ColumnModel-configs">Config Options</a>.</p>
198 * <p>Causes the {@link #configchange} event to be fired. A {@link Ext.grid.GridPanel GridPanel}
199 * using this ColumnModel will listen for this event and refresh its UI automatically.</p>
200 * @param {Array} config Array of Column definition objects.
201 * @param {Boolean} initial Specify <tt>true</tt> to bypass cleanup which deletes the <tt>totalWidth</tt>
202 * and destroys existing editors.
204 setConfig : function(config, initial){
206 if(!initial){ // cleanup
207 delete this.totalWidth;
208 for(i = 0, len = this.config.length; i < len; i++){
216 // backward compatibility
217 this.defaults = Ext.apply({
218 width: this.defaultWidth,
219 sortable: this.defaultSortable
222 this.config = config;
225 for(i = 0, len = config.length; i < len; i++){
226 c = Ext.applyIf(config[i], this.defaults);
227 // if no id, create one using column's ordinal position
228 if(typeof c.id == 'undefined'){
232 var Cls = Ext.grid.Column.types[c.xtype || 'gridcolumn'];
236 this.lookup[c.id] = c;
239 this.fireEvent('configchange', this);
243 <div id="method-Ext.grid.ColumnModel-getColumnById"></div>/**
244 * Returns the column for a specified id.
245 * @param {String} id The column id
246 * @return {Object} the column
248 getColumnById : function(id){
249 return this.lookup[id];
252 <div id="method-Ext.grid.ColumnModel-getIndexById"></div>/**
253 * Returns the index for a specified column id.
254 * @param {String} id The column id
255 * @return {Number} the index, or -1 if not found
257 getIndexById : function(id){
258 for(var i = 0, len = this.config.length; i < len; i++){
259 if(this.config[i].id == id){
266 <div id="method-Ext.grid.ColumnModel-moveColumn"></div>/**
267 * Moves a column from one position to another.
268 * @param {Number} oldIndex The index of the column to move.
269 * @param {Number} newIndex The position at which to reinsert the coolumn.
271 moveColumn : function(oldIndex, newIndex){
272 var c = this.config[oldIndex];
273 this.config.splice(oldIndex, 1);
274 this.config.splice(newIndex, 0, c);
276 this.fireEvent("columnmoved", this, oldIndex, newIndex);
279 <div id="method-Ext.grid.ColumnModel-getColumnCount"></div>/**
280 * Returns the number of columns.
281 * @param {Boolean} visibleOnly Optional. Pass as true to only include visible columns.
284 getColumnCount : function(visibleOnly){
285 if(visibleOnly === true){
287 for(var i = 0, len = this.config.length; i < len; i++){
288 if(!this.isHidden(i)){
294 return this.config.length;
297 <div id="method-Ext.grid.ColumnModel-getColumnsBy"></div>/**
298 * Returns the column configs that return true by the passed function that is called
299 * with (columnConfig, index)
301 // returns an array of column config objects for all hidden columns
302 var columns = grid.getColumnModel().getColumnsBy(function(c){
306 * @param {Function} fn
307 * @param {Object} scope (optional)
308 * @return {Array} result
310 getColumnsBy : function(fn, scope){
312 for(var i = 0, len = this.config.length; i < len; i++){
313 var c = this.config[i];
314 if(fn.call(scope||this, c, i) === true){
321 <div id="method-Ext.grid.ColumnModel-isSortable"></div>/**
322 * Returns true if the specified column is sortable.
323 * @param {Number} col The column index
326 isSortable : function(col){
327 return !!this.config[col].sortable;
330 <div id="method-Ext.grid.ColumnModel-isMenuDisabled"></div>/**
331 * Returns true if the specified column menu is disabled.
332 * @param {Number} col The column index
335 isMenuDisabled : function(col){
336 return !!this.config[col].menuDisabled;
339 <div id="method-Ext.grid.ColumnModel-getRenderer"></div>/**
340 * Returns the rendering (formatting) function defined for the column.
341 * @param {Number} col The column index.
342 * @return {Function} The function used to render the cell. See {@link #setRenderer}.
344 getRenderer : function(col){
345 if(!this.config[col].renderer){
346 return Ext.grid.ColumnModel.defaultRenderer;
348 return this.config[col].renderer;
351 <div id="method-Ext.grid.ColumnModel-setRenderer"></div>/**
352 * Sets the rendering (formatting) function for a column. See {@link Ext.util.Format} for some
353 * default formatting functions.
354 * @param {Number} col The column index
355 * @param {Function} fn The function to use to process the cell's raw data
356 * to return HTML markup for the grid view. The render function is called with
357 * the following parameters:<ul>
358 * <li><b>value</b> : Object<p class="sub-desc">The data value for the cell.</p></li>
359 * <li><b>metadata</b> : Object<p class="sub-desc">An object in which you may set the following attributes:<ul>
360 * <li><b>css</b> : String<p class="sub-desc">A CSS class name to add to the cell's TD element.</p></li>
361 * <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
362 * (e.g. 'style="color:red;"').</p></li></ul></p></li>
363 * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record} from which the data was extracted.</p></li>
364 * <li><b>rowIndex</b> : Number<p class="sub-desc">Row index</p></li>
365 * <li><b>colIndex</b> : Number<p class="sub-desc">Column index</p></li>
366 * <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>
368 setRenderer : function(col, fn){
369 this.config[col].renderer = fn;
372 <div id="method-Ext.grid.ColumnModel-getColumnWidth"></div>/**
373 * Returns the width for the specified column.
374 * @param {Number} col The column index
377 getColumnWidth : function(col){
378 return this.config[col].width;
381 <div id="method-Ext.grid.ColumnModel-setColumnWidth"></div>/**
382 * Sets the width for a column.
383 * @param {Number} col The column index
384 * @param {Number} width The new width
385 * @param {Boolean} suppressEvent True to suppress firing the <code>{@link #widthchange}</code>
386 * event. Defaults to false.
388 setColumnWidth : function(col, width, suppressEvent){
389 this.config[col].width = width;
390 this.totalWidth = null;
392 this.fireEvent("widthchange", this, col, width);
396 <div id="method-Ext.grid.ColumnModel-getTotalWidth"></div>/**
397 * Returns the total width of all columns.
398 * @param {Boolean} includeHidden True to include hidden column widths
401 getTotalWidth : function(includeHidden){
402 if(!this.totalWidth){
404 for(var i = 0, len = this.config.length; i < len; i++){
405 if(includeHidden || !this.isHidden(i)){
406 this.totalWidth += this.getColumnWidth(i);
410 return this.totalWidth;
413 <div id="method-Ext.grid.ColumnModel-getColumnHeader"></div>/**
414 * Returns the header for the specified column.
415 * @param {Number} col The column index
418 getColumnHeader : function(col){
419 return this.config[col].header;
422 <div id="method-Ext.grid.ColumnModel-setColumnHeader"></div>/**
423 * Sets the header for a column.
424 * @param {Number} col The column index
425 * @param {String} header The new header
427 setColumnHeader : function(col, header){
428 this.config[col].header = header;
429 this.fireEvent("headerchange", this, col, header);
432 <div id="method-Ext.grid.ColumnModel-getColumnTooltip"></div>/**
433 * Returns the tooltip for the specified column.
434 * @param {Number} col The column index
437 getColumnTooltip : function(col){
438 return this.config[col].tooltip;
440 <div id="method-Ext.grid.ColumnModel-setColumnTooltip"></div>/**
441 * Sets the tooltip for a column.
442 * @param {Number} col The column index
443 * @param {String} tooltip The new tooltip
445 setColumnTooltip : function(col, tooltip){
446 this.config[col].tooltip = tooltip;
449 <div id="method-Ext.grid.ColumnModel-getDataIndex"></div>/**
450 * Returns the dataIndex for the specified column.
452 // Get field name for the column
453 var fieldName = grid.getColumnModel().getDataIndex(columnIndex);
455 * @param {Number} col The column index
456 * @return {String} The column's dataIndex
458 getDataIndex : function(col){
459 return this.config[col].dataIndex;
462 <div id="method-Ext.grid.ColumnModel-setDataIndex"></div>/**
463 * Sets the dataIndex for a column.
464 * @param {Number} col The column index
465 * @param {String} dataIndex The new dataIndex
467 setDataIndex : function(col, dataIndex){
468 this.config[col].dataIndex = dataIndex;
471 <div id="method-Ext.grid.ColumnModel-findColumnIndex"></div>/**
472 * Finds the index of the first matching column for the given dataIndex.
473 * @param {String} col The dataIndex to find
474 * @return {Number} The column index, or -1 if no match was found
476 findColumnIndex : function(dataIndex){
478 for(var i = 0, len = c.length; i < len; i++){
479 if(c[i].dataIndex == dataIndex){
486 <div id="method-Ext.grid.ColumnModel-isCellEditable"></div>/**
487 * Returns true if the cell is editable.
489 var store = new Ext.data.Store({...});
490 var colModel = new Ext.grid.ColumnModel({
492 isCellEditable: function(col, row) {
493 var record = store.getAt(row);
494 if (record.get('readonly')) { // replace with your condition
497 return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, col, row);
500 var grid = new Ext.grid.GridPanel({
506 * @param {Number} colIndex The column index
507 * @param {Number} rowIndex The row index
510 isCellEditable : function(colIndex, rowIndex){
511 return (this.config[colIndex].editable || (typeof this.config[colIndex].editable == "undefined" && this.config[colIndex].editor)) ? true : false;
514 <div id="method-Ext.grid.ColumnModel-getCellEditor"></div>/**
515 * Returns the editor defined for the cell/column.
516 * @param {Number} colIndex The column index
517 * @param {Number} rowIndex The row index
518 * @return {Ext.Editor} The {@link Ext.Editor Editor} that was created to wrap
519 * the {@link Ext.form.Field Field} used to edit the cell.
521 getCellEditor : function(colIndex, rowIndex){
522 return this.config[colIndex].getCellEditor(rowIndex);
525 <div id="method-Ext.grid.ColumnModel-setEditable"></div>/**
526 * Sets if a column is editable.
527 * @param {Number} col The column index
528 * @param {Boolean} editable True if the column is editable
530 setEditable : function(col, editable){
531 this.config[col].editable = editable;
534 <div id="method-Ext.grid.ColumnModel-isHidden"></div>/**
535 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#hidden hidden}</code>,
536 * <tt>false</tt> otherwise.
537 * @param {Number} colIndex The column index
540 isHidden : function(colIndex){
541 return !!this.config[colIndex].hidden; // ensure returns boolean
544 <div id="method-Ext.grid.ColumnModel-isFixed"></div>/**
545 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#fixed fixed}</code>,
546 * <tt>false</tt> otherwise.
547 * @param {Number} colIndex The column index
550 isFixed : function(colIndex){
551 return !!this.config[colIndex].fixed;
554 <div id="method-Ext.grid.ColumnModel-isResizable"></div>/**
555 * Returns true if the column can be resized
558 isResizable : function(colIndex){
559 return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
561 <div id="method-Ext.grid.ColumnModel-setHidden"></div>/**
562 * Sets if a column is hidden.
564 myGrid.getColumnModel().setHidden(0, true); // hide column 0 (0 = the first column).
566 * @param {Number} colIndex The column index
567 * @param {Boolean} hidden True if the column is hidden
569 setHidden : function(colIndex, hidden){
570 var c = this.config[colIndex];
571 if(c.hidden !== hidden){
573 this.totalWidth = null;
574 this.fireEvent("hiddenchange", this, colIndex, hidden);
578 <div id="method-Ext.grid.ColumnModel-setEditor"></div>/**
579 * Sets the editor for a column and destroys the prior editor.
580 * @param {Number} col The column index
581 * @param {Object} editor The editor object
583 setEditor : function(col, editor){
584 Ext.destroy(this.config[col].editor);
585 this.config[col].editor = editor;
588 <div id="method-Ext.grid.ColumnModel-destroy"></div>/**
589 * Destroys this column model by purging any event listeners, and removing any editors.
591 destroy : function(){
592 for(var i = 0, c = this.config, len = c.length; i < len; i++){
593 Ext.destroy(c[i].editor);
595 this.purgeListeners();
600 Ext.grid.ColumnModel.defaultRenderer = function(value){
601 if(typeof value == "string" && value.length < 1){