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
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++){
49 if(typeof c.dataIndex == "undefined"){
52 if(typeof c.renderer == "string"){
53 c.renderer = Roo.util.Format[c.renderer];
55 if(typeof c.id == "undefined"){
58 if(c.editor && c.editor.xtype){
59 c.editor = Roo.factory(c.editor, Roo.grid);
61 if(c.editor && c.editor.isFormField){
62 c.editor = new Roo.grid.GridEditor(c.editor);
64 this.lookup[c.id] = c;
68 * The width of columns which have no width specified (defaults to 100)
71 this.defaultWidth = 100;
74 * Default sortable of columns which have no sortable specified (defaults to false)
77 this.defaultSortable = false;
82 * Fires when the width of a column changes.
83 * @param {ColumnModel} this
84 * @param {Number} columnIndex The column index
85 * @param {Number} newWidth The new width
90 * Fires when the text of a header changes.
91 * @param {ColumnModel} this
92 * @param {Number} columnIndex The column index
93 * @param {Number} newText The new header text
98 * Fires when a column is hidden or "unhidden".
99 * @param {ColumnModel} this
100 * @param {Number} columnIndex The column index
101 * @param {Boolean} hidden true if hidden, false otherwise
103 "hiddenchange": true,
106 * Fires when a column is moved.
107 * @param {ColumnModel} this
108 * @param {Number} oldIndex
109 * @param {Number} newIndex
111 "columnmoved" : true,
113 * @event columlockchange
114 * Fires when a column's locked state is changed
115 * @param {ColumnModel} this
116 * @param {Number} colIndex
117 * @param {Boolean} locked true if locked
119 "columnlockchange" : true
121 Roo.grid.ColumnModel.superclass.constructor.call(this);
123 Roo.extend(Roo.grid.ColumnModel, Roo.util.Observable, {
125 * @cfg {String} header The header text to display in the Grid view.
128 * @cfg {String} dataIndex (Optional) 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 (Optional) 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 (Optional) 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 (Optional) True to lock the column in place while scrolling the Grid. Defaults to false.
145 * @cfg {Boolean} fixed (Optional) True if the column width cannot be changed. Defaults to false.
148 * @cfg {Boolean} resizable (Optional) False to disable column resizing. Defaults to true.
151 * @cfg {Boolean} hidden (Optional) True to hide the column. Defaults to false.
154 * @cfg {Function} renderer (Optional) 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 uses the raw 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 (Optional) For grid editors - returns the grid editor
163 * @cfg {String} align (Optional) Set the CSS text-align property of the column. Defaults to undefined.
166 * @cfg {String} cursor (Optional)
169 * @cfg {String} tooltip (Optional)
172 * @cfg {Number} xs (Optional)
175 * @cfg {Number} sm (Optional)
178 * @cfg {Number} md (Optional)
181 * @cfg {Number} lg (Optional)
184 * Returns the id of the column at the specified index.
185 * @param {Number} index The column index
186 * @return {String} the id
188 getColumnId : function(index){
189 return this.config[index].id;
193 * Returns the column for a specified id.
194 * @param {String} id The column id
195 * @return {Object} the column
197 getColumnById : function(id){
198 return this.lookup[id];
203 * Returns the column for a specified dataIndex.
204 * @param {String} dataIndex The column dataIndex
205 * @return {Object|Boolean} the column or false if not found
207 getColumnByDataIndex: function(dataIndex){
208 var index = this.findColumnIndex(dataIndex);
209 return index > -1 ? this.config[index] : false;
213 * Returns the index for a specified column id.
214 * @param {String} id The column id
215 * @return {Number} the index, or -1 if not found
217 getIndexById : function(id){
218 for(var i = 0, len = this.config.length; i < len; i++){
219 if(this.config[i].id == id){
227 * Returns the index for a specified column dataIndex.
228 * @param {String} dataIndex The column dataIndex
229 * @return {Number} the index, or -1 if not found
232 findColumnIndex : function(dataIndex){
233 for(var i = 0, len = this.config.length; i < len; i++){
234 if(this.config[i].dataIndex == dataIndex){
242 moveColumn : function(oldIndex, newIndex){
243 var c = this.config[oldIndex];
244 this.config.splice(oldIndex, 1);
245 this.config.splice(newIndex, 0, c);
247 this.fireEvent("columnmoved", this, oldIndex, newIndex);
250 isLocked : function(colIndex){
253 return this.config[colIndex].locked === true;
256 setLocked : function(colIndex, value, suppressEvent){
257 if(this.isLocked(colIndex) == value){
260 this.config[colIndex].locked = value;
262 this.fireEvent("columnlockchange", this, colIndex, value);
266 getTotalLockedWidth : function(){
268 for(var i = 0; i < this.config.length; i++){
269 if(this.isLocked(i) && !this.isHidden(i)){
270 this.totalWidth += this.getColumnWidth(i);
276 getLockedCount : function(){
277 Roo.log('getLockedCOunt');
278 Roo.log(this.config.length);
279 for(var i = 0, len = this.config.length; i < len; i++){
280 if(!this.isLocked(i)){
287 * Returns the number of columns.
290 getColumnCount : function(visibleOnly){
291 if(visibleOnly === true){
293 for(var i = 0, len = this.config.length; i < len; i++){
294 if(!this.isHidden(i)){
300 return this.config.length;
304 * Returns the column configs that return true by the passed function that is called with (columnConfig, index)
305 * @param {Function} fn
306 * @param {Object} scope (optional)
307 * @return {Array} result
309 getColumnsBy : function(fn, scope){
311 for(var i = 0, len = this.config.length; i < len; i++){
312 var c = this.config[i];
313 if(fn.call(scope||this, c, i) === true){
321 * Returns true if the specified column is sortable.
322 * @param {Number} col The column index
325 isSortable : function(col){
326 if(typeof this.config[col].sortable == "undefined"){
327 return this.defaultSortable;
329 return this.config[col].sortable;
333 * Returns the rendering (formatting) function defined for the column.
334 * @param {Number} col The column index.
335 * @return {Function} The function used to render the cell. See {@link #setRenderer}.
337 getRenderer : function(col){
338 if(!this.config[col].renderer){
339 return Roo.grid.ColumnModel.defaultRenderer;
341 return this.config[col].renderer;
345 * Sets the rendering (formatting) function for a column.
346 * @param {Number} col The column index
347 * @param {Function} fn The function to use to process the cell's raw data
348 * to return HTML markup for the grid view. The render function is called with
349 * the following parameters:<ul>
350 * <li>Data value.</li>
351 * <li>Cell metadata. An object in which you may set the following attributes:<ul>
352 * <li>css A CSS style string to apply to the table cell.</li>
353 * <li>attr An HTML attribute definition string to apply to the data container element <i>within</i> the table cell.</li></ul>
354 * <li>The {@link Roo.data.Record} from which the data was extracted.</li>
356 * <li>Column index</li>
357 * <li>The {@link Roo.data.Store} object from which the Record was extracted</li></ul>
359 setRenderer : function(col, fn){
360 this.config[col].renderer = fn;
364 * Returns the width for the specified column.
365 * @param {Number} col The column index
368 getColumnWidth : function(col){
369 return this.config[col].width * 1 || this.defaultWidth;
373 * Sets the width for a column.
374 * @param {Number} col The column index
375 * @param {Number} width The new width
377 setColumnWidth : function(col, width, suppressEvent){
378 this.config[col].width = width;
379 this.totalWidth = null;
381 this.fireEvent("widthchange", this, col, width);
386 * Returns the total width of all columns.
387 * @param {Boolean} includeHidden True to include hidden column widths
390 getTotalWidth : function(includeHidden){
391 if(!this.totalWidth){
393 for(var i = 0, len = this.config.length; i < len; i++){
394 if(includeHidden || !this.isHidden(i)){
395 this.totalWidth += this.getColumnWidth(i);
399 return this.totalWidth;
403 * Returns the header for the specified column.
404 * @param {Number} col The column index
407 getColumnHeader : function(col){
408 return this.config[col].header;
412 * Sets the header for a column.
413 * @param {Number} col The column index
414 * @param {String} header The new header
416 setColumnHeader : function(col, header){
417 this.config[col].header = header;
418 this.fireEvent("headerchange", this, col, header);
422 * Returns the tooltip for the specified column.
423 * @param {Number} col The column index
426 getColumnTooltip : function(col){
427 return this.config[col].tooltip;
430 * Sets the tooltip for a column.
431 * @param {Number} col The column index
432 * @param {String} tooltip The new tooltip
434 setColumnTooltip : function(col, tooltip){
435 this.config[col].tooltip = tooltip;
439 * Returns the dataIndex for the specified column.
440 * @param {Number} col The column index
443 getDataIndex : function(col){
444 return this.config[col].dataIndex;
448 * Sets the dataIndex for a column.
449 * @param {Number} col The column index
450 * @param {Number} dataIndex The new dataIndex
452 setDataIndex : function(col, dataIndex){
453 this.config[col].dataIndex = dataIndex;
459 * Returns true if the cell is editable.
460 * @param {Number} colIndex The column index
461 * @param {Number} rowIndex The row index - this is nto actually used..?
464 isCellEditable : function(colIndex, rowIndex){
465 return (this.config[colIndex].editable || (typeof this.config[colIndex].editable == "undefined" && this.config[colIndex].editor)) ? true : false;
469 * Returns the editor defined for the cell/column.
470 * return false or null to disable editing.
471 * @param {Number} colIndex The column index
472 * @param {Number} rowIndex The row index
475 getCellEditor : function(colIndex, rowIndex){
476 return this.config[colIndex].editor;
480 * Sets if a column is editable.
481 * @param {Number} col The column index
482 * @param {Boolean} editable True if the column is editable
484 setEditable : function(col, editable){
485 this.config[col].editable = editable;
490 * Returns true if the column is hidden.
491 * @param {Number} colIndex The column index
494 isHidden : function(colIndex){
495 return this.config[colIndex].hidden;
500 * Returns true if the column width cannot be changed
502 isFixed : function(colIndex){
503 return this.config[colIndex].fixed;
507 * Returns true if the column can be resized
510 isResizable : function(colIndex){
511 return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
514 * Sets if a column is hidden.
515 * @param {Number} colIndex The column index
516 * @param {Boolean} hidden True if the column is hidden
518 setHidden : function(colIndex, hidden){
519 this.config[colIndex].hidden = hidden;
520 this.totalWidth = null;
521 this.fireEvent("hiddenchange", this, colIndex, hidden);
525 * Sets the editor for a column.
526 * @param {Number} col The column index
527 * @param {Object} editor The editor object
529 setEditor : function(col, editor){
530 this.config[col].editor = editor;
534 Roo.grid.ColumnModel.defaultRenderer = function(value){
535 if(typeof value == "string" && value.length < 1){
541 // Alias for backwards compatibility
542 Roo.grid.DefaultColumnModel = Roo.grid.ColumnModel;