2 * Ext JS Library 2.2.1
\r
3 * Copyright(c) 2006-2009, Ext JS, LLC.
\r
4 * licensing@extjs.com
\r
6 * http://extjs.com/license
\r
10 * @class Ext.grid.ColumnModel
\r
11 * @extends Ext.util.Observable
\r
12 * This is the default implementation of a ColumnModel used by the Grid. This class is initialized
\r
13 * with an Array of column config objects.
\r
15 * An individual column's config object defines the header string, the {@link Ext.data.Record}
\r
16 * field the column draws its data from, an optional rendering function to provide customized
\r
17 * data formatting, and the ability to apply a CSS class to all cells in a column through its
\r
18 * {@link #id} config option.<br>
\r
21 var colModel = new Ext.grid.ColumnModel([
\r
22 { header: "Ticker", width: 60, sortable: true},
\r
23 { header: "Company Name", width: 150, sortable: true},
\r
24 { header: "Market Cap.", width: 100, sortable: true},
\r
25 { header: "$ Sales", width: 100, sortable: true, renderer: money},
\r
26 { header: "Employees", width: 100, sortable: true, resizable: false}
\r
30 * The config options <b>defined by</b> this class are options which may appear in each
\r
31 * individual column definition. In order to use configuration options from the superclass,
\r
32 * specify the column configuration Array in the <tt><b>columns<b><tt> config property. eg:<pre><code>
\r
33 var colModel = new Ext.grid.ColumnModel({
\r
35 widthchange: function(cm, colIndex, width) {
\r
36 saveConfig(colIndex, width);
\r
40 { header: "Ticker", width: 60, sortable: true},
\r
41 { header: "Company Name", width: 150, sortable: true},
\r
42 { header: "Market Cap.", width: 100, sortable: true},
\r
43 { header: "$ Sales", width: 100, sortable: true, renderer: money},
\r
44 { header: "Employees", width: 100, sortable: true, resizable: false}
\r
49 * @param {Object} config An Array of column config objects. See this class's
\r
50 * config objects for details.
\r
52 Ext.grid.ColumnModel = function(config){
\r
54 * The width of columns which have no width specified (defaults to 100)
\r
57 this.defaultWidth = 100;
\r
60 * Default sortable of columns which have no sortable specified (defaults to false)
\r
63 this.defaultSortable = false;
\r
66 * The config passed into the constructor
\r
67 * @property {Array} config
\r
70 Ext.apply(this, config);
\r
71 this.setConfig(config.columns, true);
\r
73 this.setConfig(config, true);
\r
77 * @event widthchange
\r
78 * Fires when the width of a column changes.
\r
79 * @param {ColumnModel} this
\r
80 * @param {Number} columnIndex The column index
\r
81 * @param {Number} newWidth The new width
\r
85 * @event headerchange
\r
86 * Fires when the text of a header changes.
\r
87 * @param {ColumnModel} this
\r
88 * @param {Number} columnIndex The column index
\r
89 * @param {String} newText The new header text
\r
93 * @event hiddenchange
\r
94 * Fires when a column is hidden or "unhidden".
\r
95 * @param {ColumnModel} this
\r
96 * @param {Number} columnIndex The column index
\r
97 * @param {Boolean} hidden true if hidden, false otherwise
\r
101 * @event columnmoved
\r
102 * Fires when a column is moved.
\r
103 * @param {ColumnModel} this
\r
104 * @param {Number} oldIndex
\r
105 * @param {Number} newIndex
\r
108 // deprecated - to be removed
\r
109 "columnlockchange",
\r
111 * @event configchange
\r
112 * Fires when the configuration is changed
\r
113 * @param {ColumnModel} this
\r
117 Ext.grid.ColumnModel.superclass.constructor.call(this);
\r
119 Ext.extend(Ext.grid.ColumnModel, Ext.util.Observable, {
\r
121 * @cfg {String} id (optional) Defaults to the column's initial ordinal position.
\r
122 * A name which identifies this column. The id is used to create a CSS class name which
\r
123 * is applied to all table cells (including headers) in that column. The class name
\r
124 * takes the form of <pre>x-grid3-td-<b>id</b></pre>
\r
126 * Header cells will also recieve this class name, but will also have the class <pr>x-grid3-hd</pre>,
\r
127 * so to target header cells, use CSS selectors such as:<pre>.x-grid3-hd.x-grid3-td-<b>id</b></pre>
\r
128 * The {@link Ext.grid.GridPanel#autoExpandColumn} grid config option references the column
\r
129 * via this identifier.
\r
132 * @cfg {String} header The header text to display in the Grid view.
\r
135 * @cfg {String} dataIndex (optional) The name of the field in the grid's {@link Ext.data.Store}'s
\r
136 * {@link Ext.data.Record} definition from which to draw the column's value. If not
\r
137 * specified, the column's index is used as an index into the Record's data Array.
\r
140 * @cfg {Number} width (optional) The initial width in pixels of the column. This is ignored if the
\r
141 * Grid's {@link Ext.grid.GridView view} is configured with {@link Ext.grid.GridView#forceFit forceFit} true.
\r
144 * @cfg {Boolean} sortable (optional) True if sorting is to be allowed on this column.
\r
145 * Defaults to the value of the {@link #defaultSortable} property.
\r
146 * Whether local/remote sorting is used is specified in {@link Ext.data.Store#remoteSort}.
\r
149 * @cfg {Boolean} fixed (optional) True if the column width cannot be changed. Defaults to false.
\r
152 * @cfg {Boolean} resizable (optional) False to disable column resizing. Defaults to true.
\r
155 * @cfg {Boolean} menuDisabled (optional) True to disable the column menu. Defaults to false.
\r
158 * @cfg {Boolean} hidden (optional) True to hide the column. Defaults to false.
\r
161 * @cfg {String} tooltip (optional) A text string to use as the column header's tooltip. If Quicktips are enabled, this
\r
162 * value will be used as the text of the quick tip, otherwise it will be set as the header's HTML title attribute.
\r
166 * @cfg {Function} renderer (optional) A function used to generate HTML markup for a cell
\r
167 * given the cell's data value. See {@link #setRenderer}. If not specified, the
\r
168 * default renderer uses the raw data value.
\r
171 * @cfg {String} align (optional) Set the CSS text-align property of the column. Defaults to undefined.
\r
174 * @cfg {String} css (optional) Set custom CSS for all table cells in the column (excluding headers). Defaults to undefined.
\r
177 * @cfg {Boolean} hideable (optional) Specify as <tt>false</tt> to prevent the user from hiding this column
\r
178 * (defaults to true). To disallow column hiding globally for all columns in the grid, use
\r
179 * {@link Ext.grid.GridPanel#enableColumnHide} instead.
\r
182 * @cfg {Ext.form.Field} editor (optional) The {@link Ext.form.Field} to use when editing values in this column if
\r
183 * editing is supported by the grid.
\r
187 * Returns the id of the column at the specified index.
\r
188 * @param {Number} index The column index
\r
189 * @return {String} the id
\r
191 getColumnId : function(index){
\r
192 return this.config[index].id;
\r
196 * <p>Reconfigures this column model according to the passed Array of column definition objects. For a description of
\r
197 * the individual properties of a column definition object, see the <a href="#Ext.grid.ColumnModel-configs">Config Options</a>.</p>
\r
198 * <p>Causes the {@link #configchange} event to be fired. A {@link Ext.grid.GridPanel GridPanel} using
\r
199 * this ColumnModel will listen for this event and refresh its UI automatically.</p>
\r
200 * @param {Array} config Array of Column definition objects.
\r
202 setConfig : function(config, initial){
\r
203 if(!initial){ // cleanup
\r
204 delete this.totalWidth;
\r
205 for(var i = 0, len = this.config.length; i < len; i++){
\r
206 var c = this.config[i];
\r
208 c.editor.destroy();
\r
212 this.config = config;
\r
214 // if no id, create one
\r
215 for(var i = 0, len = config.length; i < len; i++){
\r
217 if(typeof c.renderer == "string"){
\r
218 c.renderer = Ext.util.Format[c.renderer];
\r
220 if(typeof c.id == "undefined"){
\r
223 if(c.editor && c.editor.isFormField){
\r
224 c.editor = new Ext.grid.GridEditor(c.editor);
\r
226 this.lookup[c.id] = c;
\r
229 this.fireEvent('configchange', this);
\r
234 * Returns the column for a specified id.
\r
235 * @param {String} id The column id
\r
236 * @return {Object} the column
\r
238 getColumnById : function(id){
\r
239 return this.lookup[id];
\r
243 * Returns the index for a specified column id.
\r
244 * @param {String} id The column id
\r
245 * @return {Number} the index, or -1 if not found
\r
247 getIndexById : function(id){
\r
248 for(var i = 0, len = this.config.length; i < len; i++){
\r
249 if(this.config[i].id == id){
\r
257 * Moves a column from one position to another.
\r
258 * @param {Number} oldIndex The index of the column to move.
\r
259 * @param {Number} newIndex The position at which to reinsert the coolumn.
\r
261 moveColumn : function(oldIndex, newIndex){
\r
262 var c = this.config[oldIndex];
\r
263 this.config.splice(oldIndex, 1);
\r
264 this.config.splice(newIndex, 0, c);
\r
265 this.dataMap = null;
\r
266 this.fireEvent("columnmoved", this, oldIndex, newIndex);
\r
269 // deprecated - to be removed
\r
270 isLocked : function(colIndex){
\r
271 return this.config[colIndex].locked === true;
\r
274 // deprecated - to be removed
\r
275 setLocked : function(colIndex, value, suppressEvent){
\r
276 if(this.isLocked(colIndex) == value){
\r
279 this.config[colIndex].locked = value;
\r
280 if(!suppressEvent){
\r
281 this.fireEvent("columnlockchange", this, colIndex, value);
\r
285 // deprecated - to be removed
\r
286 getTotalLockedWidth : function(){
\r
287 var totalWidth = 0;
\r
288 for(var i = 0; i < this.config.length; i++){
\r
289 if(this.isLocked(i) && !this.isHidden(i)){
\r
290 this.totalWidth += this.getColumnWidth(i);
\r
296 // deprecated - to be removed
\r
297 getLockedCount : function(){
\r
298 for(var i = 0, len = this.config.length; i < len; i++){
\r
299 if(!this.isLocked(i)){
\r
306 * Returns the number of columns.
\r
307 * @param {Boolean} visibleOnly Optional. Pass as true to only include visible columns.
\r
310 getColumnCount : function(visibleOnly){
\r
311 if(visibleOnly === true){
\r
313 for(var i = 0, len = this.config.length; i < len; i++){
\r
314 if(!this.isHidden(i)){
\r
320 return this.config.length;
\r
324 * Returns the column configs that return true by the passed function that is called with (columnConfig, index)
\r
325 * @param {Function} fn
\r
326 * @param {Object} scope (optional)
\r
327 * @return {Array} result
\r
329 getColumnsBy : function(fn, scope){
\r
331 for(var i = 0, len = this.config.length; i < len; i++){
\r
332 var c = this.config[i];
\r
333 if(fn.call(scope||this, c, i) === true){
\r
341 * Returns true if the specified column is sortable.
\r
342 * @param {Number} col The column index
\r
343 * @return {Boolean}
\r
345 isSortable : function(col){
\r
346 if(typeof this.config[col].sortable == "undefined"){
\r
347 return this.defaultSortable;
\r
349 return this.config[col].sortable;
\r
353 * Returns true if the specified column menu is disabled.
\r
354 * @param {Number} col The column index
\r
355 * @return {Boolean}
\r
357 isMenuDisabled : function(col){
\r
358 return !!this.config[col].menuDisabled;
\r
362 * Returns the rendering (formatting) function defined for the column.
\r
363 * @param {Number} col The column index.
\r
364 * @return {Function} The function used to render the cell. See {@link #setRenderer}.
\r
366 getRenderer : function(col){
\r
367 if(!this.config[col].renderer){
\r
368 return Ext.grid.ColumnModel.defaultRenderer;
\r
370 return this.config[col].renderer;
\r
374 * Sets the rendering (formatting) function for a column. See {@link Ext.util.Format} for some
\r
375 * default formatting functions.
\r
376 * @param {Number} col The column index
\r
377 * @param {Function} fn The function to use to process the cell's raw data
\r
378 * to return HTML markup for the grid view. The render function is called with
\r
379 * the following parameters:<ul>
\r
380 * <li><b>value</b> : Object<p class="sub-desc">The data value for the cell.</p></li>
\r
381 * <li><b>metadata</b> : Object<p class="sub-desc">An object in which you may set the following attributes:<ul>
\r
382 * <li><b>css</b> : String<p class="sub-desc">A CSS class name to add to the cell's TD element.</p></li>
\r
383 * <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
\r
384 * (e.g. 'style="color:red;"').</p></li></ul></p></li>
\r
385 * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record} from which the data was extracted.</p></li>
\r
386 * <li><b>rowIndex</b> : Number<p class="sub-desc">Row index</p></li>
\r
387 * <li><b>colIndex</b> : Number<p class="sub-desc">Column index</p></li>
\r
388 * <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>
\r
390 setRenderer : function(col, fn){
\r
391 this.config[col].renderer = fn;
\r
395 * Returns the width for the specified column.
\r
396 * @param {Number} col The column index
\r
399 getColumnWidth : function(col){
\r
400 return this.config[col].width || this.defaultWidth;
\r
404 * Sets the width for a column.
\r
405 * @param {Number} col The column index
\r
406 * @param {Number} width The new width
\r
408 setColumnWidth : function(col, width, suppressEvent){
\r
409 this.config[col].width = width;
\r
410 this.totalWidth = null;
\r
411 if(!suppressEvent){
\r
412 this.fireEvent("widthchange", this, col, width);
\r
417 * Returns the total width of all columns.
\r
418 * @param {Boolean} includeHidden True to include hidden column widths
\r
421 getTotalWidth : function(includeHidden){
\r
422 if(!this.totalWidth){
\r
423 this.totalWidth = 0;
\r
424 for(var i = 0, len = this.config.length; i < len; i++){
\r
425 if(includeHidden || !this.isHidden(i)){
\r
426 this.totalWidth += this.getColumnWidth(i);
\r
430 return this.totalWidth;
\r
434 * Returns the header for the specified column.
\r
435 * @param {Number} col The column index
\r
438 getColumnHeader : function(col){
\r
439 return this.config[col].header;
\r
443 * Sets the header for a column.
\r
444 * @param {Number} col The column index
\r
445 * @param {String} header The new header
\r
447 setColumnHeader : function(col, header){
\r
448 this.config[col].header = header;
\r
449 this.fireEvent("headerchange", this, col, header);
\r
453 * Returns the tooltip for the specified column.
\r
454 * @param {Number} col The column index
\r
457 getColumnTooltip : function(col){
\r
458 return this.config[col].tooltip;
\r
461 * Sets the tooltip for a column.
\r
462 * @param {Number} col The column index
\r
463 * @param {String} tooltip The new tooltip
\r
465 setColumnTooltip : function(col, tooltip){
\r
466 this.config[col].tooltip = tooltip;
\r
470 * Returns the dataIndex for the specified column.
\r
471 * @param {Number} col The column index
\r
472 * @return {String} The column's dataIndex
\r
474 getDataIndex : function(col){
\r
475 return this.config[col].dataIndex;
\r
479 * Sets the dataIndex for a column.
\r
480 * @param {Number} col The column index
\r
481 * @param {String} dataIndex The new dataIndex
\r
483 setDataIndex : function(col, dataIndex){
\r
484 this.config[col].dataIndex = dataIndex;
\r
488 * Finds the index of the first matching column for the given dataIndex.
\r
489 * @param {String} col The dataIndex to find
\r
490 * @return {Number} The column index, or -1 if no match was found
\r
492 findColumnIndex : function(dataIndex){
\r
493 var c = this.config;
\r
494 for(var i = 0, len = c.length; i < len; i++){
\r
495 if(c[i].dataIndex == dataIndex){
\r
503 * Returns true if the cell is editable.
\r
504 * @param {Number} colIndex The column index
\r
505 * @param {Number} rowIndex The row index
\r
506 * @return {Boolean}
\r
508 isCellEditable : function(colIndex, rowIndex){
\r
509 return (this.config[colIndex].editable || (typeof this.config[colIndex].editable == "undefined" && this.config[colIndex].editor)) ? true : false;
\r
513 * Returns the editor defined for the cell/column.
\r
514 * @param {Number} colIndex The column index
\r
515 * @param {Number} rowIndex The row index
\r
516 * @return {Ext.Editor} The {@link Ext.Editor Editor} that was created to wrap
\r
517 * the {@link Ext.form.Field Field} used to edit the cell.
\r
519 getCellEditor : function(colIndex, rowIndex){
\r
520 return this.config[colIndex].editor;
\r
524 * Sets if a column is editable.
\r
525 * @param {Number} col The column index
\r
526 * @param {Boolean} editable True if the column is editable
\r
528 setEditable : function(col, editable){
\r
529 this.config[col].editable = editable;
\r
534 * Returns true if the column is hidden.
\r
535 * @param {Number} colIndex The column index
\r
536 * @return {Boolean}
\r
538 isHidden : function(colIndex){
\r
539 return this.config[colIndex].hidden;
\r
544 * Returns true if the column width cannot be changed
\r
546 isFixed : function(colIndex){
\r
547 return this.config[colIndex].fixed;
\r
551 * Returns true if the column can be resized
\r
552 * @return {Boolean}
\r
554 isResizable : function(colIndex){
\r
555 return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
\r
558 * Sets if a column is hidden.
\r
559 * @param {Number} colIndex The column index
\r
560 * @param {Boolean} hidden True if the column is hidden
\r
562 setHidden : function(colIndex, hidden){
\r
563 var c = this.config[colIndex];
\r
564 if(c.hidden !== hidden){
\r
566 this.totalWidth = null;
\r
567 this.fireEvent("hiddenchange", this, colIndex, hidden);
\r
572 * Sets the editor for a column.
\r
573 * @param {Number} col The column index
\r
574 * @param {Object} editor The editor object
\r
576 setEditor : function(col, editor){
\r
577 this.config[col].editor = editor;
\r
582 Ext.grid.ColumnModel.defaultRenderer = function(value){
\r
583 if(typeof value == "string" && value.length < 1){
\r
589 // Alias for backwards compatibility
\r
590 Ext.grid.DefaultColumnModel = Ext.grid.ColumnModel;