3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 * @class Ext.grid.ColumnModel
9 * @extends Ext.util.Observable
10 * <p>After the data has been read into the client side cache (<b>{@link Ext.data.Store Store}</b>),
11 * the ColumnModel is used to configure how and what parts of that data will be displayed in the
12 * vertical slices (columns) of the grid. The Ext.grid.ColumnModel Class is the default implementation
13 * of a ColumnModel used by implentations of {@link Ext.grid.GridPanel GridPanel}.</p>
14 * <p>Data is mapped into the store's records and then indexed into the ColumnModel using the
15 * <tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt>:</p>
17 {data source} == mapping ==> {data store} == <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b> ==> {ColumnModel}
19 * <p>Each {@link Ext.grid.Column Column} in the grid's ColumnModel is configured with a
20 * <tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt> to specify how the data within
21 * each record in the store is indexed into the ColumnModel.</p>
22 * <p>There are two ways to initialize the ColumnModel class:</p>
23 * <p><u>Initialization Method 1: an Array</u></p>
25 var colModel = new Ext.grid.ColumnModel([
26 { header: "Ticker", width: 60, sortable: true},
27 { header: "Company Name", width: 150, sortable: true, id: 'company'},
28 { header: "Market Cap.", width: 100, sortable: true},
29 { header: "$ Sales", width: 100, sortable: true, renderer: money},
30 { header: "Employees", width: 100, sortable: true, resizable: false}
33 * <p>The ColumnModel may be initialized with an Array of {@link Ext.grid.Column} column configuration
34 * objects to define the initial layout / display of the columns in the Grid. The order of each
35 * {@link Ext.grid.Column} column configuration object within the specified Array defines the initial
36 * order of the column display. A Column's display may be initially hidden using the
37 * <tt>{@link Ext.grid.Column#hidden hidden}</tt></b> config property (and then shown using the column
38 * header menu). Fields that are not included in the ColumnModel will not be displayable at all.</p>
39 * <p>How each column in the grid correlates (maps) to the {@link Ext.data.Record} field in the
40 * {@link Ext.data.Store Store} the column draws its data from is configured through the
41 * <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b>. If the
42 * <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b> is not explicitly defined (as shown in the
43 * example above) it will use the column configuration's index in the Array as the index.</p>
44 * <p>See <b><tt>{@link Ext.grid.Column}</tt></b> for additional configuration options for each column.</p>
45 * <p><u>Initialization Method 2: an Object</u></p>
46 * <p>In order to use configuration options from <tt>Ext.grid.ColumnModel</tt>, an Object may be used to
47 * initialize the ColumnModel. The column configuration Array will be specified in the <tt><b>{@link #columns}</b></tt>
48 * config property. The <tt><b>{@link #defaults}</b></tt> config property can be used to apply defaults
49 * for all columns, e.g.:</p><pre><code>
50 var colModel = new Ext.grid.ColumnModel({
52 { header: "Ticker", width: 60, menuDisabled: false},
53 { header: "Company Name", width: 150, id: 'company'},
54 { header: "Market Cap."},
55 { header: "$ Sales", renderer: money},
56 { header: "Employees", resizable: false}
64 {@link #hiddenchange}: function(cm, colIndex, hidden) {
65 saveConfig(colIndex, hidden);
70 * <p>In both examples above, the ability to apply a CSS class to all cells in a column (including the
71 * header) is demonstrated through the use of the <b><tt>{@link Ext.grid.Column#id id}</tt></b> config
72 * option. This column could be styled by including the following css:</p><pre><code>
73 //add this css *after* the core css is loaded
75 color: red; // entire column will have red font
77 // modify the header row only, adding an icon to the column header
79 background: transparent
80 url(../../resources/images/icons/silk/building.png)
81 no-repeat 3px 3px ! important;
85 * Note that the "Company Name" column could be specified as the
86 * <b><tt>{@link Ext.grid.GridPanel}.{@link Ext.grid.GridPanel#autoExpandColumn autoExpandColumn}</tt></b>.
88 * @param {Mixed} config Specify either an Array of {@link Ext.grid.Column} configuration objects or specify
89 * a configuration Object (see introductory section discussion utilizing Initialization Method 2 above).
91 Ext.grid.ColumnModel = Ext.extend(Ext.util.Observable, {
93 * @cfg {Number} defaultWidth (optional) The width of columns which have no <tt>{@link #width}</tt>
94 * specified (defaults to <tt>100</tt>). This property shall preferably be configured through the
95 * <tt><b>{@link #defaults}</b></tt> config property.
99 * @cfg {Boolean} defaultSortable (optional) Default sortable of columns which have no
100 * sortable specified (defaults to <tt>false</tt>). This property shall preferably be configured
101 * through the <tt><b>{@link #defaults}</b></tt> config property.
103 defaultSortable: false,
105 * @cfg {Array} columns An Array of object literals. The config options defined by
106 * <b>{@link Ext.grid.Column}</b> are the options which may appear in the object literal for each
107 * individual column definition.
110 * @cfg {Object} defaults Object literal which will be used to apply {@link Ext.grid.Column}
111 * configuration options to all <tt><b>{@link #columns}</b></tt>. Configuration options specified with
112 * individual {@link Ext.grid.Column column} configs will supersede these <tt><b>{@link #defaults}</b></tt>.
115 constructor : function(config){
117 * An Array of {@link Ext.grid.Column Column definition} objects representing the configuration
118 * of this ColumnModel. See {@link Ext.grid.Column} for the configuration properties that may
124 Ext.apply(this, config);
125 this.setConfig(config.columns, true);
127 this.setConfig(config, true);
132 * Fires when the width of a column is programmaticially changed using
133 * <code>{@link #setColumnWidth}</code>.
134 * Note internal resizing suppresses the event from firing. See also
135 * {@link Ext.grid.GridPanel}.<code>{@link #columnresize}</code>.
136 * @param {ColumnModel} this
137 * @param {Number} columnIndex The column index
138 * @param {Number} newWidth The new width
142 * @event headerchange
143 * Fires when the text of a header changes.
144 * @param {ColumnModel} this
145 * @param {Number} columnIndex The column index
146 * @param {String} newText The new header text
150 * @event hiddenchange
151 * Fires when a column is hidden or "unhidden".
152 * @param {ColumnModel} this
153 * @param {Number} columnIndex The column index
154 * @param {Boolean} hidden true if hidden, false otherwise
159 * Fires when a column is moved.
160 * @param {ColumnModel} this
161 * @param {Number} oldIndex
162 * @param {Number} newIndex
166 * @event configchange
167 * Fires when the configuration is changed
168 * @param {ColumnModel} this
172 Ext.grid.ColumnModel.superclass.constructor.call(this);
176 * Returns the id of the column at the specified index.
177 * @param {Number} index The column index
178 * @return {String} the id
180 getColumnId : function(index){
181 return this.config[index].id;
184 getColumnAt : function(index){
185 return this.config[index];
189 * <p>Reconfigures this column model according to the passed Array of column definition objects.
190 * For a description of the individual properties of a column definition object, see the
191 * <a href="#Ext.grid.ColumnModel-configs">Config Options</a>.</p>
192 * <p>Causes the {@link #configchange} event to be fired. A {@link Ext.grid.GridPanel GridPanel}
193 * using this ColumnModel will listen for this event and refresh its UI automatically.</p>
194 * @param {Array} config Array of Column definition objects.
195 * @param {Boolean} initial Specify <tt>true</tt> to bypass cleanup which deletes the <tt>totalWidth</tt>
196 * and destroys existing editors.
198 setConfig : function(config, initial){
200 if(!initial){ // cleanup
201 delete this.totalWidth;
202 for(i = 0, len = this.config.length; i < len; i++){
205 //check here, in case we have a special column like a CheckboxSelectionModel
211 // backward compatibility
212 this.defaults = Ext.apply({
213 width: this.defaultWidth,
214 sortable: this.defaultSortable
217 this.config = config;
220 for(i = 0, len = config.length; i < len; i++){
221 c = Ext.applyIf(config[i], this.defaults);
222 // if no id, create one using column's ordinal position
223 if(Ext.isEmpty(c.id)){
227 var Cls = Ext.grid.Column.types[c.xtype || 'gridcolumn'];
231 this.lookup[c.id] = c;
234 this.fireEvent('configchange', this);
239 * Returns the column for a specified id.
240 * @param {String} id The column id
241 * @return {Object} the column
243 getColumnById : function(id){
244 return this.lookup[id];
248 * Returns the index for a specified column id.
249 * @param {String} id The column id
250 * @return {Number} the index, or -1 if not found
252 getIndexById : function(id){
253 for(var i = 0, len = this.config.length; i < len; i++){
254 if(this.config[i].id == id){
262 * Moves a column from one position to another.
263 * @param {Number} oldIndex The index of the column to move.
264 * @param {Number} newIndex The position at which to reinsert the coolumn.
266 moveColumn : function(oldIndex, newIndex){
267 var c = this.config[oldIndex];
268 this.config.splice(oldIndex, 1);
269 this.config.splice(newIndex, 0, c);
271 this.fireEvent("columnmoved", this, oldIndex, newIndex);
275 * Returns the number of columns.
276 * @param {Boolean} visibleOnly Optional. Pass as true to only include visible columns.
279 getColumnCount : function(visibleOnly){
280 if(visibleOnly === true){
282 for(var i = 0, len = this.config.length; i < len; i++){
283 if(!this.isHidden(i)){
289 return this.config.length;
293 * Returns the column configs that return true by the passed function that is called
294 * with (columnConfig, index)
296 // returns an array of column config objects for all hidden columns
297 var columns = grid.getColumnModel().getColumnsBy(function(c){
301 * @param {Function} fn A function which, when passed a {@link Ext.grid.Column Column} object, must
302 * return <code>true</code> if the column is to be included in the returned Array.
303 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function
304 * is executed. Defaults to this ColumnModel.
305 * @return {Array} result
307 getColumnsBy : function(fn, scope){
309 for(var i = 0, len = this.config.length; i < len; i++){
310 var c = this.config[i];
311 if(fn.call(scope||this, c, i) === true){
319 * Returns true if the specified column is sortable.
320 * @param {Number} col The column index
323 isSortable : function(col){
324 return !!this.config[col].sortable;
328 * Returns true if the specified column menu is disabled.
329 * @param {Number} col The column index
332 isMenuDisabled : function(col){
333 return !!this.config[col].menuDisabled;
337 * Returns the rendering (formatting) function defined for the column.
338 * @param {Number} col The column index.
339 * @return {Function} The function used to render the cell. See {@link #setRenderer}.
341 getRenderer : function(col){
342 if(!this.config[col].renderer){
343 return Ext.grid.ColumnModel.defaultRenderer;
345 return this.config[col].renderer;
348 getRendererScope : function(col){
349 return this.config[col].scope;
353 * Sets the rendering (formatting) function for a column. See {@link Ext.util.Format} for some
354 * default formatting functions.
355 * @param {Number} col The column index
356 * @param {Function} fn The function to use to process the cell's raw data
357 * to return HTML markup for the grid view. The render function is called with
358 * the following parameters:<ul>
359 * <li><b>value</b> : Object<p class="sub-desc">The data value for the cell.</p></li>
360 * <li><b>metadata</b> : Object<p class="sub-desc">An object in which you may set the following attributes:<ul>
361 * <li><b>css</b> : String<p class="sub-desc">A CSS class name to add to the cell's TD element.</p></li>
362 * <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
363 * (e.g. 'style="color:red;"').</p></li></ul></p></li>
364 * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record} from which the data was extracted.</p></li>
365 * <li><b>rowIndex</b> : Number<p class="sub-desc">Row index</p></li>
366 * <li><b>colIndex</b> : Number<p class="sub-desc">Column index</p></li>
367 * <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>
369 setRenderer : function(col, fn){
370 this.config[col].renderer = fn;
374 * Returns the width for the specified column.
375 * @param {Number} col The column index
378 getColumnWidth : function(col){
379 return this.config[col].width;
383 * Sets the width for a column.
384 * @param {Number} col The column index
385 * @param {Number} width The new width
386 * @param {Boolean} suppressEvent True to suppress firing the <code>{@link #widthchange}</code>
387 * event. Defaults to false.
389 setColumnWidth : function(col, width, suppressEvent){
390 this.config[col].width = width;
391 this.totalWidth = null;
393 this.fireEvent("widthchange", this, col, width);
398 * Returns the total width of all columns.
399 * @param {Boolean} includeHidden True to include hidden column widths
402 getTotalWidth : function(includeHidden){
403 if(!this.totalWidth){
405 for(var i = 0, len = this.config.length; i < len; i++){
406 if(includeHidden || !this.isHidden(i)){
407 this.totalWidth += this.getColumnWidth(i);
411 return this.totalWidth;
415 * Returns the header for the specified column.
416 * @param {Number} col The column index
419 getColumnHeader : function(col){
420 return this.config[col].header;
424 * Sets the header for a column.
425 * @param {Number} col The column index
426 * @param {String} header The new header
428 setColumnHeader : function(col, header){
429 this.config[col].header = header;
430 this.fireEvent("headerchange", this, col, header);
434 * Returns the tooltip for the specified column.
435 * @param {Number} col The column index
438 getColumnTooltip : function(col){
439 return this.config[col].tooltip;
442 * Sets the tooltip for a column.
443 * @param {Number} col The column index
444 * @param {String} tooltip The new tooltip
446 setColumnTooltip : function(col, tooltip){
447 this.config[col].tooltip = tooltip;
451 * Returns the dataIndex for the specified column.
453 // Get field name for the column
454 var fieldName = grid.getColumnModel().getDataIndex(columnIndex);
456 * @param {Number} col The column index
457 * @return {String} The column's dataIndex
459 getDataIndex : function(col){
460 return this.config[col].dataIndex;
464 * Sets the dataIndex for a column.
465 * @param {Number} col The column index
466 * @param {String} dataIndex The new dataIndex
468 setDataIndex : function(col, dataIndex){
469 this.config[col].dataIndex = dataIndex;
473 * Finds the index of the first matching column for the given dataIndex.
474 * @param {String} col The dataIndex to find
475 * @return {Number} The column index, or -1 if no match was found
477 findColumnIndex : function(dataIndex){
479 for(var i = 0, len = c.length; i < len; i++){
480 if(c[i].dataIndex == dataIndex){
488 * Returns true if the cell is editable.
490 var store = new Ext.data.Store({...});
491 var colModel = new Ext.grid.ColumnModel({
493 isCellEditable: function(col, row) {
494 var record = store.getAt(row);
495 if (record.get('readonly')) { // replace with your condition
498 return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, col, row);
501 var grid = new Ext.grid.GridPanel({
507 * @param {Number} colIndex The column index
508 * @param {Number} rowIndex The row index
511 isCellEditable : function(colIndex, rowIndex){
512 var c = this.config[colIndex],
516 return !!(ed || (!Ext.isDefined(ed) && c.editor));
520 * Returns the editor defined for the cell/column.
521 * @param {Number} colIndex The column index
522 * @param {Number} rowIndex The row index
523 * @return {Ext.Editor} The {@link Ext.Editor Editor} that was created to wrap
524 * the {@link Ext.form.Field Field} used to edit the cell.
526 getCellEditor : function(colIndex, rowIndex){
527 return this.config[colIndex].getCellEditor(rowIndex);
531 * Sets if a column is editable.
532 * @param {Number} col The column index
533 * @param {Boolean} editable True if the column is editable
535 setEditable : function(col, editable){
536 this.config[col].editable = editable;
540 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#hidden hidden}</code>,
541 * <tt>false</tt> otherwise.
542 * @param {Number} colIndex The column index
545 isHidden : function(colIndex){
546 return !!this.config[colIndex].hidden; // ensure returns boolean
550 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#fixed fixed}</code>,
551 * <tt>false</tt> otherwise.
552 * @param {Number} colIndex The column index
555 isFixed : function(colIndex){
556 return !!this.config[colIndex].fixed;
560 * Returns true if the column can be resized
563 isResizable : function(colIndex){
564 return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
567 * Sets if a column is hidden.
569 myGrid.getColumnModel().setHidden(0, true); // hide column 0 (0 = the first column).
571 * @param {Number} colIndex The column index
572 * @param {Boolean} hidden True if the column is hidden
574 setHidden : function(colIndex, hidden){
575 var c = this.config[colIndex];
576 if(c.hidden !== hidden){
578 this.totalWidth = null;
579 this.fireEvent("hiddenchange", this, colIndex, hidden);
584 * Sets the editor for a column and destroys the prior editor.
585 * @param {Number} col The column index
586 * @param {Object} editor The editor object
588 setEditor : function(col, editor){
589 this.config[col].setEditor(editor);
593 * Destroys this column model by purging any event listeners, and removing any editors.
595 destroy : function(){
597 for(var i = 0, len = this.config.length; i < len; i++){
603 this.purgeListeners();
608 Ext.grid.ColumnModel.defaultRenderer = function(value){
609 if(typeof value == "string" && value.length < 1){