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++){
205 this.config[i].destroy();
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(Ext.isEmpty(c.id)){
225 var Cls = Ext.grid.Column.types[c.xtype || 'gridcolumn'];
229 this.lookup[c.id] = c;
232 this.fireEvent('configchange', this);
236 <div id="method-Ext.grid.ColumnModel-getColumnById"></div>/**
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];
245 <div id="method-Ext.grid.ColumnModel-getIndexById"></div>/**
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){
259 <div id="method-Ext.grid.ColumnModel-moveColumn"></div>/**
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);
272 <div id="method-Ext.grid.ColumnModel-getColumnCount"></div>/**
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;
290 <div id="method-Ext.grid.ColumnModel-getColumnsBy"></div>/**
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 A function which, when passed a {@link Ext.grid.Column Column} object, must
300 * return <code>true</code> if the column is to be included in the returned Array.
301 * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the function
302 * is executed. Defaults to this ColumnModel.
303 * @return {Array} result
305 getColumnsBy : function(fn, scope){
307 for(var i = 0, len = this.config.length; i < len; i++){
308 var c = this.config[i];
309 if(fn.call(scope||this, c, i) === true){
316 <div id="method-Ext.grid.ColumnModel-isSortable"></div>/**
317 * Returns true if the specified column is sortable.
318 * @param {Number} col The column index
321 isSortable : function(col){
322 return !!this.config[col].sortable;
325 <div id="method-Ext.grid.ColumnModel-isMenuDisabled"></div>/**
326 * Returns true if the specified column menu is disabled.
327 * @param {Number} col The column index
330 isMenuDisabled : function(col){
331 return !!this.config[col].menuDisabled;
334 <div id="method-Ext.grid.ColumnModel-getRenderer"></div>/**
335 * Returns the rendering (formatting) function defined for the column.
336 * @param {Number} col The column index.
337 * @return {Function} The function used to render the cell. See {@link #setRenderer}.
339 getRenderer : function(col){
340 if(!this.config[col].renderer){
341 return Ext.grid.ColumnModel.defaultRenderer;
343 return this.config[col].renderer;
346 getRendererScope : function(col){
347 return this.config[col].scope;
350 <div id="method-Ext.grid.ColumnModel-setRenderer"></div>/**
351 * Sets the rendering (formatting) function for a column. See {@link Ext.util.Format} for some
352 * default formatting functions.
353 * @param {Number} col The column index
354 * @param {Function} fn The function to use to process the cell's raw data
355 * to return HTML markup for the grid view. The render function is called with
356 * the following parameters:<ul>
357 * <li><b>value</b> : Object<p class="sub-desc">The data value for the cell.</p></li>
358 * <li><b>metadata</b> : Object<p class="sub-desc">An object in which you may set the following attributes:<ul>
359 * <li><b>css</b> : String<p class="sub-desc">A CSS class name to add to the cell's TD element.</p></li>
360 * <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
361 * (e.g. 'style="color:red;"').</p></li></ul></p></li>
362 * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record} from which the data was extracted.</p></li>
363 * <li><b>rowIndex</b> : Number<p class="sub-desc">Row index</p></li>
364 * <li><b>colIndex</b> : Number<p class="sub-desc">Column index</p></li>
365 * <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>
367 setRenderer : function(col, fn){
368 this.config[col].renderer = fn;
371 <div id="method-Ext.grid.ColumnModel-getColumnWidth"></div>/**
372 * Returns the width for the specified column.
373 * @param {Number} col The column index
376 getColumnWidth : function(col){
377 return this.config[col].width;
380 <div id="method-Ext.grid.ColumnModel-setColumnWidth"></div>/**
381 * Sets the width for a column.
382 * @param {Number} col The column index
383 * @param {Number} width The new width
384 * @param {Boolean} suppressEvent True to suppress firing the <code>{@link #widthchange}</code>
385 * event. Defaults to false.
387 setColumnWidth : function(col, width, suppressEvent){
388 this.config[col].width = width;
389 this.totalWidth = null;
391 this.fireEvent("widthchange", this, col, width);
395 <div id="method-Ext.grid.ColumnModel-getTotalWidth"></div>/**
396 * Returns the total width of all columns.
397 * @param {Boolean} includeHidden True to include hidden column widths
400 getTotalWidth : function(includeHidden){
401 if(!this.totalWidth){
403 for(var i = 0, len = this.config.length; i < len; i++){
404 if(includeHidden || !this.isHidden(i)){
405 this.totalWidth += this.getColumnWidth(i);
409 return this.totalWidth;
412 <div id="method-Ext.grid.ColumnModel-getColumnHeader"></div>/**
413 * Returns the header for the specified column.
414 * @param {Number} col The column index
417 getColumnHeader : function(col){
418 return this.config[col].header;
421 <div id="method-Ext.grid.ColumnModel-setColumnHeader"></div>/**
422 * Sets the header for a column.
423 * @param {Number} col The column index
424 * @param {String} header The new header
426 setColumnHeader : function(col, header){
427 this.config[col].header = header;
428 this.fireEvent("headerchange", this, col, header);
431 <div id="method-Ext.grid.ColumnModel-getColumnTooltip"></div>/**
432 * Returns the tooltip for the specified column.
433 * @param {Number} col The column index
436 getColumnTooltip : function(col){
437 return this.config[col].tooltip;
439 <div id="method-Ext.grid.ColumnModel-setColumnTooltip"></div>/**
440 * Sets the tooltip for a column.
441 * @param {Number} col The column index
442 * @param {String} tooltip The new tooltip
444 setColumnTooltip : function(col, tooltip){
445 this.config[col].tooltip = tooltip;
448 <div id="method-Ext.grid.ColumnModel-getDataIndex"></div>/**
449 * Returns the dataIndex for the specified column.
451 // Get field name for the column
452 var fieldName = grid.getColumnModel().getDataIndex(columnIndex);
454 * @param {Number} col The column index
455 * @return {String} The column's dataIndex
457 getDataIndex : function(col){
458 return this.config[col].dataIndex;
461 <div id="method-Ext.grid.ColumnModel-setDataIndex"></div>/**
462 * Sets the dataIndex for a column.
463 * @param {Number} col The column index
464 * @param {String} dataIndex The new dataIndex
466 setDataIndex : function(col, dataIndex){
467 this.config[col].dataIndex = dataIndex;
470 <div id="method-Ext.grid.ColumnModel-findColumnIndex"></div>/**
471 * Finds the index of the first matching column for the given dataIndex.
472 * @param {String} col The dataIndex to find
473 * @return {Number} The column index, or -1 if no match was found
475 findColumnIndex : function(dataIndex){
477 for(var i = 0, len = c.length; i < len; i++){
478 if(c[i].dataIndex == dataIndex){
485 <div id="method-Ext.grid.ColumnModel-isCellEditable"></div>/**
486 * Returns true if the cell is editable.
488 var store = new Ext.data.Store({...});
489 var colModel = new Ext.grid.ColumnModel({
491 isCellEditable: function(col, row) {
492 var record = store.getAt(row);
493 if (record.get('readonly')) { // replace with your condition
496 return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, col, row);
499 var grid = new Ext.grid.GridPanel({
505 * @param {Number} colIndex The column index
506 * @param {Number} rowIndex The row index
509 isCellEditable : function(colIndex, rowIndex){
510 var c = this.config[colIndex],
514 return !!(ed || (!Ext.isDefined(ed) && c.editor));
517 <div id="method-Ext.grid.ColumnModel-getCellEditor"></div>/**
518 * Returns the editor defined for the cell/column.
519 * @param {Number} colIndex The column index
520 * @param {Number} rowIndex The row index
521 * @return {Ext.Editor} The {@link Ext.Editor Editor} that was created to wrap
522 * the {@link Ext.form.Field Field} used to edit the cell.
524 getCellEditor : function(colIndex, rowIndex){
525 return this.config[colIndex].getCellEditor(rowIndex);
528 <div id="method-Ext.grid.ColumnModel-setEditable"></div>/**
529 * Sets if a column is editable.
530 * @param {Number} col The column index
531 * @param {Boolean} editable True if the column is editable
533 setEditable : function(col, editable){
534 this.config[col].editable = editable;
537 <div id="method-Ext.grid.ColumnModel-isHidden"></div>/**
538 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#hidden hidden}</code>,
539 * <tt>false</tt> otherwise.
540 * @param {Number} colIndex The column index
543 isHidden : function(colIndex){
544 return !!this.config[colIndex].hidden; // ensure returns boolean
547 <div id="method-Ext.grid.ColumnModel-isFixed"></div>/**
548 * Returns <tt>true</tt> if the column is <code>{@link Ext.grid.Column#fixed fixed}</code>,
549 * <tt>false</tt> otherwise.
550 * @param {Number} colIndex The column index
553 isFixed : function(colIndex){
554 return !!this.config[colIndex].fixed;
557 <div id="method-Ext.grid.ColumnModel-isResizable"></div>/**
558 * Returns true if the column can be resized
561 isResizable : function(colIndex){
562 return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
564 <div id="method-Ext.grid.ColumnModel-setHidden"></div>/**
565 * Sets if a column is hidden.
567 myGrid.getColumnModel().setHidden(0, true); // hide column 0 (0 = the first column).
569 * @param {Number} colIndex The column index
570 * @param {Boolean} hidden True if the column is hidden
572 setHidden : function(colIndex, hidden){
573 var c = this.config[colIndex];
574 if(c.hidden !== hidden){
576 this.totalWidth = null;
577 this.fireEvent("hiddenchange", this, colIndex, hidden);
581 <div id="method-Ext.grid.ColumnModel-setEditor"></div>/**
582 * Sets the editor for a column and destroys the prior editor.
583 * @param {Number} col The column index
584 * @param {Object} editor The editor object
586 setEditor : function(col, editor){
587 this.config[col].setEditor(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, len = this.config.length; i < len; i++){
595 this.config[i].destroy();
597 this.purgeListeners();
602 Ext.grid.ColumnModel.defaultRenderer = function(value){
603 if(typeof value == "string" && value.length < 1){