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.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.
107 <div id="cfg-Ext.grid.ColumnModel-defaultSortable"></div>/**
108 * @cfg {Boolean} defaultSortable (optional) Default sortable of columns which have no
109 * sortable specified (defaults to <tt>false</tt>). This property shall preferably be configured
110 * through the <tt><b>{@link #defaults}</b></tt> config property.
112 defaultSortable: false,
114 <div id="cfg-Ext.grid.ColumnModel-columns"></div>/**
115 * @cfg {Array} columns An Array of object literals. The config options defined by
116 * <b>{@link Ext.grid.Column}</b> are the options which may appear in the object literal for each
117 * individual column definition.
120 <div id="cfg-Ext.grid.ColumnModel-defaults"></div>/**
121 * @cfg {Object} defaults Object literal which will be used to apply {@link Ext.grid.Column}
122 * configuration options to all <tt><b>{@link #columns}</b></tt>. Configuration options specified with
123 * individual {@link Ext.grid.Column column} configs will supersede these <tt><b>{@link #defaults}</b></tt>.
126 constructor : function(config) {
127 <div id="prop-Ext.grid.ColumnModel-config"></div>/**
128 * An Array of {@link Ext.grid.Column Column definition} objects representing the configuration
129 * of this ColumnModel. See {@link Ext.grid.Column} for the configuration properties that may
134 if (config.columns) {
135 Ext.apply(this, config);
136 this.setConfig(config.columns, true);
138 this.setConfig(config, true);
142 <div id="event-Ext.grid.ColumnModel-widthchange"></div>/**
144 * Fires when the width of a column is programmaticially changed using
145 * <code>{@link #setColumnWidth}</code>.
146 * Note internal resizing suppresses the event from firing. See also
147 * {@link Ext.grid.GridPanel}.<code>{@link #columnresize}</code>.
148 * @param {ColumnModel} this
149 * @param {Number} columnIndex The column index
150 * @param {Number} newWidth The new width
154 <div id="event-Ext.grid.ColumnModel-headerchange"></div>/**
155 * @event headerchange
156 * Fires when the text of a header changes.
157 * @param {ColumnModel} this
158 * @param {Number} columnIndex The column index
159 * @param {String} newText The new header text
163 <div id="event-Ext.grid.ColumnModel-hiddenchange"></div>/**
164 * @event hiddenchange
165 * Fires when a column is hidden or "unhidden".
166 * @param {ColumnModel} this
167 * @param {Number} columnIndex The column index
168 * @param {Boolean} hidden true if hidden, false otherwise
172 <div id="event-Ext.grid.ColumnModel-columnmoved"></div>/**
174 * Fires when a column is moved.
175 * @param {ColumnModel} this
176 * @param {Number} oldIndex
177 * @param {Number} newIndex
181 <div id="event-Ext.grid.ColumnModel-configchange"></div>/**
182 * @event configchange
183 * Fires when the configuration is changed
184 * @param {ColumnModel} this
189 Ext.grid.ColumnModel.superclass.constructor.call(this);
192 <div id="method-Ext.grid.ColumnModel-getColumnId"></div>/**
193 * Returns the id of the column at the specified index.
194 * @param {Number} index The column index
195 * @return {String} the id
197 getColumnId : function(index) {
198 return this.config[index].id;
201 getColumnAt : function(index) {
202 return this.config[index];
205 <div id="method-Ext.grid.ColumnModel-setConfig"></div>/**
206 * <p>Reconfigures this column model according to the passed Array of column definition objects.
207 * For a description of the individual properties of a column definition object, see the
208 * <a href="#Ext.grid.ColumnModel-configs">Config Options</a>.</p>
209 * <p>Causes the {@link #configchange} event to be fired. A {@link Ext.grid.GridPanel GridPanel}
210 * using this ColumnModel will listen for this event and refresh its UI automatically.</p>
211 * @param {Array} config Array of Column definition objects.
212 * @param {Boolean} initial Specify <tt>true</tt> to bypass cleanup which deletes the <tt>totalWidth</tt>
213 * and destroys existing editors.
215 setConfig : function(config, initial) {
218 if (!initial) { // cleanup
219 delete this.totalWidth;
221 for (i = 0, len = this.config.length; i < len; i++) {
225 //check here, in case we have a special column like a CheckboxSelectionModel
231 // backward compatibility
232 this.defaults = Ext.apply({
233 width: this.defaultWidth,
234 sortable: this.defaultSortable
237 this.config = config;
240 for (i = 0, len = config.length; i < len; i++) {
241 c = Ext.applyIf(config[i], this.defaults);
243 // if no id, create one using column's ordinal position
244 if (Ext.isEmpty(c.id)) {
249 var Cls = Ext.grid.Column.types[c.xtype || 'gridcolumn'];
254 this.lookup[c.id] = c;
258 this.fireEvent('configchange', this);
262 <div id="method-Ext.grid.ColumnModel-getColumnById"></div>/**
263 * Returns the column for a specified id.
264 * @param {String} id The column id
265 * @return {Object} the column
267 getColumnById : function(id) {
268 return this.lookup[id];
271 <div id="method-Ext.grid.ColumnModel-getIndexById"></div>/**
272 * Returns the index for a specified column id.
273 * @param {String} id The column id
274 * @return {Number} the index, or -1 if not found
276 getIndexById : function(id) {
277 for (var i = 0, len = this.config.length; i < len; i++) {
278 if (this.config[i].id == id) {
285 <div id="method-Ext.grid.ColumnModel-moveColumn"></div>/**
286 * Moves a column from one position to another.
287 * @param {Number} oldIndex The index of the column to move.
288 * @param {Number} newIndex The position at which to reinsert the coolumn.
290 moveColumn : function(oldIndex, newIndex) {
291 var config = this.config,
292 c = config[oldIndex];
294 config.splice(oldIndex, 1);
295 config.splice(newIndex, 0, c);
297 this.fireEvent("columnmoved", this, oldIndex, newIndex);
300 <div id="method-Ext.grid.ColumnModel-getColumnCount"></div>/**
301 * Returns the number of columns.
302 * @param {Boolean} visibleOnly Optional. Pass as true to only include visible columns.
305 getColumnCount : function(visibleOnly) {
306 var length = this.config.length,
310 if (visibleOnly === true) {
311 for (i = 0; i < length; i++) {
312 if (!this.isHidden(i)) {
323 <div id="method-Ext.grid.ColumnModel-getColumnsBy"></div>/**
324 * Returns the column configs that return true by the passed function that is called
325 * with (columnConfig, index)
327 // returns an array of column config objects for all hidden columns
328 var columns = grid.getColumnModel().getColumnsBy(function(c){
332 * @param {Function} fn A function which, when passed a {@link Ext.grid.Column Column} object, must
333 * return <code>true</code> if the column is to be included in the returned Array.
334 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function
335 * is executed. Defaults to this ColumnModel.
336 * @return {Array} result
338 getColumnsBy : function(fn, scope) {
339 var config = this.config,
340 length = config.length,
344 for (i = 0; i < length; i++){
347 if (fn.call(scope || this, c, i) === true) {
348 result[result.length] = c;
355 <div id="method-Ext.grid.ColumnModel-isSortable"></div>/**
356 * Returns true if the specified column is sortable.
357 * @param {Number} col The column index
360 isSortable : function(col) {
361 return !!this.config[col].sortable;
364 <div id="method-Ext.grid.ColumnModel-isMenuDisabled"></div>/**
365 * Returns true if the specified column menu is disabled.
366 * @param {Number} col The column index
369 isMenuDisabled : function(col) {
370 return !!this.config[col].menuDisabled;
373 <div id="method-Ext.grid.ColumnModel-getRenderer"></div>/**
374 * Returns the rendering (formatting) function defined for the column.
375 * @param {Number} col The column index.
376 * @return {Function} The function used to render the cell. See {@link #setRenderer}.
378 getRenderer : function(col) {
379 return this.config[col].renderer || Ext.grid.ColumnModel.defaultRenderer;
382 getRendererScope : function(col) {
383 return this.config[col].scope;
386 <div id="method-Ext.grid.ColumnModel-setRenderer"></div>/**
387 * Sets the rendering (formatting) function for a column. See {@link Ext.util.Format} for some
388 * default formatting functions.
389 * @param {Number} col The column index
390 * @param {Function} fn The function to use to process the cell's raw data
391 * to return HTML markup for the grid view. The render function is called with
392 * the following parameters:<ul>
393 * <li><b>value</b> : Object<p class="sub-desc">The data value for the cell.</p></li>
394 * <li><b>metadata</b> : Object<p class="sub-desc">An object in which you may set the following attributes:<ul>
395 * <li><b>css</b> : String<p class="sub-desc">A CSS class name to add to the cell's TD element.</p></li>
396 * <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
397 * (e.g. 'style="color:red;"').</p></li></ul></p></li>
398 * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record} from which the data was extracted.</p></li>
399 * <li><b>rowIndex</b> : Number<p class="sub-desc">Row index</p></li>
400 * <li><b>colIndex</b> : Number<p class="sub-desc">Column index</p></li>
401 * <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>
403 setRenderer : function(col, fn) {
404 this.config[col].renderer = fn;
407 <div id="method-Ext.grid.ColumnModel-getColumnWidth"></div>/**
408 * Returns the width for the specified column.
409 * @param {Number} col The column index
412 getColumnWidth : function(col) {
413 var width = this.config[col].width;
414 if(typeof width != 'number'){
415 width = this.defaultWidth;
420 <div id="method-Ext.grid.ColumnModel-setColumnWidth"></div>/**
421 * Sets the width for a column.
422 * @param {Number} col The column index
423 * @param {Number} width The new width
424 * @param {Boolean} suppressEvent True to suppress firing the <code>{@link #widthchange}</code>
425 * event. Defaults to false.
427 setColumnWidth : function(col, width, suppressEvent) {
428 this.config[col].width = width;
429 this.totalWidth = null;
431 if (!suppressEvent) {
432 this.fireEvent("widthchange", this, col, width);
436 <div id="method-Ext.grid.ColumnModel-getTotalWidth"></div>/**
437 * Returns the total width of all columns.
438 * @param {Boolean} includeHidden True to include hidden column widths
441 getTotalWidth : function(includeHidden) {
442 if (!this.totalWidth) {
444 for (var i = 0, len = this.config.length; i < len; i++) {
445 if (includeHidden || !this.isHidden(i)) {
446 this.totalWidth += this.getColumnWidth(i);
450 return this.totalWidth;
453 <div id="method-Ext.grid.ColumnModel-getColumnHeader"></div>/**
454 * Returns the header for the specified column.
455 * @param {Number} col The column index
458 getColumnHeader : function(col) {
459 return this.config[col].header;
462 <div id="method-Ext.grid.ColumnModel-setColumnHeader"></div>/**
463 * Sets the header for a column.
464 * @param {Number} col The column index
465 * @param {String} header The new header
467 setColumnHeader : function(col, header) {
468 this.config[col].header = header;
469 this.fireEvent("headerchange", this, col, header);
472 <div id="method-Ext.grid.ColumnModel-getColumnTooltip"></div>/**
473 * Returns the tooltip for the specified column.
474 * @param {Number} col The column index
477 getColumnTooltip : function(col) {
478 return this.config[col].tooltip;
480 <div id="method-Ext.grid.ColumnModel-setColumnTooltip"></div>/**
481 * Sets the tooltip for a column.
482 * @param {Number} col The column index
483 * @param {String} tooltip The new tooltip
485 setColumnTooltip : function(col, tooltip) {
486 this.config[col].tooltip = tooltip;
489 <div id="method-Ext.grid.ColumnModel-getDataIndex"></div>/**
490 * Returns the dataIndex for the specified column.
492 // Get field name for the column
493 var fieldName = grid.getColumnModel().getDataIndex(columnIndex);
495 * @param {Number} col The column index
496 * @return {String} The column's dataIndex
498 getDataIndex : function(col) {
499 return this.config[col].dataIndex;
502 <div id="method-Ext.grid.ColumnModel-setDataIndex"></div>/**
503 * Sets the dataIndex for a column.
504 * @param {Number} col The column index
505 * @param {String} dataIndex The new dataIndex
507 setDataIndex : function(col, dataIndex) {
508 this.config[col].dataIndex = dataIndex;
511 <div id="method-Ext.grid.ColumnModel-findColumnIndex"></div>/**
512 * Finds the index of the first matching column for the given dataIndex.
513 * @param {String} col The dataIndex to find
514 * @return {Number} The column index, or -1 if no match was found
516 findColumnIndex : function(dataIndex) {
518 for(var i = 0, len = c.length; i < len; i++){
519 if(c[i].dataIndex == dataIndex){
526 <div id="method-Ext.grid.ColumnModel-isCellEditable"></div>/**
527 * Returns true if the cell is editable.
529 var store = new Ext.data.Store({...});
530 var colModel = new Ext.grid.ColumnModel({
532 isCellEditable: function(col, row) {
533 var record = store.getAt(row);
534 if (record.get('readonly')) { // replace with your condition
537 return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, col, row);
540 var grid = new Ext.grid.GridPanel({
546 * @param {Number} colIndex The column index
547 * @param {Number} rowIndex The row index
550 isCellEditable : function(colIndex, rowIndex) {
551 var c = this.config[colIndex],
555 return !!(ed || (!Ext.isDefined(ed) && c.editor));
558 <div id="method-Ext.grid.ColumnModel-getCellEditor"></div>/**
559 * Returns the editor defined for the cell/column.
560 * @param {Number} colIndex The column index
561 * @param {Number} rowIndex The row index
562 * @return {Ext.Editor} The {@link Ext.Editor Editor} that was created to wrap
563 * the {@link Ext.form.Field Field} used to edit the cell.
565 getCellEditor : function(colIndex, rowIndex) {
566 return this.config[colIndex].getCellEditor(rowIndex);
569 <div id="method-Ext.grid.ColumnModel-setEditable"></div>/**
570 * Sets if a column is editable.
571 * @param {Number} col The column index
572 * @param {Boolean} editable True if the column is editable
574 setEditable : function(col, editable) {
575 this.config[col].editable = editable;
578 <div id="method-Ext.grid.ColumnModel-isHidden"></div>/**
579 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#hidden hidden}</code>,
580 * <tt>false</tt> otherwise.
581 * @param {Number} colIndex The column index
584 isHidden : function(colIndex) {
585 return !!this.config[colIndex].hidden; // ensure returns boolean
588 <div id="method-Ext.grid.ColumnModel-isFixed"></div>/**
589 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#fixed fixed}</code>,
590 * <tt>false</tt> otherwise.
591 * @param {Number} colIndex The column index
594 isFixed : function(colIndex) {
595 return !!this.config[colIndex].fixed;
598 <div id="method-Ext.grid.ColumnModel-isResizable"></div>/**
599 * Returns true if the column can be resized
602 isResizable : function(colIndex) {
603 return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
606 <div id="method-Ext.grid.ColumnModel-setHidden"></div>/**
607 * Sets if a column is hidden.
609 myGrid.getColumnModel().setHidden(0, true); // hide column 0 (0 = the first column).
611 * @param {Number} colIndex The column index
612 * @param {Boolean} hidden True if the column is hidden
614 setHidden : function(colIndex, hidden) {
615 var c = this.config[colIndex];
616 if(c.hidden !== hidden){
618 this.totalWidth = null;
619 this.fireEvent("hiddenchange", this, colIndex, hidden);
623 <div id="method-Ext.grid.ColumnModel-setEditor"></div>/**
624 * Sets the editor for a column and destroys the prior editor.
625 * @param {Number} col The column index
626 * @param {Object} editor The editor object
628 setEditor : function(col, editor) {
629 this.config[col].setEditor(editor);
632 <div id="method-Ext.grid.ColumnModel-destroy"></div>/**
633 * Destroys this column model by purging any event listeners. Destroys and dereferences all Columns.
635 destroy : function() {
636 var length = this.config.length,
639 for (; i < length; i++){
640 this.config[i].destroy(); // Column's destroy encapsulates all cleanup.
644 this.purgeListeners();
649 * Setup any saved state for the column, ensures that defaults are applied.
651 setState : function(col, state) {
652 state = Ext.applyIf(state, this.defaults);
653 Ext.apply(this.config[col], state);
658 Ext.grid.ColumnModel.defaultRenderer = function(value) {
659 if (typeof value == "string" && value.length < 1) {