3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.grid.ColumnModel"></div>/**
10 * @class Ext.grid.ColumnModel
11 * @extends Ext.util.Observable
12 * <p>After the data has been read into the client side cache (<b>{@link Ext.data.Store Store}</b>),
13 * the ColumnModel is used to configure how and what parts of that data will be displayed in the
14 * vertical slices (columns) of the grid. The Ext.grid.ColumnModel Class is the default implementation
15 * of a ColumnModel used by implentations of {@link Ext.grid.GridPanel GridPanel}.</p>
16 * <p>Data is mapped into the store's records and then indexed into the ColumnModel using the
17 * <tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt>:</p>
19 {data source} == mapping ==> {data store} == <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b> ==> {ColumnModel}
21 * <p>Each {@link Ext.grid.Column Column} in the grid's ColumnModel is configured with a
22 * <tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt> to specify how the data within
23 * each record in the store is indexed into the ColumnModel.</p>
24 * <p>There are two ways to initialize the ColumnModel class:</p>
25 * <p><u>Initialization Method 1: an Array</u></p>
27 var colModel = new Ext.grid.ColumnModel([
28 { header: "Ticker", width: 60, sortable: true},
29 { header: "Company Name", width: 150, sortable: true, id: 'company'},
30 { header: "Market Cap.", width: 100, sortable: true},
31 { header: "$ Sales", width: 100, sortable: true, renderer: money},
32 { header: "Employees", width: 100, sortable: true, resizable: false}
35 * <p>The ColumnModel may be initialized with an Array of {@link Ext.grid.Column} column configuration
36 * objects to define the initial layout / display of the columns in the Grid. The order of each
37 * {@link Ext.grid.Column} column configuration object within the specified Array defines the initial
38 * order of the column display. A Column's display may be initially hidden using the
39 * <tt>{@link Ext.grid.Column#hidden hidden}</tt></b> config property (and then shown using the column
40 * header menu). Fields that are not included in the ColumnModel will not be displayable at all.</p>
41 * <p>How each column in the grid correlates (maps) to the {@link Ext.data.Record} field in the
42 * {@link Ext.data.Store Store} the column draws its data from is configured through the
43 * <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b>. If the
44 * <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b> is not explicitly defined (as shown in the
45 * example above) it will use the column configuration's index in the Array as the index.</p>
46 * <p>See <b><tt>{@link Ext.grid.Column}</tt></b> for additional configuration options for each column.</p>
47 * <p><u>Initialization Method 2: an Object</u></p>
48 * <p>In order to use configuration options from <tt>Ext.grid.ColumnModel</tt>, an Object may be used to
49 * initialize the ColumnModel. The column configuration Array will be specified in the <tt><b>{@link #columns}</b></tt>
50 * config property. The <tt><b>{@link #defaults}</b></tt> config property can be used to apply defaults
51 * for all columns, e.g.:</p><pre><code>
52 var colModel = new Ext.grid.ColumnModel({
54 { header: "Ticker", width: 60, menuDisabled: false},
55 { header: "Company Name", width: 150, id: 'company'},
56 { header: "Market Cap."},
57 { header: "$ Sales", renderer: money},
58 { header: "Employees", resizable: false}
66 {@link #hiddenchange}: function(cm, colIndex, hidden) {
67 saveConfig(colIndex, hidden);
72 * <p>In both examples above, the ability to apply a CSS class to all cells in a column (including the
73 * header) is demonstrated through the use of the <b><tt>{@link Ext.grid.Column#id id}</tt></b> config
74 * option. This column could be styled by including the following css:</p><pre><code>
75 //add this css *after* the core css is loaded
77 color: red; // entire column will have red font
79 // modify the header row only, adding an icon to the column header
81 background: transparent
82 url(../../resources/images/icons/silk/building.png)
83 no-repeat 3px 3px ! important;
87 * Note that the "Company Name" column could be specified as the
88 * <b><tt>{@link Ext.grid.GridPanel}.{@link Ext.grid.GridPanel#autoExpandColumn autoExpandColumn}</tt></b>.
90 * @param {Mixed} config Specify either an Array of {@link Ext.grid.Column} configuration objects or specify
91 * a configuration Object (see introductory section discussion utilizing Initialization Method 2 above).
93 Ext.grid.ColumnModel = Ext.extend(Ext.util.Observable, {
94 <div id="cfg-Ext.grid.ColumnModel-defaultWidth"></div>/**
95 * @cfg {Number} defaultWidth (optional) The width of columns which have no <tt>{@link #width}</tt>
96 * specified (defaults to <tt>100</tt>). This property shall preferably be configured through the
97 * <tt><b>{@link #defaults}</b></tt> config property.
100 <div id="cfg-Ext.grid.ColumnModel-defaultSortable"></div>/**
101 * @cfg {Boolean} defaultSortable (optional) Default sortable of columns which have no
102 * sortable specified (defaults to <tt>false</tt>). This property shall preferably be configured
103 * through the <tt><b>{@link #defaults}</b></tt> config property.
105 defaultSortable: false,
106 <div id="cfg-Ext.grid.ColumnModel-columns"></div>/**
107 * @cfg {Array} columns An Array of object literals. The config options defined by
108 * <b>{@link Ext.grid.Column}</b> are the options which may appear in the object literal for each
109 * individual column definition.
111 <div id="cfg-Ext.grid.ColumnModel-defaults"></div>/**
112 * @cfg {Object} defaults Object literal which will be used to apply {@link Ext.grid.Column}
113 * configuration options to all <tt><b>{@link #columns}</b></tt>. Configuration options specified with
114 * individual {@link Ext.grid.Column column} configs will supersede these <tt><b>{@link #defaults}</b></tt>.
117 constructor : function(config){
118 <div id="prop-Ext.grid.ColumnModel-config"></div>/**
119 * An Array of {@link Ext.grid.Column Column definition} objects representing the configuration
120 * of this ColumnModel. See {@link Ext.grid.Column} for the configuration properties that may
126 Ext.apply(this, config);
127 this.setConfig(config.columns, true);
129 this.setConfig(config, true);
132 <div id="event-Ext.grid.ColumnModel-widthchange"></div>/**
134 * Fires when the width of a column is programmaticially changed using
135 * <code>{@link #setColumnWidth}</code>.
136 * Note internal resizing suppresses the event from firing. See also
137 * {@link Ext.grid.GridPanel}.<code>{@link #columnresize}</code>.
138 * @param {ColumnModel} this
139 * @param {Number} columnIndex The column index
140 * @param {Number} newWidth The new width
143 <div id="event-Ext.grid.ColumnModel-headerchange"></div>/**
144 * @event headerchange
145 * Fires when the text of a header changes.
146 * @param {ColumnModel} this
147 * @param {Number} columnIndex The column index
148 * @param {String} newText The new header text
151 <div id="event-Ext.grid.ColumnModel-hiddenchange"></div>/**
152 * @event hiddenchange
153 * Fires when a column is hidden or "unhidden".
154 * @param {ColumnModel} this
155 * @param {Number} columnIndex The column index
156 * @param {Boolean} hidden true if hidden, false otherwise
159 <div id="event-Ext.grid.ColumnModel-columnmoved"></div>/**
161 * Fires when a column is moved.
162 * @param {ColumnModel} this
163 * @param {Number} oldIndex
164 * @param {Number} newIndex
167 <div id="event-Ext.grid.ColumnModel-configchange"></div>/**
168 * @event configchange
169 * Fires when the configuration is changed
170 * @param {ColumnModel} this
174 Ext.grid.ColumnModel.superclass.constructor.call(this);
177 <div id="method-Ext.grid.ColumnModel-getColumnId"></div>/**
178 * Returns the id of the column at the specified index.
179 * @param {Number} index The column index
180 * @return {String} the id
182 getColumnId : function(index){
183 return this.config[index].id;
186 getColumnAt : function(index){
187 return this.config[index];
190 <div id="method-Ext.grid.ColumnModel-setConfig"></div>/**
191 * <p>Reconfigures this column model according to the passed Array of column definition objects.
192 * For a description of the individual properties of a column definition object, see the
193 * <a href="#Ext.grid.ColumnModel-configs">Config Options</a>.</p>
194 * <p>Causes the {@link #configchange} event to be fired. A {@link Ext.grid.GridPanel GridPanel}
195 * using this ColumnModel will listen for this event and refresh its UI automatically.</p>
196 * @param {Array} config Array of Column definition objects.
197 * @param {Boolean} initial Specify <tt>true</tt> to bypass cleanup which deletes the <tt>totalWidth</tt>
198 * and destroys existing editors.
200 setConfig : function(config, initial){
202 if(!initial){ // cleanup
203 delete this.totalWidth;
204 for(i = 0, len = this.config.length; i < len; i++){
212 // backward compatibility
213 this.defaults = Ext.apply({
214 width: this.defaultWidth,
215 sortable: this.defaultSortable
218 this.config = config;
221 for(i = 0, len = config.length; i < len; i++){
222 c = Ext.applyIf(config[i], this.defaults);
223 // if no id, create one using column's ordinal position
224 if(typeof c.id == 'undefined'){
228 var Cls = Ext.grid.Column.types[c.xtype || 'gridcolumn'];
232 this.lookup[c.id] = c;
235 this.fireEvent('configchange', this);
239 <div id="method-Ext.grid.ColumnModel-getColumnById"></div>/**
240 * Returns the column for a specified id.
241 * @param {String} id The column id
242 * @return {Object} the column
244 getColumnById : function(id){
245 return this.lookup[id];
248 <div id="method-Ext.grid.ColumnModel-getIndexById"></div>/**
249 * Returns the index for a specified column id.
250 * @param {String} id The column id
251 * @return {Number} the index, or -1 if not found
253 getIndexById : function(id){
254 for(var i = 0, len = this.config.length; i < len; i++){
255 if(this.config[i].id == id){
262 <div id="method-Ext.grid.ColumnModel-moveColumn"></div>/**
263 * Moves a column from one position to another.
264 * @param {Number} oldIndex The index of the column to move.
265 * @param {Number} newIndex The position at which to reinsert the coolumn.
267 moveColumn : function(oldIndex, newIndex){
268 var c = this.config[oldIndex];
269 this.config.splice(oldIndex, 1);
270 this.config.splice(newIndex, 0, c);
272 this.fireEvent("columnmoved", this, oldIndex, newIndex);
275 <div id="method-Ext.grid.ColumnModel-getColumnCount"></div>/**
276 * Returns the number of columns.
277 * @param {Boolean} visibleOnly Optional. Pass as true to only include visible columns.
280 getColumnCount : function(visibleOnly){
281 if(visibleOnly === true){
283 for(var i = 0, len = this.config.length; i < len; i++){
284 if(!this.isHidden(i)){
290 return this.config.length;
293 <div id="method-Ext.grid.ColumnModel-getColumnsBy"></div>/**
294 * Returns the column configs that return true by the passed function that is called
295 * with (columnConfig, index)
297 // returns an array of column config objects for all hidden columns
298 var columns = grid.getColumnModel().getColumnsBy(function(c){
302 * @param {Function} fn A function which, when passed a {@link Ext.grid.Column Column} object, must
303 * return <code>true</code> if the column is to be included in the returned Array.
304 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function
305 * is executed. Defaults to this ColumnModel.
306 * @return {Array} result
308 getColumnsBy : function(fn, scope){
310 for(var i = 0, len = this.config.length; i < len; i++){
311 var c = this.config[i];
312 if(fn.call(scope||this, c, i) === true){
319 <div id="method-Ext.grid.ColumnModel-isSortable"></div>/**
320 * Returns true if the specified column is sortable.
321 * @param {Number} col The column index
324 isSortable : function(col){
325 return !!this.config[col].sortable;
328 <div id="method-Ext.grid.ColumnModel-isMenuDisabled"></div>/**
329 * Returns true if the specified column menu is disabled.
330 * @param {Number} col The column index
333 isMenuDisabled : function(col){
334 return !!this.config[col].menuDisabled;
337 <div id="method-Ext.grid.ColumnModel-getRenderer"></div>/**
338 * Returns the rendering (formatting) function defined for the column.
339 * @param {Number} col The column index.
340 * @return {Function} The function used to render the cell. See {@link #setRenderer}.
342 getRenderer : function(col){
343 if(!this.config[col].renderer){
344 return Ext.grid.ColumnModel.defaultRenderer;
346 return this.config[col].renderer;
349 getRendererScope : function(col){
350 return this.config[col].scope;
353 <div id="method-Ext.grid.ColumnModel-setRenderer"></div>/**
354 * Sets the rendering (formatting) function for a column. See {@link Ext.util.Format} for some
355 * default formatting functions.
356 * @param {Number} col The column index
357 * @param {Function} fn The function to use to process the cell's raw data
358 * to return HTML markup for the grid view. The render function is called with
359 * the following parameters:<ul>
360 * <li><b>value</b> : Object<p class="sub-desc">The data value for the cell.</p></li>
361 * <li><b>metadata</b> : Object<p class="sub-desc">An object in which you may set the following attributes:<ul>
362 * <li><b>css</b> : String<p class="sub-desc">A CSS class name to add to the cell's TD element.</p></li>
363 * <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
364 * (e.g. 'style="color:red;"').</p></li></ul></p></li>
365 * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record} from which the data was extracted.</p></li>
366 * <li><b>rowIndex</b> : Number<p class="sub-desc">Row index</p></li>
367 * <li><b>colIndex</b> : Number<p class="sub-desc">Column index</p></li>
368 * <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>
370 setRenderer : function(col, fn){
371 this.config[col].renderer = fn;
374 <div id="method-Ext.grid.ColumnModel-getColumnWidth"></div>/**
375 * Returns the width for the specified column.
376 * @param {Number} col The column index
379 getColumnWidth : function(col){
380 return this.config[col].width;
383 <div id="method-Ext.grid.ColumnModel-setColumnWidth"></div>/**
384 * Sets the width for a column.
385 * @param {Number} col The column index
386 * @param {Number} width The new width
387 * @param {Boolean} suppressEvent True to suppress firing the <code>{@link #widthchange}</code>
388 * event. Defaults to false.
390 setColumnWidth : function(col, width, suppressEvent){
391 this.config[col].width = width;
392 this.totalWidth = null;
394 this.fireEvent("widthchange", this, col, width);
398 <div id="method-Ext.grid.ColumnModel-getTotalWidth"></div>/**
399 * Returns the total width of all columns.
400 * @param {Boolean} includeHidden True to include hidden column widths
403 getTotalWidth : function(includeHidden){
404 if(!this.totalWidth){
406 for(var i = 0, len = this.config.length; i < len; i++){
407 if(includeHidden || !this.isHidden(i)){
408 this.totalWidth += this.getColumnWidth(i);
412 return this.totalWidth;
415 <div id="method-Ext.grid.ColumnModel-getColumnHeader"></div>/**
416 * Returns the header for the specified column.
417 * @param {Number} col The column index
420 getColumnHeader : function(col){
421 return this.config[col].header;
424 <div id="method-Ext.grid.ColumnModel-setColumnHeader"></div>/**
425 * Sets the header for a column.
426 * @param {Number} col The column index
427 * @param {String} header The new header
429 setColumnHeader : function(col, header){
430 this.config[col].header = header;
431 this.fireEvent("headerchange", this, col, header);
434 <div id="method-Ext.grid.ColumnModel-getColumnTooltip"></div>/**
435 * Returns the tooltip for the specified column.
436 * @param {Number} col The column index
439 getColumnTooltip : function(col){
440 return this.config[col].tooltip;
442 <div id="method-Ext.grid.ColumnModel-setColumnTooltip"></div>/**
443 * Sets the tooltip for a column.
444 * @param {Number} col The column index
445 * @param {String} tooltip The new tooltip
447 setColumnTooltip : function(col, tooltip){
448 this.config[col].tooltip = tooltip;
451 <div id="method-Ext.grid.ColumnModel-getDataIndex"></div>/**
452 * Returns the dataIndex for the specified column.
454 // Get field name for the column
455 var fieldName = grid.getColumnModel().getDataIndex(columnIndex);
457 * @param {Number} col The column index
458 * @return {String} The column's dataIndex
460 getDataIndex : function(col){
461 return this.config[col].dataIndex;
464 <div id="method-Ext.grid.ColumnModel-setDataIndex"></div>/**
465 * Sets the dataIndex for a column.
466 * @param {Number} col The column index
467 * @param {String} dataIndex The new dataIndex
469 setDataIndex : function(col, dataIndex){
470 this.config[col].dataIndex = dataIndex;
473 <div id="method-Ext.grid.ColumnModel-findColumnIndex"></div>/**
474 * Finds the index of the first matching column for the given dataIndex.
475 * @param {String} col The dataIndex to find
476 * @return {Number} The column index, or -1 if no match was found
478 findColumnIndex : function(dataIndex){
480 for(var i = 0, len = c.length; i < len; i++){
481 if(c[i].dataIndex == dataIndex){
488 <div id="method-Ext.grid.ColumnModel-isCellEditable"></div>/**
489 * Returns true if the cell is editable.
491 var store = new Ext.data.Store({...});
492 var colModel = new Ext.grid.ColumnModel({
494 isCellEditable: function(col, row) {
495 var record = store.getAt(row);
496 if (record.get('readonly')) { // replace with your condition
499 return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, col, row);
502 var grid = new Ext.grid.GridPanel({
508 * @param {Number} colIndex The column index
509 * @param {Number} rowIndex The row index
512 isCellEditable : function(colIndex, rowIndex){
513 return (this.config[colIndex].editable || (typeof this.config[colIndex].editable == "undefined" && this.config[colIndex].editor)) ? true : false;
516 <div id="method-Ext.grid.ColumnModel-getCellEditor"></div>/**
517 * Returns the editor defined for the cell/column.
518 * @param {Number} colIndex The column index
519 * @param {Number} rowIndex The row index
520 * @return {Ext.Editor} The {@link Ext.Editor Editor} that was created to wrap
521 * the {@link Ext.form.Field Field} used to edit the cell.
523 getCellEditor : function(colIndex, rowIndex){
524 return this.config[colIndex].getCellEditor(rowIndex);
527 <div id="method-Ext.grid.ColumnModel-setEditable"></div>/**
528 * Sets if a column is editable.
529 * @param {Number} col The column index
530 * @param {Boolean} editable True if the column is editable
532 setEditable : function(col, editable){
533 this.config[col].editable = editable;
536 <div id="method-Ext.grid.ColumnModel-isHidden"></div>/**
537 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#hidden hidden}</code>,
538 * <tt>false</tt> otherwise.
539 * @param {Number} colIndex The column index
542 isHidden : function(colIndex){
543 return !!this.config[colIndex].hidden; // ensure returns boolean
546 <div id="method-Ext.grid.ColumnModel-isFixed"></div>/**
547 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#fixed fixed}</code>,
548 * <tt>false</tt> otherwise.
549 * @param {Number} colIndex The column index
552 isFixed : function(colIndex){
553 return !!this.config[colIndex].fixed;
556 <div id="method-Ext.grid.ColumnModel-isResizable"></div>/**
557 * Returns true if the column can be resized
560 isResizable : function(colIndex){
561 return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
563 <div id="method-Ext.grid.ColumnModel-setHidden"></div>/**
564 * Sets if a column is hidden.
566 myGrid.getColumnModel().setHidden(0, true); // hide column 0 (0 = the first column).
568 * @param {Number} colIndex The column index
569 * @param {Boolean} hidden True if the column is hidden
571 setHidden : function(colIndex, hidden){
572 var c = this.config[colIndex];
573 if(c.hidden !== hidden){
575 this.totalWidth = null;
576 this.fireEvent("hiddenchange", this, colIndex, hidden);
580 <div id="method-Ext.grid.ColumnModel-setEditor"></div>/**
581 * Sets the editor for a column and destroys the prior editor.
582 * @param {Number} col The column index
583 * @param {Object} editor The editor object
585 setEditor : function(col, editor){
586 Ext.destroy(this.config[col].editor);
587 this.config[col].editor = editor;
590 <div id="method-Ext.grid.ColumnModel-destroy"></div>/**
591 * Destroys this column model by purging any event listeners, and removing any editors.
593 destroy : function(){
594 for(var i = 0, c = this.config, len = c.length; i < len; i++){
595 Ext.destroy(c[i].editor);
597 this.purgeListeners();
602 Ext.grid.ColumnModel.defaultRenderer = function(value){
603 if(typeof value == "string" && value.length < 1){