Roo/grid/ColumnModel.js
[roojs1] / Roo / grid / ColumnModel.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12
13 /**
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.
18  * <br>Usage:<br>
19  <pre><code>
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}
26  ]);
27  </code></pre>
28  * <p>
29  
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.
33  * @constructor
34  * @param {Object} config An Array of column config objects. See this class's
35  * config objects for details.
36 */
37 Roo.grid.ColumnModel = function(config){
38         /**
39      * The config passed into the constructor
40      */
41     this.config = config;
42     this.lookup = {};
43
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         var c = config[i];
49         if(typeof c.dataIndex == "undefined"){
50             c.dataIndex = i;
51         }
52         if(typeof c.renderer == "string"){
53             c.renderer = Roo.util.Format[c.renderer];
54         }
55         if(typeof c.id == "undefined"){
56             c.id = Roo.id();
57         }
58         if(c.editor && c.editor.xtype){
59             c.editor  = Roo.factory(c.editor, Roo.grid);
60         }
61         if(c.editor && c.editor.isFormField){
62             c.editor = new Roo.grid.GridEditor(c.editor);
63         }
64         this.lookup[c.id] = c;
65     }
66
67     /**
68      * The width of columns which have no width specified (defaults to 100)
69      * @type Number
70      */
71     this.defaultWidth = 100;
72
73     /**
74      * Default sortable of columns which have no sortable specified (defaults to false)
75      * @type Boolean
76      */
77     this.defaultSortable = false;
78
79     this.addEvents({
80         /**
81              * @event widthchange
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
86              */
87             "widthchange": true,
88         /**
89              * @event headerchange
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
94              */
95             "headerchange": true,
96         /**
97              * @event hiddenchange
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
102              */
103             "hiddenchange": true,
104             /**
105          * @event columnmoved
106          * Fires when a column is moved.
107          * @param {ColumnModel} this
108          * @param {Number} oldIndex
109          * @param {Number} newIndex
110          */
111         "columnmoved" : true,
112         /**
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
118          */
119         "columnlockchange" : true
120     });
121     Roo.grid.ColumnModel.superclass.constructor.call(this);
122 };
123 Roo.extend(Roo.grid.ColumnModel, Roo.util.Observable, {
124     /**
125      * @cfg {String} header The header text to display in the Grid view.
126      */
127     /**
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.
131      */
132     /**
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.
135      */
136     /**
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}.
140      */
141     /**
142      * @cfg {Boolean} locked (Optional) True to lock the column in place while scrolling the Grid.  Defaults to false.
143      */
144     /**
145      * @cfg {Boolean} fixed (Optional) True if the column width cannot be changed.  Defaults to false.
146      */
147     /**
148      * @cfg {Boolean} resizable (Optional) False to disable column resizing. Defaults to true.
149      */
150     /**
151      * @cfg {Boolean} hidden (Optional) True to hide the column. Defaults to false.
152      */
153     /**
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
158      */
159        /**
160      * @cfg {Roo.grid.GridEditor} editor (Optional) For grid editors - returns the grid editor 
161      */
162     /**
163      * @cfg {String} align (Optional) Set the CSS text-align property of the column.  Defaults to undefined.
164      */
165     /**
166      * @cfg {String} cursor (Optional)
167      */
168     /**
169      * Returns the id of the column at the specified index.
170      * @param {Number} index The column index
171      * @return {String} the id
172      */
173     getColumnId : function(index){
174         return this.config[index].id;
175     },
176
177     /**
178      * Returns the column for a specified id.
179      * @param {String} id The column id
180      * @return {Object} the column
181      */
182     getColumnById : function(id){
183         return this.lookup[id];
184     },
185
186     
187     /**
188      * Returns the column for a specified dataIndex.
189      * @param {String} dataIndex The column dataIndex
190      * @return {Object|Boolean} the column or false if not found
191      */
192     getColumnByDataIndex: function(dataIndex){
193         var index = this.findColumnIndex(dataIndex);
194         return index > -1 ? this.config[index] : false;
195     },
196     
197     /**
198      * Returns the index for a specified column id.
199      * @param {String} id The column id
200      * @return {Number} the index, or -1 if not found
201      */
202     getIndexById : function(id){
203         for(var i = 0, len = this.config.length; i < len; i++){
204             if(this.config[i].id == id){
205                 return i;
206             }
207         }
208         return -1;
209     },
210     
211     /**
212      * Returns the index for a specified column dataIndex.
213      * @param {String} dataIndex The column dataIndex
214      * @return {Number} the index, or -1 if not found
215      */
216     
217     findColumnIndex : function(dataIndex){
218         for(var i = 0, len = this.config.length; i < len; i++){
219             if(this.config[i].dataIndex == dataIndex){
220                 return i;
221             }
222         }
223         return -1;
224     },
225     
226     
227     moveColumn : function(oldIndex, newIndex){
228         var c = this.config[oldIndex];
229         this.config.splice(oldIndex, 1);
230         this.config.splice(newIndex, 0, c);
231         this.dataMap = null;
232         this.fireEvent("columnmoved", this, oldIndex, newIndex);
233     },
234
235     isLocked : function(colIndex){
236         return this.config[colIndex].locked === true;
237     },
238
239     setLocked : function(colIndex, value, suppressEvent){
240         if(this.isLocked(colIndex) == value){
241             return;
242         }
243         this.config[colIndex].locked = value;
244         if(!suppressEvent){
245             this.fireEvent("columnlockchange", this, colIndex, value);
246         }
247     },
248
249     getTotalLockedWidth : function(){
250         var totalWidth = 0;
251         for(var i = 0; i < this.config.length; i++){
252             if(this.isLocked(i) && !this.isHidden(i)){
253                 this.totalWidth += this.getColumnWidth(i);
254             }
255         }
256         return totalWidth;
257     },
258
259     getLockedCount : function(){
260         for(var i = 0, len = this.config.length; i < len; i++){
261             if(!this.isLocked(i)){
262                 return i;
263             }
264         }
265     },
266
267     /**
268      * Returns the number of columns.
269      * @return {Number}
270      */
271     getColumnCount : function(visibleOnly){
272         if(visibleOnly === true){
273             var c = 0;
274             for(var i = 0, len = this.config.length; i < len; i++){
275                 if(!this.isHidden(i)){
276                     c++;
277                 }
278             }
279             return c;
280         }
281         return this.config.length;
282     },
283
284     /**
285      * Returns the column configs that return true by the passed function that is called with (columnConfig, index)
286      * @param {Function} fn
287      * @param {Object} scope (optional)
288      * @return {Array} result
289      */
290     getColumnsBy : function(fn, scope){
291         var r = [];
292         for(var i = 0, len = this.config.length; i < len; i++){
293             var c = this.config[i];
294             if(fn.call(scope||this, c, i) === true){
295                 r[r.length] = c;
296             }
297         }
298         return r;
299     },
300
301     /**
302      * Returns true if the specified column is sortable.
303      * @param {Number} col The column index
304      * @return {Boolean}
305      */
306     isSortable : function(col){
307         if(typeof this.config[col].sortable == "undefined"){
308             return this.defaultSortable;
309         }
310         return this.config[col].sortable;
311     },
312
313     /**
314      * Returns the rendering (formatting) function defined for the column.
315      * @param {Number} col The column index.
316      * @return {Function} The function used to render the cell. See {@link #setRenderer}.
317      */
318     getRenderer : function(col){
319         if(!this.config[col].renderer){
320             return Roo.grid.ColumnModel.defaultRenderer;
321         }
322         return this.config[col].renderer;
323     },
324
325     /**
326      * Sets the rendering (formatting) function for a column.
327      * @param {Number} col The column index
328      * @param {Function} fn The function to use to process the cell's raw data
329      * to return HTML markup for the grid view. The render function is called with
330      * the following parameters:<ul>
331      * <li>Data value.</li>
332      * <li>Cell metadata. An object in which you may set the following attributes:<ul>
333      * <li>css A CSS style string to apply to the table cell.</li>
334      * <li>attr An HTML attribute definition string to apply to the data container element <i>within</i> the table cell.</li></ul>
335      * <li>The {@link Roo.data.Record} from which the data was extracted.</li>
336      * <li>Row index</li>
337      * <li>Column index</li>
338      * <li>The {@link Roo.data.Store} object from which the Record was extracted</li></ul>
339      */
340     setRenderer : function(col, fn){
341         this.config[col].renderer = fn;
342     },
343
344     /**
345      * Returns the width for the specified column.
346      * @param {Number} col The column index
347      * @return {Number}
348      */
349     getColumnWidth : function(col){
350         return this.config[col].width * 1 || this.defaultWidth;
351     },
352
353     /**
354      * Sets the width for a column.
355      * @param {Number} col The column index
356      * @param {Number} width The new width
357      */
358     setColumnWidth : function(col, width, suppressEvent){
359         this.config[col].width = width;
360         this.totalWidth = null;
361         if(!suppressEvent){
362              this.fireEvent("widthchange", this, col, width);
363         }
364     },
365
366     /**
367      * Returns the total width of all columns.
368      * @param {Boolean} includeHidden True to include hidden column widths
369      * @return {Number}
370      */
371     getTotalWidth : function(includeHidden){
372         if(!this.totalWidth){
373             this.totalWidth = 0;
374             for(var i = 0, len = this.config.length; i < len; i++){
375                 if(includeHidden || !this.isHidden(i)){
376                     this.totalWidth += this.getColumnWidth(i);
377                 }
378             }
379         }
380         return this.totalWidth;
381     },
382
383     /**
384      * Returns the header for the specified column.
385      * @param {Number} col The column index
386      * @return {String}
387      */
388     getColumnHeader : function(col){
389         return this.config[col].header;
390     },
391
392     /**
393      * Sets the header for a column.
394      * @param {Number} col The column index
395      * @param {String} header The new header
396      */
397     setColumnHeader : function(col, header){
398         this.config[col].header = header;
399         this.fireEvent("headerchange", this, col, header);
400     },
401
402     /**
403      * Returns the tooltip for the specified column.
404      * @param {Number} col The column index
405      * @return {String}
406      */
407     getColumnTooltip : function(col){
408             return this.config[col].tooltip;
409     },
410     /**
411      * Sets the tooltip for a column.
412      * @param {Number} col The column index
413      * @param {String} tooltip The new tooltip
414      */
415     setColumnTooltip : function(col, tooltip){
416             this.config[col].tooltip = tooltip;
417     },
418
419     /**
420      * Returns the dataIndex for the specified column.
421      * @param {Number} col The column index
422      * @return {Number}
423      */
424     getDataIndex : function(col){
425         return this.config[col].dataIndex;
426     },
427
428     /**
429      * Sets the dataIndex for a column.
430      * @param {Number} col The column index
431      * @param {Number} dataIndex The new dataIndex
432      */
433     setDataIndex : function(col, dataIndex){
434         this.config[col].dataIndex = dataIndex;
435     },
436
437     
438     
439     /**
440      * Returns true if the cell is editable.
441      * @param {Number} colIndex The column index
442      * @param {Number} rowIndex The row index
443      * @return {Boolean}
444      */
445     isCellEditable : function(colIndex, rowIndex){
446         return (this.config[colIndex].editable || (typeof this.config[colIndex].editable == "undefined" && this.config[colIndex].editor)) ? true : false;
447     },
448
449     /**
450      * Returns the editor defined for the cell/column.
451      * return false or null to disable editing.
452      * @param {Number} colIndex The column index
453      * @param {Number} rowIndex The row index
454      * @return {Object}
455      */
456     getCellEditor : function(colIndex, rowIndex){
457         return this.config[colIndex].editor;
458     },
459
460     /**
461      * Sets if a column is editable.
462      * @param {Number} col The column index
463      * @param {Boolean} editable True if the column is editable
464      */
465     setEditable : function(col, editable){
466         this.config[col].editable = editable;
467     },
468
469
470     /**
471      * Returns true if the column is hidden.
472      * @param {Number} colIndex The column index
473      * @return {Boolean}
474      */
475     isHidden : function(colIndex){
476         return this.config[colIndex].hidden;
477     },
478
479
480     /**
481      * Returns true if the column width cannot be changed
482      */
483     isFixed : function(colIndex){
484         return this.config[colIndex].fixed;
485     },
486
487     /**
488      * Returns true if the column can be resized
489      * @return {Boolean}
490      */
491     isResizable : function(colIndex){
492         return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
493     },
494     /**
495      * Sets if a column is hidden.
496      * @param {Number} colIndex The column index
497      * @param {Boolean} hidden True if the column is hidden
498      */
499     setHidden : function(colIndex, hidden){
500         this.config[colIndex].hidden = hidden;
501         this.totalWidth = null;
502         this.fireEvent("hiddenchange", this, colIndex, hidden);
503     },
504
505     /**
506      * Sets the editor for a column.
507      * @param {Number} col The column index
508      * @param {Object} editor The editor object
509      */
510     setEditor : function(col, editor){
511         this.config[col].editor = editor;
512     }
513 });
514
515 Roo.grid.ColumnModel.defaultRenderer = function(value){
516         if(typeof value == "string" && value.length < 1){
517             return "&#160;";
518         }
519         return value;
520 };
521
522 // Alias for backwards compatibility
523 Roo.grid.DefaultColumnModel = Roo.grid.ColumnModel;