d1fa60d579b6594d59ab16ccf1649ac9946472cd
[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 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 (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} valign (Optional) Set the CSS vertical-align property of the column (eg. middle, top, bottom etc).  Defaults to undefined.
167      */
168     /**
169      * @cfg {String} cursor (Optional)
170      */
171     /**
172      * @cfg {String} tooltip (Optional)
173      */
174     /**
175      * @cfg {Number} xs (Optional)
176      */
177     /**
178      * @cfg {Number} sm (Optional)
179      */
180     /**
181      * @cfg {Number} md (Optional)
182      */
183     /**
184      * @cfg {Number} lg (Optional)
185      */
186     /**
187      * Returns the id of the column at the specified index.
188      * @param {Number} index The column index
189      * @return {String} the id
190      */
191     getColumnId : function(index){
192         return this.config[index].id;
193     },
194
195     /**
196      * Returns the column for a specified id.
197      * @param {String} id The column id
198      * @return {Object} the column
199      */
200     getColumnById : function(id){
201         return this.lookup[id];
202     },
203
204     
205     /**
206      * Returns the column for a specified dataIndex.
207      * @param {String} dataIndex The column dataIndex
208      * @return {Object|Boolean} the column or false if not found
209      */
210     getColumnByDataIndex: function(dataIndex){
211         var index = this.findColumnIndex(dataIndex);
212         return index > -1 ? this.config[index] : false;
213     },
214     
215     /**
216      * Returns the index for a specified column id.
217      * @param {String} id The column id
218      * @return {Number} the index, or -1 if not found
219      */
220     getIndexById : function(id){
221         for(var i = 0, len = this.config.length; i < len; i++){
222             if(this.config[i].id == id){
223                 return i;
224             }
225         }
226         return -1;
227     },
228     
229     /**
230      * Returns the index for a specified column dataIndex.
231      * @param {String} dataIndex The column dataIndex
232      * @return {Number} the index, or -1 if not found
233      */
234     
235     findColumnIndex : function(dataIndex){
236         for(var i = 0, len = this.config.length; i < len; i++){
237             if(this.config[i].dataIndex == dataIndex){
238                 return i;
239             }
240         }
241         return -1;
242     },
243     
244     
245     moveColumn : function(oldIndex, newIndex){
246         var c = this.config[oldIndex];
247         this.config.splice(oldIndex, 1);
248         this.config.splice(newIndex, 0, c);
249         this.dataMap = null;
250         this.fireEvent("columnmoved", this, oldIndex, newIndex);
251     },
252
253     isLocked : function(colIndex){
254         return this.config[colIndex].locked === true;
255     },
256
257     setLocked : function(colIndex, value, suppressEvent){
258         if(this.isLocked(colIndex) == value){
259             return;
260         }
261         this.config[colIndex].locked = value;
262         if(!suppressEvent){
263             this.fireEvent("columnlockchange", this, colIndex, value);
264         }
265     },
266
267     getTotalLockedWidth : function(){
268         var totalWidth = 0;
269         for(var i = 0; i < this.config.length; i++){
270             if(this.isLocked(i) && !this.isHidden(i)){
271                 this.totalWidth += this.getColumnWidth(i);
272             }
273         }
274         return totalWidth;
275     },
276
277     getLockedCount : function(){
278         for(var i = 0, len = this.config.length; i < len; i++){
279             if(!this.isLocked(i)){
280                 return i;
281             }
282         }
283         
284         return this.config.length;
285     },
286
287     /**
288      * Returns the number of columns.
289      * @return {Number}
290      */
291     getColumnCount : function(visibleOnly){
292         if(visibleOnly === true){
293             var c = 0;
294             for(var i = 0, len = this.config.length; i < len; i++){
295                 if(!this.isHidden(i)){
296                     c++;
297                 }
298             }
299             return c;
300         }
301         return this.config.length;
302     },
303
304     /**
305      * Returns the column configs that return true by the passed function that is called with (columnConfig, index)
306      * @param {Function} fn
307      * @param {Object} scope (optional)
308      * @return {Array} result
309      */
310     getColumnsBy : function(fn, scope){
311         var r = [];
312         for(var i = 0, len = this.config.length; i < len; i++){
313             var c = this.config[i];
314             if(fn.call(scope||this, c, i) === true){
315                 r[r.length] = c;
316             }
317         }
318         return r;
319     },
320
321     /**
322      * Returns true if the specified column is sortable.
323      * @param {Number} col The column index
324      * @return {Boolean}
325      */
326     isSortable : function(col){
327         if(typeof this.config[col].sortable == "undefined"){
328             return this.defaultSortable;
329         }
330         return this.config[col].sortable;
331     },
332
333     /**
334      * Returns the rendering (formatting) function defined for the column.
335      * @param {Number} col The column index.
336      * @return {Function} The function used to render the cell. See {@link #setRenderer}.
337      */
338     getRenderer : function(col){
339         if(!this.config[col].renderer){
340             return Roo.grid.ColumnModel.defaultRenderer;
341         }
342         return this.config[col].renderer;
343     },
344
345     /**
346      * Sets the rendering (formatting) function for a column.
347      * @param {Number} col The column index
348      * @param {Function} fn The function to use to process the cell's raw data
349      * to return HTML markup for the grid view. The render function is called with
350      * the following parameters:<ul>
351      * <li>Data value.</li>
352      * <li>Cell metadata. An object in which you may set the following attributes:<ul>
353      * <li>css A CSS style string to apply to the table cell.</li>
354      * <li>attr An HTML attribute definition string to apply to the data container element <i>within</i> the table cell.</li></ul>
355      * <li>The {@link Roo.data.Record} from which the data was extracted.</li>
356      * <li>Row index</li>
357      * <li>Column index</li>
358      * <li>The {@link Roo.data.Store} object from which the Record was extracted</li></ul>
359      */
360     setRenderer : function(col, fn){
361         this.config[col].renderer = fn;
362     },
363
364     /**
365      * Returns the width for the specified column.
366      * @param {Number} col The column index
367      * @return {Number}
368      */
369     getColumnWidth : function(col){
370         return this.config[col].width * 1 || this.defaultWidth;
371     },
372
373     /**
374      * Sets the width for a column.
375      * @param {Number} col The column index
376      * @param {Number} width The new width
377      */
378     setColumnWidth : function(col, width, suppressEvent){
379         this.config[col].width = width;
380         this.totalWidth = null;
381         if(!suppressEvent){
382              this.fireEvent("widthchange", this, col, width);
383         }
384     },
385
386     /**
387      * Returns the total width of all columns.
388      * @param {Boolean} includeHidden True to include hidden column widths
389      * @return {Number}
390      */
391     getTotalWidth : function(includeHidden){
392         if(!this.totalWidth){
393             this.totalWidth = 0;
394             for(var i = 0, len = this.config.length; i < len; i++){
395                 if(includeHidden || !this.isHidden(i)){
396                     this.totalWidth += this.getColumnWidth(i);
397                 }
398             }
399         }
400         return this.totalWidth;
401     },
402
403     /**
404      * Returns the header for the specified column.
405      * @param {Number} col The column index
406      * @return {String}
407      */
408     getColumnHeader : function(col){
409         return this.config[col].header;
410     },
411
412     /**
413      * Sets the header for a column.
414      * @param {Number} col The column index
415      * @param {String} header The new header
416      */
417     setColumnHeader : function(col, header){
418         this.config[col].header = header;
419         this.fireEvent("headerchange", this, col, header);
420     },
421
422     /**
423      * Returns the tooltip for the specified column.
424      * @param {Number} col The column index
425      * @return {String}
426      */
427     getColumnTooltip : function(col){
428             return this.config[col].tooltip;
429     },
430     /**
431      * Sets the tooltip for a column.
432      * @param {Number} col The column index
433      * @param {String} tooltip The new tooltip
434      */
435     setColumnTooltip : function(col, tooltip){
436             this.config[col].tooltip = tooltip;
437     },
438
439     /**
440      * Returns the dataIndex for the specified column.
441      * @param {Number} col The column index
442      * @return {Number}
443      */
444     getDataIndex : function(col){
445         return this.config[col].dataIndex;
446     },
447
448     /**
449      * Sets the dataIndex for a column.
450      * @param {Number} col The column index
451      * @param {Number} dataIndex The new dataIndex
452      */
453     setDataIndex : function(col, dataIndex){
454         this.config[col].dataIndex = dataIndex;
455     },
456
457     
458     
459     /**
460      * Returns true if the cell is editable.
461      * @param {Number} colIndex The column index
462      * @param {Number} rowIndex The row index - this is nto actually used..?
463      * @return {Boolean}
464      */
465     isCellEditable : function(colIndex, rowIndex){
466         return (this.config[colIndex].editable || (typeof this.config[colIndex].editable == "undefined" && this.config[colIndex].editor)) ? true : false;
467     },
468
469     /**
470      * Returns the editor defined for the cell/column.
471      * return false or null to disable editing.
472      * @param {Number} colIndex The column index
473      * @param {Number} rowIndex The row index
474      * @return {Object}
475      */
476     getCellEditor : function(colIndex, rowIndex){
477         return this.config[colIndex].editor;
478     },
479
480     /**
481      * Sets if a column is editable.
482      * @param {Number} col The column index
483      * @param {Boolean} editable True if the column is editable
484      */
485     setEditable : function(col, editable){
486         this.config[col].editable = editable;
487     },
488
489
490     /**
491      * Returns true if the column is hidden.
492      * @param {Number} colIndex The column index
493      * @return {Boolean}
494      */
495     isHidden : function(colIndex){
496         return this.config[colIndex].hidden;
497     },
498
499
500     /**
501      * Returns true if the column width cannot be changed
502      */
503     isFixed : function(colIndex){
504         return this.config[colIndex].fixed;
505     },
506
507     /**
508      * Returns true if the column can be resized
509      * @return {Boolean}
510      */
511     isResizable : function(colIndex){
512         return colIndex >= 0 && this.config[colIndex].resizable !== false && this.config[colIndex].fixed !== true;
513     },
514     /**
515      * Sets if a column is hidden.
516      * @param {Number} colIndex The column index
517      * @param {Boolean} hidden True if the column is hidden
518      */
519     setHidden : function(colIndex, hidden){
520         this.config[colIndex].hidden = hidden;
521         this.totalWidth = null;
522         this.fireEvent("hiddenchange", this, colIndex, hidden);
523     },
524
525     /**
526      * Sets the editor for a column.
527      * @param {Number} col The column index
528      * @param {Object} editor The editor object
529      */
530     setEditor : function(col, editor){
531         this.config[col].editor = editor;
532     }
533 });
534
535 Roo.grid.ColumnModel.defaultRenderer = function(value)
536 {
537     if(typeof value == "object") {
538         return value;
539     }
540         if(typeof value == "string" && value.length < 1){
541             return "&#160;";
542         }
543     
544         return String.format("{0}", value);
545 };
546
547 // Alias for backwards compatibility
548 Roo.grid.DefaultColumnModel = Roo.grid.ColumnModel;