4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * @class Roo.grid.ColumnModel
15 * @extends Roo.util.Observable
16 * This is the default implementation of a ColumnModel used by the Grid. It defines
17 * the columns in the grid.
20 var colModel = new Roo.grid.ColumnModel([
21 {header: "Ticker", width: 60, sortable: true, locked: true},
22 {header: "Company Name", width: 150, sortable: true},
23 {header: "Market Cap.", width: 100, sortable: true},
24 {header: "$ Sales", width: 100, sortable: true, renderer: money},
25 {header: "Employees", width: 100, sortable: true, resizable: false}
30 * The config options listed for this class are options which may appear in each
31 * individual column definition.
32 * <br/>RooJS Fix - column id's are not sequential but use Roo.id() - fixes bugs with layouts.
34 * @param {Object} config An Array of column config objects. See this class's
35 * config objects for details.
37 Roo.grid.ColumnModel = function(config){
39 * The config passed into the constructor
41 this.config = []; //config;
44 // if no id, create one
45 // if the column does not have a dataIndex mapping,
46 // map it to the order it is in the config
47 for(var i = 0, len = config.length; i < len; i++){
48 this.addColumn(config[i]);
53 * The width of columns which have no width specified (defaults to 100)
56 this.defaultWidth = 100;
59 * Default sortable of columns which have no sortable specified (defaults to false)
62 this.defaultSortable = false;
67 * Fires when the width of a column changes.
68 * @param {ColumnModel} this
69 * @param {Number} columnIndex The column index
70 * @param {Number} newWidth The new width
75 * Fires when the text of a header changes.
76 * @param {ColumnModel} this
77 * @param {Number} columnIndex The column index
78 * @param {Number} newText The new header text
83 * Fires when a column is hidden or "unhidden".
84 * @param {ColumnModel} this
85 * @param {Number} columnIndex The column index
86 * @param {Boolean} hidden true if hidden, false otherwise
91 * Fires when a column is moved.
92 * @param {ColumnModel} this
93 * @param {Number} oldIndex
94 * @param {Number} newIndex
98 * @event columlockchange
99 * Fires when a column's locked state is changed
100 * @param {ColumnModel} this
101 * @param {Number} colIndex
102 * @param {Boolean} locked true if locked
104 "columnlockchange" : true
106 Roo.grid.ColumnModel.superclass.constructor.call(this);
108 Roo.extend(Roo.grid.ColumnModel, Roo.util.Observable, {
110 * @cfg {String} header [required] The header text to display in the Grid view.
113 * @cfg {String} xsHeader Header at Bootsrap Extra Small width (default for all)
116 * @cfg {String} smHeader Header at Bootsrap Small width
119 * @cfg {String} mdHeader Header at Bootsrap Medium width
122 * @cfg {String} lgHeader Header at Bootsrap Large width
125 * @cfg {String} xlHeader Header at Bootsrap extra Large width
128 * @cfg {String} dataIndex The name of the field in the grid's {@link Roo.data.Store}'s
129 * {@link Roo.data.Record} definition from which to draw the column's value. If not
130 * specified, the column's index is used as an index into the Record's data Array.
133 * @cfg {Number} width The initial width in pixels of the column. Using this
134 * instead of {@link Roo.grid.Grid#autoSizeColumns} is more efficient.
137 * @cfg {Boolean} sortable True if sorting is to be allowed on this column.
138 * Defaults to the value of the {@link #defaultSortable} property.
139 * Whether local/remote sorting is used is specified in {@link Roo.data.Store#remoteSort}.
142 * @cfg {Boolean} locked True to lock the column in place while scrolling the Grid. Defaults to false.
145 * @cfg {Boolean} fixed True if the column width cannot be changed. Defaults to false.
148 * @cfg {Boolean} resizable False to disable column resizing. Defaults to true.
151 * @cfg {Boolean} hidden True to hide the column. Defaults to false.
154 * @cfg {Function} renderer A function used to generate HTML markup for a cell
155 * given the cell's data value. See {@link #setRenderer}. If not specified, the
156 * default renderer returns the escaped data value. If an object is returned (bootstrap only)
157 * then it is treated as a Roo Component object instance, and it is rendered after the initial row is rendered
160 * @cfg {Roo.grid.GridEditor} editor For grid editors - returns the grid editor
163 * @cfg {String} align (left|right) Set the CSS text-align property of the column. Defaults to undefined (left).
166 * @cfg {String} valign (top|bottom|middle) Set the CSS vertical-align property of the column (eg. middle, top, bottom etc). Defaults to undefined (middle)
169 * @cfg {String} cursor ( auto|default|none|context-menu|help|pointer|progress|wait|cell|crosshair|text|vertical-text|alias|copy|move|no-drop|not-allowed|e-resize|n-resize|ne-resize|nw-resize|s-resize|se-resize|sw-resize|w-resize|ew-resize|ns-resize|nesw-resize|nwse-resize|col-resize|row-resize|all-scroll|zoom-in|zoom-out|grab|grabbing)
172 * @cfg {String} tooltip mouse over tooltip text
175 * @cfg {Number} xs can be '0' for hidden at this size (number less than 12)
178 * @cfg {Number} sm can be '0' for hidden at this size (number less than 12)
181 * @cfg {Number} md can be '0' for hidden at this size (number less than 12)
184 * @cfg {Number} lg can be '0' for hidden at this size (number less than 12)
187 * @cfg {Number} xl can be '0' for hidden at this size (number less than 12)
190 * Returns the id of the column at the specified index.
191 * @param {Number} index The column index
192 * @return {String} the id
194 getColumnId : function(index){
195 return this.config[index].id;
199 * Returns the column for a specified id.
200 * @param {String} id The column id
201 * @return {Object} the column
203 getColumnById : function(id){
204 return this.lookup[id];
209 * Returns the column Object for a specified dataIndex.
210 * @param {String} dataIndex The column dataIndex
211 * @return {Object|Boolean} the column or false if not found
213 getColumnByDataIndex: function(dataIndex){
214 var index = this.findColumnIndex(dataIndex);
215 return index > -1 ? this.config[index] : false;
219 * Returns the index for a specified column id.
220 * @param {String} id The column id
221 * @return {Number} the index, or -1 if not found
223 getIndexById : function(id){
224 for(var i = 0, len = this.config.length; i < len; i++){
225 if(this.config[i].id == id){
233 * Returns the index for a specified column dataIndex.
234 * @param {String} dataIndex The column dataIndex
235 * @return {Number} the index, or -1 if not found
238 findColumnIndex : function(dataIndex){
239 for(var i = 0, len = this.config.length; i < len; i++){
240 if(this.config[i].dataIndex == dataIndex){
248 moveColumn : function(oldIndex, newIndex){
249 var c = this.config[oldIndex];
250 this.config.splice(oldIndex, 1);
251 this.config.splice(newIndex, 0, c);
253 this.fireEvent("columnmoved", this, oldIndex, newIndex);
256 isLocked : function(colIndex){
257 return this.config[colIndex].locked === true;
260 setLocked : function(colIndex, value, suppressEvent){
261 if(this.isLocked(colIndex) == value){
264 this.config[colIndex].locked = value;
266 this.fireEvent("columnlockchange", this, colIndex, value);
270 getTotalLockedWidth : function(){
272 for(var i = 0; i < this.config.length; i++){
273 if(this.isLocked(i) && !this.isHidden(i)){
274 this.totalWidth += this.getColumnWidth(i);
280 getLockedCount : function(){
281 for(var i = 0, len = this.config.length; i < len; i++){
282 if(!this.isLocked(i)){
287 return this.config.length;
291 * Returns the number of columns.
294 getColumnCount : function(visibleOnly){
295 if(visibleOnly === true){
297 for(var i = 0, len = this.config.length; i < len; i++){
298 if(!this.isHidden(i)){
304 return this.config.length;
308 * Returns the column configs that return true by the passed function that is called with (columnConfig, index)
309 * @param {Function} fn
310 * @param {Object} scope (optional)
311 * @return {Array} result
313 getColumnsBy : function(fn, scope){
315 for(var i = 0, len = this.config.length; i < len; i++){
316 var c = this.config[i];
317 if(fn.call(scope||this, c, i) === true){
325 * Returns true if the specified column is sortable.
326 * @param {Number} col The column index
329 isSortable : function(col){
330 if(typeof this.config[col].sortable == "undefined"){
331 return this.defaultSortable;
333 return this.config[col].sortable;
337 * Returns the rendering (formatting) function defined for the column.
338 * @param {Number} col The column index.
339 * @return {Function} The function used to render the cell. See {@link #setRenderer}.
341 getRenderer : function(col){
342 if(!this.config[col].renderer){
343 return Roo.grid.ColumnModel.defaultRenderer;
345 return this.config[col].renderer;
349 * Sets the rendering (formatting) function for a column.
350 * @param {Number} col The column index
351 * @param {Function} fn The function to use to process the cell's raw data
352 * to return HTML markup for the grid view. The render function is called with
353 * the following parameters:<ul>
354 * <li>Data value.</li>
355 * <li>Cell metadata. An object in which you may set the following attributes:<ul>
356 * <li>css A CSS style string to apply to the table cell.</li>
357 * <li>attr An HTML attribute definition string to apply to the data container element <i>within</i> the table cell.</li></ul>
358 * <li>The {@link Roo.data.Record} from which the data was extracted.</li>
360 * <li>Column index</li>
361 * <li>The {@link Roo.data.Store} object from which the Record was extracted</li></ul>
363 setRenderer : function(col, fn){
364 this.config[col].renderer = fn;
368 * Returns the width for the specified column.
369 * @param {Number} col The column index
370 * @param (optional) {String} gridSize bootstrap width size.
373 getColumnWidth : function(col, gridSize)
375 var cfg = this.config[col];
377 if (typeof(gridSize) == 'undefined') {
378 return cfg.width * 1 || this.defaultWidth;
380 if (gridSize === false) { // if we set it..
381 return cfg.width || false;
383 var sizes = ['xl', 'lg', 'md', 'sm', 'xs'];
385 for(var i = sizes.indexOf(gridSize); i < sizes.length; i++) {
386 if (typeof(cfg[ sizes[i] ] ) == 'undefined') {
389 return cfg[ sizes[i] ];
396 * Sets the width for a column.
397 * @param {Number} col The column index
398 * @param {Number} width The new width
400 setColumnWidth : function(col, width, suppressEvent){
401 this.config[col].width = width;
402 this.totalWidth = null;
404 this.fireEvent("widthchange", this, col, width);
409 * Returns the total width of all columns.
410 * @param {Boolean} includeHidden True to include hidden column widths
413 getTotalWidth : function(includeHidden){
414 if(!this.totalWidth){
416 for(var i = 0, len = this.config.length; i < len; i++){
417 if(includeHidden || !this.isHidden(i)){
418 this.totalWidth += this.getColumnWidth(i);
422 return this.totalWidth;
426 * Returns the header for the specified column.
427 * @param {Number} col The column index
430 getColumnHeader : function(col){
431 return this.config[col].header;
435 * Sets the header for a column.
436 * @param {Number} col The column index
437 * @param {String} header The new header
439 setColumnHeader : function(col, header){
440 this.config[col].header = header;
441 this.fireEvent("headerchange", this, col, header);
445 * Returns the tooltip for the specified column.
446 * @param {Number} col The column index
449 getColumnTooltip : function(col){
450 return this.config[col].tooltip;
453 * Sets the tooltip for a column.
454 * @param {Number} col The column index
455 * @param {String} tooltip The new tooltip
457 setColumnTooltip : function(col, tooltip){
458 this.config[col].tooltip = tooltip;
462 * Returns the dataIndex for the specified column.
463 * @param {Number} col The column index
466 getDataIndex : function(col){
467 return this.config[col].dataIndex;
471 * Sets the dataIndex for a column.
472 * @param {Number} col The column index
473 * @param {Number} dataIndex The new dataIndex
475 setDataIndex : function(col, dataIndex){
476 this.config[col].dataIndex = dataIndex;
482 * Returns true if the cell is editable.
483 * @param {Number} colIndex The column index
484 * @param {Number} rowIndex The row index - this is nto actually used..?
487 isCellEditable : function(colIndex, rowIndex){
488 return (this.config[colIndex].editable || (typeof this.config[colIndex].editable == "undefined" && this.config[colIndex].editor)) ? true : false;
492 * Returns the editor defined for the cell/column.
493 * return false or null to disable editing.
494 * @param {Number} colIndex The column index
495 * @param {Number} rowIndex The row index
498 getCellEditor : function(colIndex, rowIndex){
499 return this.config[colIndex].editor;
503 * Sets if a column is editable.
504 * @param {Number} col The column index
505 * @param {Boolean} editable True if the column is editable
507 setEditable : function(col, editable){
508 this.config[col].editable = editable;
513 * Returns true if the column is hidden.
514 * @param {Number} colIndex The column index
517 isHidden : function(colIndex){
518 return this.config[colIndex].hidden;
523 * Returns true if the column width cannot be changed
525 isFixed : function(colIndex){
526 return this.config[colIndex].fixed;
530 * Returns true if the column can be resized
533 isResizable : function(colIndex){
534 return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
537 * Sets if a column is hidden.
538 * @param {Number} colIndex The column index
539 * @param {Boolean} hidden True if the column is hidden
541 setHidden : function(colIndex, hidden){
542 this.config[colIndex].hidden = hidden;
543 this.totalWidth = null;
544 this.fireEvent("hiddenchange", this, colIndex, hidden);
548 * Sets the editor for a column.
549 * @param {Number} col The column index
550 * @param {Object} editor The editor object
552 setEditor : function(col, editor){
553 this.config[col].editor = editor;
556 * Add a column (experimental...) - defaults to adding to the end..
557 * @param {Object} config
559 addColumn : function(c)
562 var i = this.config.length;
565 if(typeof c.dataIndex == "undefined"){
568 if(typeof c.renderer == "string"){
569 c.renderer = Roo.util.Format[c.renderer];
571 if(typeof c.id == "undefined"){
574 if(c.editor && c.editor.xtype){
575 c.editor = Roo.factory(c.editor, Roo.grid);
577 if(c.editor && c.editor.isFormField){
578 c.editor = new Roo.grid.GridEditor(c.editor);
580 this.lookup[c.id] = c;
585 Roo.grid.ColumnModel.defaultRenderer = function(value)
587 if(typeof value == "object") {
590 if(typeof value == "string" && value.length < 1){
594 return String.format("{0}", value);
597 // Alias for backwards compatibility
598 Roo.grid.DefaultColumnModel = Roo.grid.ColumnModel;