3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.grid.ColumnModel"></div>/**
9 * @class Ext.grid.ColumnModel
10 * @extends Ext.util.Observable
11 * <p>After the data has been read into the client side cache (<b>{@link Ext.data.Store Store}</b>),
12 * the ColumnModel is used to configure how and what parts of that data will be displayed in the
13 * vertical slices (columns) of the grid. The Ext.grid.ColumnModel Class is the default implementation
14 * of a ColumnModel used by implentations of {@link Ext.grid.GridPanel GridPanel}.</p>
15 * <p>Data is mapped into the store's records and then indexed into the ColumnModel using the
16 * <tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt>:</p>
18 {data source} == mapping ==> {data store} == <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b> ==> {ColumnModel}
20 * <p>Each {@link Ext.grid.Column Column} in the grid's ColumnModel is configured with a
21 * <tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt> to specify how the data within
22 * each record in the store is indexed into the ColumnModel.</p>
23 * <p>There are two ways to initialize the ColumnModel class:</p>
24 * <p><u>Initialization Method 1: an Array</u></p>
26 var colModel = new Ext.grid.ColumnModel([
27 { header: "Ticker", width: 60, sortable: true},
28 { header: "Company Name", width: 150, sortable: true, id: 'company'},
29 { header: "Market Cap.", width: 100, sortable: true},
30 { header: "$ Sales", width: 100, sortable: true, renderer: money},
31 { header: "Employees", width: 100, sortable: true, resizable: false}
34 * <p>The ColumnModel may be initialized with an Array of {@link Ext.grid.Column} column configuration
35 * objects to define the initial layout / display of the columns in the Grid. The order of each
36 * {@link Ext.grid.Column} column configuration object within the specified Array defines the initial
37 * order of the column display. A Column's display may be initially hidden using the
38 * <tt>{@link Ext.grid.Column#hidden hidden}</tt></b> config property (and then shown using the column
39 * header menu). Field's that are not included in the ColumnModel will not be displayable at all.</p>
40 * <p>How each column in the grid correlates (maps) to the {@link Ext.data.Record} field in the
41 * {@link Ext.data.Store Store} the column draws its data from is configured through the
42 * <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b>. If the
43 * <b><tt>{@link Ext.grid.Column#dataIndex dataIndex}</tt></b> is not explicitly defined (as shown in the
44 * example above) it will use the column configuration's index in the Array as the index.</p>
45 * <p>See <b><tt>{@link Ext.grid.Column}</tt></b> for additional configuration options for each column.</p>
46 * <p><u>Initialization Method 2: an Object</u></p>
47 * <p>In order to use configuration options from <tt>Ext.grid.ColumnModel</tt>, an Object may be used to
48 * initialize the ColumnModel. The column configuration Array will be specified in the <tt><b>{@link #columns}</b></tt>
49 * config property. The <tt><b>{@link #defaults}</b></tt> config property can be used to apply defaults
50 * for all columns, e.g.:</p><pre><code>
51 var colModel = new Ext.grid.ColumnModel({
53 { header: "Ticker", width: 60, menuDisabled: false},
54 { header: "Company Name", width: 150, id: 'company'},
55 { header: "Market Cap."},
56 { header: "$ Sales", renderer: money},
57 { header: "Employees", resizable: false}
65 {@link #hiddenchange}: function(cm, colIndex, hidden) {
66 saveConfig(colIndex, hidden);
71 * <p>In both examples above, the ability to apply a CSS class to all cells in a column (including the
72 * header) is demonstrated through the use of the <b><tt>{@link Ext.grid.Column#id id}</tt></b> config
73 * option. This column could be styled by including the following css:</p><pre><code>
74 //add this css *after* the core css is loaded
76 color: red; // entire column will have red font
78 // modify the header row only, adding an icon to the column header
80 background: transparent
81 url(../../resources/images/icons/silk/building.png)
82 no-repeat 3px 3px ! important;
86 * Note that the "Company Name" column could be specified as the
87 * <b><tt>{@link Ext.grid.GridPanel}.{@link Ext.grid.GridPanel#autoExpandColumn autoExpandColumn}</tt></b>.
89 * @param {Mixed} config Specify either an Array of {@link Ext.grid.Column} configuration objects or specify
90 * a configuration Object (see introductory section discussion utilizing Initialization Method 2 above).
92 Ext.grid.ColumnModel = function(config){
93 <div id="prop-Ext.grid.ColumnModel-config"></div>/**
94 * An Array of {@link Ext.grid.Column Column definition} objects representing the configuration
95 * of this ColumnModel. See {@link Ext.grid.Column} for the configuration properties that may
101 Ext.apply(this, config);
102 this.setConfig(config.columns, true);
104 this.setConfig(config, true);
107 <div id="event-Ext.grid.ColumnModel-widthchange"></div>/**
109 * Fires when the width of a column is programmaticially changed using
110 * <code>{@link #setColumnWidth}</code>.
111 * Note internal resizing suppresses the event from firing. See also
112 * {@link Ext.grid.GridPanel}.<code>{@link #columnresize}</code>.
113 * @param {ColumnModel} this
114 * @param {Number} columnIndex The column index
115 * @param {Number} newWidth The new width
118 <div id="event-Ext.grid.ColumnModel-headerchange"></div>/**
119 * @event headerchange
120 * Fires when the text of a header changes.
121 * @param {ColumnModel} this
122 * @param {Number} columnIndex The column index
123 * @param {String} newText The new header text
126 <div id="event-Ext.grid.ColumnModel-hiddenchange"></div>/**
127 * @event hiddenchange
128 * Fires when a column is hidden or "unhidden".
129 * @param {ColumnModel} this
130 * @param {Number} columnIndex The column index
131 * @param {Boolean} hidden true if hidden, false otherwise
134 <div id="event-Ext.grid.ColumnModel-columnmoved"></div>/**
136 * Fires when a column is moved.
137 * @param {ColumnModel} this
138 * @param {Number} oldIndex
139 * @param {Number} newIndex
142 <div id="event-Ext.grid.ColumnModel-configchange"></div>/**
143 * @event configchange
144 * Fires when the configuration is changed
145 * @param {ColumnModel} this
149 Ext.grid.ColumnModel.superclass.constructor.call(this);
151 Ext.extend(Ext.grid.ColumnModel, Ext.util.Observable, {
152 <div id="cfg-Ext.grid.ColumnModel-defaultWidth"></div>/**
153 * @cfg {Number} defaultWidth (optional) The width of columns which have no <tt>{@link #width}</tt>
154 * specified (defaults to <tt>100</tt>). This property shall preferably be configured through the
155 * <tt><b>{@link #defaults}</b></tt> config property.
158 <div id="cfg-Ext.grid.ColumnModel-defaultSortable"></div>/**
159 * @cfg {Boolean} defaultSortable (optional) Default sortable of columns which have no
160 * sortable specified (defaults to <tt>false</tt>). This property shall preferably be configured
161 * through the <tt><b>{@link #defaults}</b></tt> config property.
163 defaultSortable: false,
164 <div id="cfg-Ext.grid.ColumnModel-columns"></div>/**
165 * @cfg {Array} columns An Array of object literals. The config options defined by
166 * <b>{@link Ext.grid.Column}</b> are the options which may appear in the object literal for each
167 * individual column definition.
169 <div id="cfg-Ext.grid.ColumnModel-defaults"></div>/**
170 * @cfg {Object} defaults Object literal which will be used to apply {@link Ext.grid.Column}
171 * configuration options to all <tt><b>{@link #columns}</b></tt>. Configuration options specified with
172 * individual {@link Ext.grid.Column column} configs will supersede these <tt><b>{@link #defaults}</b></tt>.
175 <div id="method-Ext.grid.ColumnModel-getColumnId"></div>/**
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];
188 <div id="method-Ext.grid.ColumnModel-setConfig"></div>/**
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++){
210 // backward compatibility
211 this.defaults = Ext.apply({
212 width: this.defaultWidth,
213 sortable: this.defaultSortable
216 this.config = config;
218 // if no id, create one
219 for(i = 0, len = config.length; i < len; i++){
220 c = Ext.applyIf(config[i], this.defaults);
222 var cls = Ext.grid.Column.types[c.xtype || 'gridcolumn'];
226 this.lookup[c.id] = c;
229 this.fireEvent('configchange', this);
233 <div id="method-Ext.grid.ColumnModel-getColumnById"></div>/**
234 * Returns the column for a specified id.
235 * @param {String} id The column id
236 * @return {Object} the column
238 getColumnById : function(id){
239 return this.lookup[id];
242 <div id="method-Ext.grid.ColumnModel-getIndexById"></div>/**
243 * Returns the index for a specified column id.
244 * @param {String} id The column id
245 * @return {Number} the index, or -1 if not found
247 getIndexById : function(id){
248 for(var i = 0, len = this.config.length; i < len; i++){
249 if(this.config[i].id == id){
256 <div id="method-Ext.grid.ColumnModel-moveColumn"></div>/**
257 * Moves a column from one position to another.
258 * @param {Number} oldIndex The index of the column to move.
259 * @param {Number} newIndex The position at which to reinsert the coolumn.
261 moveColumn : function(oldIndex, newIndex){
262 var c = this.config[oldIndex];
263 this.config.splice(oldIndex, 1);
264 this.config.splice(newIndex, 0, c);
266 this.fireEvent("columnmoved", this, oldIndex, newIndex);
269 <div id="method-Ext.grid.ColumnModel-getColumnCount"></div>/**
270 * Returns the number of columns.
271 * @param {Boolean} visibleOnly Optional. Pass as true to only include visible columns.
274 getColumnCount : function(visibleOnly){
275 if(visibleOnly === true){
277 for(var i = 0, len = this.config.length; i < len; i++){
278 if(!this.isHidden(i)){
284 return this.config.length;
287 <div id="method-Ext.grid.ColumnModel-getColumnsBy"></div>/**
288 * Returns the column configs that return true by the passed function that is called
289 * with (columnConfig, index)
291 // returns an array of column config objects for all hidden columns
292 var columns = grid.getColumnModel().getColumnsBy(function(c){
296 * @param {Function} fn
297 * @param {Object} scope (optional)
298 * @return {Array} result
300 getColumnsBy : function(fn, scope){
302 for(var i = 0, len = this.config.length; i < len; i++){
303 var c = this.config[i];
304 if(fn.call(scope||this, c, i) === true){
311 <div id="method-Ext.grid.ColumnModel-isSortable"></div>/**
312 * Returns true if the specified column is sortable.
313 * @param {Number} col The column index
316 isSortable : function(col){
317 return this.config[col].sortable;
320 <div id="method-Ext.grid.ColumnModel-isMenuDisabled"></div>/**
321 * Returns true if the specified column menu is disabled.
322 * @param {Number} col The column index
325 isMenuDisabled : function(col){
326 return !!this.config[col].menuDisabled;
329 <div id="method-Ext.grid.ColumnModel-getRenderer"></div>/**
330 * Returns the rendering (formatting) function defined for the column.
331 * @param {Number} col The column index.
332 * @return {Function} The function used to render the cell. See {@link #setRenderer}.
334 getRenderer : function(col){
335 if(!this.config[col].renderer){
336 return Ext.grid.ColumnModel.defaultRenderer;
338 return this.config[col].renderer;
341 <div id="method-Ext.grid.ColumnModel-setRenderer"></div>/**
342 * Sets the rendering (formatting) function for a column. See {@link Ext.util.Format} for some
343 * default formatting functions.
344 * @param {Number} col The column index
345 * @param {Function} fn The function to use to process the cell's raw data
346 * to return HTML markup for the grid view. The render function is called with
347 * the following parameters:<ul>
348 * <li><b>value</b> : Object<p class="sub-desc">The data value for the cell.</p></li>
349 * <li><b>metadata</b> : Object<p class="sub-desc">An object in which you may set the following attributes:<ul>
350 * <li><b>css</b> : String<p class="sub-desc">A CSS class name to add to the cell's TD element.</p></li>
351 * <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
352 * (e.g. 'style="color:red;"').</p></li></ul></p></li>
353 * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record} from which the data was extracted.</p></li>
354 * <li><b>rowIndex</b> : Number<p class="sub-desc">Row index</p></li>
355 * <li><b>colIndex</b> : Number<p class="sub-desc">Column index</p></li>
356 * <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>
358 setRenderer : function(col, fn){
359 this.config[col].renderer = fn;
362 <div id="method-Ext.grid.ColumnModel-getColumnWidth"></div>/**
363 * Returns the width for the specified column.
364 * @param {Number} col The column index
367 getColumnWidth : function(col){
368 return this.config[col].width;
371 <div id="method-Ext.grid.ColumnModel-setColumnWidth"></div>/**
372 * Sets the width for a column.
373 * @param {Number} col The column index
374 * @param {Number} width The new width
375 * @param {Boolean} suppressEvent True to suppress firing the <code>{@link #widthchange}</code>
376 * event. Defaults to false.
378 setColumnWidth : function(col, width, suppressEvent){
379 this.config[col].width = width;
380 this.totalWidth = null;
382 this.fireEvent("widthchange", this, col, width);
386 <div id="method-Ext.grid.ColumnModel-getTotalWidth"></div>/**
387 * Returns the total width of all columns.
388 * @param {Boolean} includeHidden True to include hidden column widths
391 getTotalWidth : function(includeHidden){
392 if(!this.totalWidth){
394 for(var i = 0, len = this.config.length; i < len; i++){
395 if(includeHidden || !this.isHidden(i)){
396 this.totalWidth += this.getColumnWidth(i);
400 return this.totalWidth;
403 <div id="method-Ext.grid.ColumnModel-getColumnHeader"></div>/**
404 * Returns the header for the specified column.
405 * @param {Number} col The column index
408 getColumnHeader : function(col){
409 return this.config[col].header;
412 <div id="method-Ext.grid.ColumnModel-setColumnHeader"></div>/**
413 * Sets the header for a column.
414 * @param {Number} col The column index
415 * @param {String} header The new header
417 setColumnHeader : function(col, header){
418 this.config[col].header = header;
419 this.fireEvent("headerchange", this, col, header);
422 <div id="method-Ext.grid.ColumnModel-getColumnTooltip"></div>/**
423 * Returns the tooltip for the specified column.
424 * @param {Number} col The column index
427 getColumnTooltip : function(col){
428 return this.config[col].tooltip;
430 <div id="method-Ext.grid.ColumnModel-setColumnTooltip"></div>/**
431 * Sets the tooltip for a column.
432 * @param {Number} col The column index
433 * @param {String} tooltip The new tooltip
435 setColumnTooltip : function(col, tooltip){
436 this.config[col].tooltip = tooltip;
439 <div id="method-Ext.grid.ColumnModel-getDataIndex"></div>/**
440 * Returns the dataIndex for the specified column.
442 // Get field name for the column
443 var fieldName = grid.getColumnModel().getDataIndex(columnIndex);
445 * @param {Number} col The column index
446 * @return {String} The column's dataIndex
448 getDataIndex : function(col){
449 return this.config[col].dataIndex;
452 <div id="method-Ext.grid.ColumnModel-setDataIndex"></div>/**
453 * Sets the dataIndex for a column.
454 * @param {Number} col The column index
455 * @param {String} dataIndex The new dataIndex
457 setDataIndex : function(col, dataIndex){
458 this.config[col].dataIndex = dataIndex;
461 <div id="method-Ext.grid.ColumnModel-findColumnIndex"></div>/**
462 * Finds the index of the first matching column for the given dataIndex.
463 * @param {String} col The dataIndex to find
464 * @return {Number} The column index, or -1 if no match was found
466 findColumnIndex : function(dataIndex){
468 for(var i = 0, len = c.length; i < len; i++){
469 if(c[i].dataIndex == dataIndex){
476 <div id="method-Ext.grid.ColumnModel-isCellEditable"></div>/**
477 * Returns true if the cell is editable.
479 var store = new Ext.data.Store({...});
480 var colModel = new Ext.grid.ColumnModel({
482 isCellEditable: function(col, row) {
483 var record = store.getAt(row);
484 if (record.get('readonly')) { // replace with your condition
487 return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, col, row);
490 var grid = new Ext.grid.GridPanel({
496 * @param {Number} colIndex The column index
497 * @param {Number} rowIndex The row index
500 isCellEditable : function(colIndex, rowIndex){
501 return (this.config[colIndex].editable || (typeof this.config[colIndex].editable == "undefined" && this.config[colIndex].editor)) ? true : false;
504 <div id="method-Ext.grid.ColumnModel-getCellEditor"></div>/**
505 * Returns the editor defined for the cell/column.
506 * @param {Number} colIndex The column index
507 * @param {Number} rowIndex The row index
508 * @return {Ext.Editor} The {@link Ext.Editor Editor} that was created to wrap
509 * the {@link Ext.form.Field Field} used to edit the cell.
511 getCellEditor : function(colIndex, rowIndex){
512 return this.config[colIndex].getCellEditor(rowIndex);
515 <div id="method-Ext.grid.ColumnModel-setEditable"></div>/**
516 * Sets if a column is editable.
517 * @param {Number} col The column index
518 * @param {Boolean} editable True if the column is editable
520 setEditable : function(col, editable){
521 this.config[col].editable = editable;
525 <div id="method-Ext.grid.ColumnModel-isHidden"></div>/**
526 * Returns true if the column is hidden.
527 * @param {Number} colIndex The column index
530 isHidden : function(colIndex){
531 return this.config[colIndex].hidden;
535 <div id="method-Ext.grid.ColumnModel-isFixed"></div>/**
536 * Returns true if the column width cannot be changed
538 isFixed : function(colIndex){
539 return this.config[colIndex].fixed;
542 <div id="method-Ext.grid.ColumnModel-isResizable"></div>/**
543 * Returns true if the column can be resized
546 isResizable : function(colIndex){
547 return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
549 <div id="method-Ext.grid.ColumnModel-setHidden"></div>/**
550 * Sets if a column is hidden.
552 myGrid.getColumnModel().setHidden(0, true); // hide column 0 (0 = the first column).
554 * @param {Number} colIndex The column index
555 * @param {Boolean} hidden True if the column is hidden
557 setHidden : function(colIndex, hidden){
558 var c = this.config[colIndex];
559 if(c.hidden !== hidden){
561 this.totalWidth = null;
562 this.fireEvent("hiddenchange", this, colIndex, hidden);
566 <div id="method-Ext.grid.ColumnModel-setEditor"></div>/**
567 * Sets the editor for a column and destroys the prior editor.
568 * @param {Number} col The column index
569 * @param {Object} editor The editor object
571 setEditor : function(col, editor){
572 Ext.destroy(this.config[col].editor);
573 this.config[col].editor = editor;
576 <div id="method-Ext.grid.ColumnModel-destroy"></div>/**
577 * Destroys this column model by purging any event listeners, and removing any editors.
579 destroy : function(){
580 for(var i = 0, c = this.config, len = c.length; i < len; i++){
581 Ext.destroy(c[i].editor);
583 this.purgeListeners();
588 Ext.grid.ColumnModel.defaultRenderer = function(value){
589 if(typeof value == "string" && value.length < 1){