more docs on collumn model
[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         this.addColumn(config[i]);
49         
50     }
51
52     /**
53      * The width of columns which have no width specified (defaults to 100)
54      * @type Number
55      */
56     this.defaultWidth = 100;
57
58     /**
59      * Default sortable of columns which have no sortable specified (defaults to false)
60      * @type Boolean
61      */
62     this.defaultSortable = false;
63
64     this.addEvents({
65         /**
66              * @event widthchange
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
71              */
72             "widthchange": true,
73         /**
74              * @event headerchange
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
79              */
80             "headerchange": true,
81         /**
82              * @event hiddenchange
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
87              */
88             "hiddenchange": true,
89             /**
90          * @event columnmoved
91          * Fires when a column is moved.
92          * @param {ColumnModel} this
93          * @param {Number} oldIndex
94          * @param {Number} newIndex
95          */
96         "columnmoved" : true,
97         /**
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
103          */
104         "columnlockchange" : true
105     });
106     Roo.grid.ColumnModel.superclass.constructor.call(this);
107 };
108 Roo.extend(Roo.grid.ColumnModel, Roo.util.Observable, {
109     /**
110      * @cfg {String} header [required] The header text to display in the Grid view.
111      */
112         /**
113      * @cfg {String} xsHeader Header at Bootsrap Extra Small width (default for all)
114      */
115         /**
116      * @cfg {String} smHeader Header at Bootsrap Small width
117      */
118         /**
119      * @cfg {String} mdHeader Header at Bootsrap Medium width
120      */
121         /**
122      * @cfg {String} lgHeader Header at Bootsrap Large width
123      */
124         /**
125      * @cfg {String} xlHeader Header at Bootsrap extra Large width
126      */
127     /**
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.
131      */
132     /**
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.
135      */
136     /**
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}.
140      */
141     /**
142      * @cfg {Boolean} locked  True to lock the column in place while scrolling the Grid.  Defaults to false.
143      */
144     /**
145      * @cfg {Boolean} fixed  True if the column width cannot be changed.  Defaults to false.
146      */
147     /**
148      * @cfg {Boolean} resizable  False to disable column resizing. Defaults to true.
149      */
150     /**
151      * @cfg {Boolean} hidden  True to hide the column. Defaults to false.
152      */
153     /**
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
158      */
159        /**
160      * @cfg {Roo.grid.GridEditor} editor  For grid editors - returns the grid editor 
161      */
162     /**
163      * @cfg {String} align (left|right) Set the CSS text-align property of the column.  Defaults to undefined (left).
164      */
165     /**
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)
167      */
168     /**
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)
170      */
171     /**
172      * @cfg {String} tooltip mouse over tooltip text
173      */
174     /**
175      * @cfg {Number} xs  can be '0' for hidden at this size (number less than 12)
176      */
177     /**
178      * @cfg {Number} sm can be '0' for hidden at this size (number less than 12)
179      */
180     /**
181      * @cfg {Number} md can be '0' for hidden at this size (number less than 12)
182      */
183     /**
184      * @cfg {Number} lg   can be '0' for hidden at this size (number less than 12)
185      */
186         /**
187      * @cfg {Number} xl   can be '0' for hidden at this size (number less than 12)
188      */
189     /**
190      * Returns the id of the column at the specified index.
191      * @param {Number} index The column index
192      * @return {String} the id
193      */
194     getColumnId : function(index){
195         return this.config[index].id;
196     },
197
198     /**
199      * Returns the column for a specified id.
200      * @param {String} id The column id
201      * @return {Object} the column
202      */
203     getColumnById : function(id){
204         return this.lookup[id];
205     },
206
207     
208     /**
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
212      */
213     getColumnByDataIndex: function(dataIndex){
214         var index = this.findColumnIndex(dataIndex);
215         return index > -1 ? this.config[index] : false;
216     },
217     
218     /**
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
222      */
223     getIndexById : function(id){
224         for(var i = 0, len = this.config.length; i < len; i++){
225             if(this.config[i].id == id){
226                 return i;
227             }
228         }
229         return -1;
230     },
231     
232     /**
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
236      */
237     
238     findColumnIndex : function(dataIndex){
239         for(var i = 0, len = this.config.length; i < len; i++){
240             if(this.config[i].dataIndex == dataIndex){
241                 return i;
242             }
243         }
244         return -1;
245     },
246     
247     
248     moveColumn : function(oldIndex, newIndex){
249         var c = this.config[oldIndex];
250         this.config.splice(oldIndex, 1);
251         this.config.splice(newIndex, 0, c);
252         this.dataMap = null;
253         this.fireEvent("columnmoved", this, oldIndex, newIndex);
254     },
255
256     isLocked : function(colIndex){
257         return this.config[colIndex].locked === true;
258     },
259
260     setLocked : function(colIndex, value, suppressEvent){
261         if(this.isLocked(colIndex) == value){
262             return;
263         }
264         this.config[colIndex].locked = value;
265         if(!suppressEvent){
266             this.fireEvent("columnlockchange", this, colIndex, value);
267         }
268     },
269
270     getTotalLockedWidth : function(){
271         var totalWidth = 0;
272         for(var i = 0; i < this.config.length; i++){
273             if(this.isLocked(i) && !this.isHidden(i)){
274                 this.totalWidth += this.getColumnWidth(i);
275             }
276         }
277         return totalWidth;
278     },
279
280     getLockedCount : function(){
281         for(var i = 0, len = this.config.length; i < len; i++){
282             if(!this.isLocked(i)){
283                 return i;
284             }
285         }
286         
287         return this.config.length;
288     },
289
290     /**
291      * Returns the number of columns.
292      * @return {Number}
293      */
294     getColumnCount : function(visibleOnly){
295         if(visibleOnly === true){
296             var c = 0;
297             for(var i = 0, len = this.config.length; i < len; i++){
298                 if(!this.isHidden(i)){
299                     c++;
300                 }
301             }
302             return c;
303         }
304         return this.config.length;
305     },
306
307     /**
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
312      */
313     getColumnsBy : function(fn, scope){
314         var r = [];
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){
318                 r[r.length] = c;
319             }
320         }
321         return r;
322     },
323
324     /**
325      * Returns true if the specified column is sortable.
326      * @param {Number} col The column index
327      * @return {Boolean}
328      */
329     isSortable : function(col){
330         if(typeof this.config[col].sortable == "undefined"){
331             return this.defaultSortable;
332         }
333         return this.config[col].sortable;
334     },
335
336     /**
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}.
340      */
341     getRenderer : function(col){
342         if(!this.config[col].renderer){
343             return Roo.grid.ColumnModel.defaultRenderer;
344         }
345         return this.config[col].renderer;
346     },
347
348     /**
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>
359      * <li>Row index</li>
360      * <li>Column index</li>
361      * <li>The {@link Roo.data.Store} object from which the Record was extracted</li></ul>
362      */
363     setRenderer : function(col, fn){
364         this.config[col].renderer = fn;
365     },
366
367     /**
368      * Returns the width for the specified column.
369      * @param {Number} col The column index
370      * @param (optional) {String} gridSize bootstrap width size.
371      * @return {Number}
372      */
373     getColumnWidth : function(col, gridSize)
374         {
375                 var cfg = this.config[col];
376                 
377                 if (typeof(gridSize) == 'undefined') {
378                         return cfg.width * 1 || this.defaultWidth;
379                 }
380                 if (gridSize === false) { // if we set it..
381                         return cfg.width || false;
382                 }
383                 var sizes = ['xl', 'lg', 'md', 'sm', 'xs'];
384                 
385                 for(var i = sizes.indexOf(gridSize); i < sizes.length; i++) {
386                         if (typeof(cfg[ sizes[i] ] ) == 'undefined') {
387                                 continue;
388                         }
389                         return cfg[ sizes[i] ];
390                 }
391                 return 1;
392                 
393     },
394
395     /**
396      * Sets the width for a column.
397      * @param {Number} col The column index
398      * @param {Number} width The new width
399      */
400     setColumnWidth : function(col, width, suppressEvent){
401         this.config[col].width = width;
402         this.totalWidth = null;
403         if(!suppressEvent){
404              this.fireEvent("widthchange", this, col, width);
405         }
406     },
407
408     /**
409      * Returns the total width of all columns.
410      * @param {Boolean} includeHidden True to include hidden column widths
411      * @return {Number}
412      */
413     getTotalWidth : function(includeHidden){
414         if(!this.totalWidth){
415             this.totalWidth = 0;
416             for(var i = 0, len = this.config.length; i < len; i++){
417                 if(includeHidden || !this.isHidden(i)){
418                     this.totalWidth += this.getColumnWidth(i);
419                 }
420             }
421         }
422         return this.totalWidth;
423     },
424
425     /**
426      * Returns the header for the specified column.
427      * @param {Number} col The column index
428      * @return {String}
429      */
430     getColumnHeader : function(col){
431         return this.config[col].header;
432     },
433
434     /**
435      * Sets the header for a column.
436      * @param {Number} col The column index
437      * @param {String} header The new header
438      */
439     setColumnHeader : function(col, header){
440         this.config[col].header = header;
441         this.fireEvent("headerchange", this, col, header);
442     },
443
444     /**
445      * Returns the tooltip for the specified column.
446      * @param {Number} col The column index
447      * @return {String}
448      */
449     getColumnTooltip : function(col){
450             return this.config[col].tooltip;
451     },
452     /**
453      * Sets the tooltip for a column.
454      * @param {Number} col The column index
455      * @param {String} tooltip The new tooltip
456      */
457     setColumnTooltip : function(col, tooltip){
458             this.config[col].tooltip = tooltip;
459     },
460
461     /**
462      * Returns the dataIndex for the specified column.
463      * @param {Number} col The column index
464      * @return {Number}
465      */
466     getDataIndex : function(col){
467         return this.config[col].dataIndex;
468     },
469
470     /**
471      * Sets the dataIndex for a column.
472      * @param {Number} col The column index
473      * @param {Number} dataIndex The new dataIndex
474      */
475     setDataIndex : function(col, dataIndex){
476         this.config[col].dataIndex = dataIndex;
477     },
478
479     
480     
481     /**
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..?
485      * @return {Boolean}
486      */
487     isCellEditable : function(colIndex, rowIndex){
488         return (this.config[colIndex].editable || (typeof this.config[colIndex].editable == "undefined" && this.config[colIndex].editor)) ? true : false;
489     },
490
491     /**
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
496      * @return {Object}
497      */
498     getCellEditor : function(colIndex, rowIndex){
499         return this.config[colIndex].editor;
500     },
501
502     /**
503      * Sets if a column is editable.
504      * @param {Number} col The column index
505      * @param {Boolean} editable True if the column is editable
506      */
507     setEditable : function(col, editable){
508         this.config[col].editable = editable;
509     },
510
511
512     /**
513      * Returns true if the column is hidden.
514      * @param {Number} colIndex The column index
515      * @return {Boolean}
516      */
517     isHidden : function(colIndex){
518         return this.config[colIndex].hidden;
519     },
520
521
522     /**
523      * Returns true if the column width cannot be changed
524      */
525     isFixed : function(colIndex){
526         return this.config[colIndex].fixed;
527     },
528
529     /**
530      * Returns true if the column can be resized
531      * @return {Boolean}
532      */
533     isResizable : function(colIndex){
534         return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
535     },
536     /**
537      * Sets if a column is hidden.
538      * @param {Number} colIndex The column index
539      * @param {Boolean} hidden True if the column is hidden
540      */
541     setHidden : function(colIndex, hidden){
542         this.config[colIndex].hidden = hidden;
543         this.totalWidth = null;
544         this.fireEvent("hiddenchange", this, colIndex, hidden);
545     },
546
547     /**
548      * Sets the editor for a column.
549      * @param {Number} col The column index
550      * @param {Object} editor The editor object
551      */
552     setEditor : function(col, editor){
553         this.config[col].editor = editor;
554     },
555     /**
556      * Add a column (experimental...) - defaults to adding to the end..
557      * @param {Object} config 
558     */
559     addColumn : function(c)
560     {
561     
562         var i = this.config.length;
563         this.config[i] = c;
564         
565         if(typeof c.dataIndex == "undefined"){
566             c.dataIndex = i;
567         }
568         if(typeof c.renderer == "string"){
569             c.renderer = Roo.util.Format[c.renderer];
570         }
571         if(typeof c.id == "undefined"){
572             c.id = Roo.id();
573         }
574         if(c.editor && c.editor.xtype){
575             c.editor  = Roo.factory(c.editor, Roo.grid);
576         }
577         if(c.editor && c.editor.isFormField){
578             c.editor = new Roo.grid.GridEditor(c.editor);
579         }
580         this.lookup[c.id] = c;
581     }
582     
583 });
584
585 Roo.grid.ColumnModel.defaultRenderer = function(value)
586 {
587     if(typeof value == "object") {
588         return value;
589     }
590         if(typeof value == "string" && value.length < 1){
591             return "&#160;";
592         }
593     
594         return String.format("{0}", value);
595 };
596
597 // Alias for backwards compatibility
598 Roo.grid.DefaultColumnModel = Roo.grid.ColumnModel;