3 * Copyright(c) 2006-2009 Ext JS, LLC
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 = function(config){
93 * An Array of {@link Ext.grid.Column Column definition} objects representing the configuration
94 * of this ColumnModel. See {@link Ext.grid.Column} for the configuration properties that may
100 Ext.apply(this, config);
101 this.setConfig(config.columns, true);
103 this.setConfig(config, true);
108 * Fires when the width of a column is programmaticially changed using
109 * <code>{@link #setColumnWidth}</code>.
110 * Note internal resizing suppresses the event from firing. See also
111 * {@link Ext.grid.GridPanel}.<code>{@link #columnresize}</code>.
112 * @param {ColumnModel} this
113 * @param {Number} columnIndex The column index
114 * @param {Number} newWidth The new width
118 * @event headerchange
119 * Fires when the text of a header changes.
120 * @param {ColumnModel} this
121 * @param {Number} columnIndex The column index
122 * @param {String} newText The new header text
126 * @event hiddenchange
127 * Fires when a column is hidden or "unhidden".
128 * @param {ColumnModel} this
129 * @param {Number} columnIndex The column index
130 * @param {Boolean} hidden true if hidden, false otherwise
135 * Fires when a column is moved.
136 * @param {ColumnModel} this
137 * @param {Number} oldIndex
138 * @param {Number} newIndex
142 * @event configchange
143 * Fires when the configuration is changed
144 * @param {ColumnModel} this
148 Ext.grid.ColumnModel.superclass.constructor.call(this);
150 Ext.extend(Ext.grid.ColumnModel, Ext.util.Observable, {
152 * @cfg {Number} defaultWidth (optional) The width of columns which have no <tt>{@link #width}</tt>
153 * specified (defaults to <tt>100</tt>). This property shall preferably be configured through the
154 * <tt><b>{@link #defaults}</b></tt> config property.
158 * @cfg {Boolean} defaultSortable (optional) Default sortable of columns which have no
159 * sortable specified (defaults to <tt>false</tt>). This property shall preferably be configured
160 * through the <tt><b>{@link #defaults}</b></tt> config property.
162 defaultSortable: false,
164 * @cfg {Array} columns An Array of object literals. The config options defined by
165 * <b>{@link Ext.grid.Column}</b> are the options which may appear in the object literal for each
166 * individual column definition.
169 * @cfg {Object} defaults Object literal which will be used to apply {@link Ext.grid.Column}
170 * configuration options to all <tt><b>{@link #columns}</b></tt>. Configuration options specified with
171 * individual {@link Ext.grid.Column column} configs will supersede these <tt><b>{@link #defaults}</b></tt>.
175 * Returns the id of the column at the specified index.
176 * @param {Number} index The column index
177 * @return {String} the id
179 getColumnId : function(index){
180 return this.config[index].id;
183 getColumnAt : function(index){
184 return this.config[index];
188 * <p>Reconfigures this column model according to the passed Array of column definition objects.
189 * For a description of the individual properties of a column definition object, see the
190 * <a href="#Ext.grid.ColumnModel-configs">Config Options</a>.</p>
191 * <p>Causes the {@link #configchange} event to be fired. A {@link Ext.grid.GridPanel GridPanel}
192 * using this ColumnModel will listen for this event and refresh its UI automatically.</p>
193 * @param {Array} config Array of Column definition objects.
194 * @param {Boolean} initial Specify <tt>true</tt> to bypass cleanup which deletes the <tt>totalWidth</tt>
195 * and destroys existing editors.
197 setConfig : function(config, initial){
199 if(!initial){ // cleanup
200 delete this.totalWidth;
201 for(i = 0, len = this.config.length; i < len; i++){
209 // backward compatibility
210 this.defaults = Ext.apply({
211 width: this.defaultWidth,
212 sortable: this.defaultSortable
215 this.config = config;
218 for(i = 0, len = config.length; i < len; i++){
219 c = Ext.applyIf(config[i], this.defaults);
220 // if no id, create one using column's ordinal position
221 if(typeof c.id == 'undefined'){
225 var Cls = Ext.grid.Column.types[c.xtype || 'gridcolumn'];
229 this.lookup[c.id] = c;
232 this.fireEvent('configchange', this);
237 * Returns the column for a specified id.
238 * @param {String} id The column id
239 * @return {Object} the column
241 getColumnById : function(id){
242 return this.lookup[id];
246 * Returns the index for a specified column id.
247 * @param {String} id The column id
248 * @return {Number} the index, or -1 if not found
250 getIndexById : function(id){
251 for(var i = 0, len = this.config.length; i < len; i++){
252 if(this.config[i].id == id){
260 * Moves a column from one position to another.
261 * @param {Number} oldIndex The index of the column to move.
262 * @param {Number} newIndex The position at which to reinsert the coolumn.
264 moveColumn : function(oldIndex, newIndex){
265 var c = this.config[oldIndex];
266 this.config.splice(oldIndex, 1);
267 this.config.splice(newIndex, 0, c);
269 this.fireEvent("columnmoved", this, oldIndex, newIndex);
273 * Returns the number of columns.
274 * @param {Boolean} visibleOnly Optional. Pass as true to only include visible columns.
277 getColumnCount : function(visibleOnly){
278 if(visibleOnly === true){
280 for(var i = 0, len = this.config.length; i < len; i++){
281 if(!this.isHidden(i)){
287 return this.config.length;
291 * Returns the column configs that return true by the passed function that is called
292 * with (columnConfig, index)
294 // returns an array of column config objects for all hidden columns
295 var columns = grid.getColumnModel().getColumnsBy(function(c){
299 * @param {Function} fn
300 * @param {Object} scope (optional)
301 * @return {Array} result
303 getColumnsBy : function(fn, scope){
305 for(var i = 0, len = this.config.length; i < len; i++){
306 var c = this.config[i];
307 if(fn.call(scope||this, c, i) === true){
315 * Returns true if the specified column is sortable.
316 * @param {Number} col The column index
319 isSortable : function(col){
320 return !!this.config[col].sortable;
324 * Returns true if the specified column menu is disabled.
325 * @param {Number} col The column index
328 isMenuDisabled : function(col){
329 return !!this.config[col].menuDisabled;
333 * Returns the rendering (formatting) function defined for the column.
334 * @param {Number} col The column index.
335 * @return {Function} The function used to render the cell. See {@link #setRenderer}.
337 getRenderer : function(col){
338 if(!this.config[col].renderer){
339 return Ext.grid.ColumnModel.defaultRenderer;
341 return this.config[col].renderer;
345 * Sets the rendering (formatting) function for a column. See {@link Ext.util.Format} for some
346 * default formatting functions.
347 * @param {Number} col The column index
348 * @param {Function} fn The function to use to process the cell's raw data
349 * to return HTML markup for the grid view. The render function is called with
350 * the following parameters:<ul>
351 * <li><b>value</b> : Object<p class="sub-desc">The data value for the cell.</p></li>
352 * <li><b>metadata</b> : Object<p class="sub-desc">An object in which you may set the following attributes:<ul>
353 * <li><b>css</b> : String<p class="sub-desc">A CSS class name to add to the cell's TD element.</p></li>
354 * <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
355 * (e.g. 'style="color:red;"').</p></li></ul></p></li>
356 * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record} from which the data was extracted.</p></li>
357 * <li><b>rowIndex</b> : Number<p class="sub-desc">Row index</p></li>
358 * <li><b>colIndex</b> : Number<p class="sub-desc">Column index</p></li>
359 * <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>
361 setRenderer : function(col, fn){
362 this.config[col].renderer = fn;
366 * Returns the width for the specified column.
367 * @param {Number} col The column index
370 getColumnWidth : function(col){
371 return this.config[col].width;
375 * Sets the width for a column.
376 * @param {Number} col The column index
377 * @param {Number} width The new width
378 * @param {Boolean} suppressEvent True to suppress firing the <code>{@link #widthchange}</code>
379 * event. Defaults to false.
381 setColumnWidth : function(col, width, suppressEvent){
382 this.config[col].width = width;
383 this.totalWidth = null;
385 this.fireEvent("widthchange", this, col, width);
390 * Returns the total width of all columns.
391 * @param {Boolean} includeHidden True to include hidden column widths
394 getTotalWidth : function(includeHidden){
395 if(!this.totalWidth){
397 for(var i = 0, len = this.config.length; i < len; i++){
398 if(includeHidden || !this.isHidden(i)){
399 this.totalWidth += this.getColumnWidth(i);
403 return this.totalWidth;
407 * Returns the header for the specified column.
408 * @param {Number} col The column index
411 getColumnHeader : function(col){
412 return this.config[col].header;
416 * Sets the header for a column.
417 * @param {Number} col The column index
418 * @param {String} header The new header
420 setColumnHeader : function(col, header){
421 this.config[col].header = header;
422 this.fireEvent("headerchange", this, col, header);
426 * Returns the tooltip for the specified column.
427 * @param {Number} col The column index
430 getColumnTooltip : function(col){
431 return this.config[col].tooltip;
434 * Sets the tooltip for a column.
435 * @param {Number} col The column index
436 * @param {String} tooltip The new tooltip
438 setColumnTooltip : function(col, tooltip){
439 this.config[col].tooltip = tooltip;
443 * Returns the dataIndex for the specified column.
445 // Get field name for the column
446 var fieldName = grid.getColumnModel().getDataIndex(columnIndex);
448 * @param {Number} col The column index
449 * @return {String} The column's dataIndex
451 getDataIndex : function(col){
452 return this.config[col].dataIndex;
456 * Sets the dataIndex for a column.
457 * @param {Number} col The column index
458 * @param {String} dataIndex The new dataIndex
460 setDataIndex : function(col, dataIndex){
461 this.config[col].dataIndex = dataIndex;
465 * Finds the index of the first matching column for the given dataIndex.
466 * @param {String} col The dataIndex to find
467 * @return {Number} The column index, or -1 if no match was found
469 findColumnIndex : function(dataIndex){
471 for(var i = 0, len = c.length; i < len; i++){
472 if(c[i].dataIndex == dataIndex){
480 * Returns true if the cell is editable.
482 var store = new Ext.data.Store({...});
483 var colModel = new Ext.grid.ColumnModel({
485 isCellEditable: function(col, row) {
486 var record = store.getAt(row);
487 if (record.get('readonly')) { // replace with your condition
490 return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, col, row);
493 var grid = new Ext.grid.GridPanel({
499 * @param {Number} colIndex The column index
500 * @param {Number} rowIndex The row index
503 isCellEditable : function(colIndex, rowIndex){
504 return (this.config[colIndex].editable || (typeof this.config[colIndex].editable == "undefined" && this.config[colIndex].editor)) ? true : false;
508 * Returns the editor defined for the cell/column.
509 * @param {Number} colIndex The column index
510 * @param {Number} rowIndex The row index
511 * @return {Ext.Editor} The {@link Ext.Editor Editor} that was created to wrap
512 * the {@link Ext.form.Field Field} used to edit the cell.
514 getCellEditor : function(colIndex, rowIndex){
515 return this.config[colIndex].getCellEditor(rowIndex);
519 * Sets if a column is editable.
520 * @param {Number} col The column index
521 * @param {Boolean} editable True if the column is editable
523 setEditable : function(col, editable){
524 this.config[col].editable = editable;
528 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#hidden hidden}</code>,
529 * <tt>false</tt> otherwise.
530 * @param {Number} colIndex The column index
533 isHidden : function(colIndex){
534 return !!this.config[colIndex].hidden; // ensure returns boolean
538 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#fixed fixed}</code>,
539 * <tt>false</tt> otherwise.
540 * @param {Number} colIndex The column index
543 isFixed : function(colIndex){
544 return !!this.config[colIndex].fixed;
548 * Returns true if the column can be resized
551 isResizable : function(colIndex){
552 return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
555 * Sets if a column is hidden.
557 myGrid.getColumnModel().setHidden(0, true); // hide column 0 (0 = the first column).
559 * @param {Number} colIndex The column index
560 * @param {Boolean} hidden True if the column is hidden
562 setHidden : function(colIndex, hidden){
563 var c = this.config[colIndex];
564 if(c.hidden !== hidden){
566 this.totalWidth = null;
567 this.fireEvent("hiddenchange", this, colIndex, hidden);
572 * Sets the editor for a column and destroys the prior editor.
573 * @param {Number} col The column index
574 * @param {Object} editor The editor object
576 setEditor : function(col, editor){
577 Ext.destroy(this.config[col].editor);
578 this.config[col].editor = editor;
582 * Destroys this column model by purging any event listeners, and removing any editors.
584 destroy : function(){
585 for(var i = 0, c = this.config, len = c.length; i < len; i++){
586 Ext.destroy(c[i].editor);
588 this.purgeListeners();
593 Ext.grid.ColumnModel.defaultRenderer = function(value){
594 if(typeof value == "string" && value.length < 1){