Roo/bootstrap/Table.js
[roojs1] / Roo / bootstrap / Table.js
1 /*
2  * - LGPL
3  *
4  * table
5  * 
6  */
7
8 /**
9  * @class Roo.bootstrap.Table
10  * @extends Roo.bootstrap.Component
11  * Bootstrap Table class
12  * @cfg {String} cls table class
13  * @cfg {String} align (left|center|right) Specifies the alignment of a table according to surrounding text
14  * @cfg {String} bgcolor Specifies the background color for a table
15  * @cfg {Number} border Specifies whether the table cells should have borders or not
16  * @cfg {Number} cellpadding Specifies the space between the cell wall and the cell content
17  * @cfg {Number} cellspacing Specifies the space between cells
18  * @cfg {String} frame Specifies which parts of the outside borders that should be visible
19  * @cfg {String} rules Specifies which parts of the inside borders that should be visible
20  * @cfg {String} sortable Specifies that the table should be sortable
21  * @cfg {String} summary Specifies a summary of the content of a table
22  * @cfg {Number} width Specifies the width of a table
23  * @cfg {String} layout table layout (auto | fixed | initial | inherit)
24  * 
25  * @cfg {boolean} striped Should the rows be alternative striped
26  * @cfg {boolean} bordered Add borders to the table
27  * @cfg {boolean} hover Add hover highlighting
28  * @cfg {boolean} condensed Format condensed
29  * @cfg {boolean} responsive Format condensed
30  * @cfg {Boolean} loadMask (true|false) default false
31  * @cfg {Boolean} footerShow (true|false) generate tfoot, default true
32  * @cfg {Boolean} headerShow (true|false) generate thead, default true
33  * @cfg {Boolean} rowSelection (true|false) default false
34  * @cfg {Boolean} cellSelection (true|false) default false
35  * @cfg {Boolean} scrollBody (true|false) default false - body scrolled / fixed header
36  * @cfg {Roo.bootstrap.PagingToolbar} footer  a paging toolbar
37  * @cfg {Boolean} lazyLoad  auto load data while scrolling to the end (default false)
38  * @cfg {Boolean} autoHideFooter  auto hide footer if only one page (default false)
39  
40  * 
41  * @constructor
42  * Create a new Table
43  * @param {Object} config The config object
44  */
45
46 Roo.bootstrap.Table = function(config){
47     Roo.bootstrap.Table.superclass.constructor.call(this, config);
48     
49   
50     
51     // BC...
52     this.rowSelection = (typeof(config.rowSelection) != 'undefined') ? config.rowSelection : this.rowSelection;
53     this.cellSelection = (typeof(config.cellSelection) != 'undefined') ? config.cellSelection : this.cellSelection;
54     this.headerShow = (typeof(config.thead) != 'undefined') ? config.thead : this.headerShow;
55     this.footerShow = (typeof(config.tfoot) != 'undefined') ? config.tfoot : this.footerShow;
56     
57     this.sm = this.sm || {xtype: 'RowSelectionModel'};
58     if (this.sm) {
59         this.sm.grid = this;
60         this.selModel = Roo.factory(this.sm, Roo.bootstrap.Table);
61         this.sm = this.selModel;
62         this.sm.xmodule = this.xmodule || false;
63     }
64     
65     if (this.cm && typeof(this.cm.config) == 'undefined') {
66         this.colModel = new Roo.grid.ColumnModel(this.cm);
67         this.cm = this.colModel;
68         this.cm.xmodule = this.xmodule || false;
69     }
70     if (this.store) {
71         this.store= Roo.factory(this.store, Roo.data);
72         this.ds = this.store;
73         this.ds.xmodule = this.xmodule || false;
74          
75     }
76     if (this.footer && this.store) {
77         this.footer.dataSource = this.ds;
78         this.footer = Roo.factory(this.footer);
79     }
80     
81     /** @private */
82     this.addEvents({
83         /**
84          * @event cellclick
85          * Fires when a cell is clicked
86          * @param {Roo.bootstrap.Table} this
87          * @param {Roo.Element} el
88          * @param {Number} rowIndex
89          * @param {Number} columnIndex
90          * @param {Roo.EventObject} e
91          */
92         "cellclick" : true,
93         /**
94          * @event celldblclick
95          * Fires when a cell is double clicked
96          * @param {Roo.bootstrap.Table} this
97          * @param {Roo.Element} el
98          * @param {Number} rowIndex
99          * @param {Number} columnIndex
100          * @param {Roo.EventObject} e
101          */
102         "celldblclick" : true,
103         /**
104          * @event rowclick
105          * Fires when a row is clicked
106          * @param {Roo.bootstrap.Table} this
107          * @param {Roo.Element} el
108          * @param {Number} rowIndex
109          * @param {Roo.EventObject} e
110          */
111         "rowclick" : true,
112         /**
113          * @event rowdblclick
114          * Fires when a row is double clicked
115          * @param {Roo.bootstrap.Table} this
116          * @param {Roo.Element} el
117          * @param {Number} rowIndex
118          * @param {Roo.EventObject} e
119          */
120         "rowdblclick" : true,
121         /**
122          * @event mouseover
123          * Fires when a mouseover occur
124          * @param {Roo.bootstrap.Table} this
125          * @param {Roo.Element} el
126          * @param {Number} rowIndex
127          * @param {Number} columnIndex
128          * @param {Roo.EventObject} e
129          */
130         "mouseover" : true,
131         /**
132          * @event mouseout
133          * Fires when a mouseout occur
134          * @param {Roo.bootstrap.Table} this
135          * @param {Roo.Element} el
136          * @param {Number} rowIndex
137          * @param {Number} columnIndex
138          * @param {Roo.EventObject} e
139          */
140         "mouseout" : true,
141         /**
142          * @event rowclass
143          * Fires when a row is rendered, so you can change add a style to it.
144          * @param {Roo.bootstrap.Table} this
145          * @param {Object} rowcfg   contains record  rowIndex colIndex and rowClass - set rowClass to add a style.
146          */
147         'rowclass' : true,
148           /**
149          * @event rowsrendered
150          * Fires when all the  rows have been rendered
151          * @param {Roo.bootstrap.Table} this
152          */
153         'rowsrendered' : true,
154         /**
155          * @event contextmenu
156          * The raw contextmenu event for the entire grid.
157          * @param {Roo.EventObject} e
158          */
159         "contextmenu" : true,
160         /**
161          * @event rowcontextmenu
162          * Fires when a row is right clicked
163          * @param {Roo.bootstrap.Table} this
164          * @param {Number} rowIndex
165          * @param {Roo.EventObject} e
166          */
167         "rowcontextmenu" : true,
168         /**
169          * @event cellcontextmenu
170          * Fires when a cell is right clicked
171          * @param {Roo.bootstrap.Table} this
172          * @param {Number} rowIndex
173          * @param {Number} cellIndex
174          * @param {Roo.EventObject} e
175          */
176          "cellcontextmenu" : true,
177          /**
178          * @event headercontextmenu
179          * Fires when a header is right clicked
180          * @param {Roo.bootstrap.Table} this
181          * @param {Number} columnIndex
182          * @param {Roo.EventObject} e
183          */
184         "headercontextmenu" : true
185     });
186 };
187
188 Roo.extend(Roo.bootstrap.Table, Roo.bootstrap.Component,  {
189     
190     cls: false,
191     align: false,
192     bgcolor: false,
193     border: false,
194     cellpadding: false,
195     cellspacing: false,
196     frame: false,
197     rules: false,
198     sortable: false,
199     summary: false,
200     width: false,
201     striped : false,
202     scrollBody : false,
203     bordered: false,
204     hover:  false,
205     condensed : false,
206     responsive : false,
207     sm : false,
208     cm : false,
209     store : false,
210     loadMask : false,
211     footerShow : true,
212     headerShow : true,
213   
214     rowSelection : false,
215     cellSelection : false,
216     layout : false,
217     
218     // Roo.Element - the tbody
219     mainBody: false,
220     // Roo.Element - thead element
221     mainHead: false,
222     
223     container: false, // used by gridpanel...
224     
225     lazyLoad : false,
226     
227     CSS : Roo.util.CSS,
228     
229     getAutoCreate : function()
230     {
231         var cfg = Roo.apply({}, Roo.bootstrap.Table.superclass.getAutoCreate.call(this));
232         
233         cfg = {
234             tag: 'table',
235             cls : 'table',
236             cn : []
237         };
238         if (this.scrollBody) {
239             cfg.cls += ' table-body-fixed';
240         }    
241         if (this.striped) {
242             cfg.cls += ' table-striped';
243         }
244         
245         if (this.hover) {
246             cfg.cls += ' table-hover';
247         }
248         if (this.bordered) {
249             cfg.cls += ' table-bordered';
250         }
251         if (this.condensed) {
252             cfg.cls += ' table-condensed';
253         }
254         if (this.responsive) {
255             cfg.cls += ' table-responsive';
256         }
257         
258         if (this.cls) {
259             cfg.cls+=  ' ' +this.cls;
260         }
261         
262         // this lot should be simplifed...
263         var _t = this;
264         var cp = [
265             'align',
266             'bgcolor',
267             'border',
268             'cellpadding',
269             'cellspacing',
270             'frame',
271             'rules',
272             'sortable',
273             'summary',
274             'width'
275         ].forEach(function(k) {
276             if (_t[k]) {
277                 cfg[k] = _t[k];
278             }
279         });
280         
281         
282         if (this.layout) {
283             cfg.style = (typeof(cfg.style) == 'undefined') ? ('table-layout:' + this.layout + ';') : (cfg.style + ('table-layout:' + this.layout + ';'));
284         }
285         
286         if(this.store || this.cm){
287             if(this.headerShow){
288                 cfg.cn.push(this.renderHeader());
289             }
290             
291             cfg.cn.push(this.renderBody());
292             
293             if(this.footerShow){
294                 cfg.cn.push(this.renderFooter());
295             }
296             // where does this come from?
297             //cfg.cls+=  ' TableGrid';
298         }
299         
300         return { cn : [ cfg ] };
301     },
302     
303     initEvents : function()
304     {   
305         if(!this.store || !this.cm){
306             return;
307         }
308         if (this.selModel) {
309             this.selModel.initEvents();
310         }
311         
312         
313         //Roo.log('initEvents with ds!!!!');
314         
315         this.mainBody = this.el.select('tbody', true).first();
316         this.mainHead = this.el.select('thead', true).first();
317         
318         
319         
320         
321         var _this = this;
322         
323         Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
324             e.on('click', _this.sort, _this);
325         });
326         
327         this.mainBody.on("click", this.onClick, this);
328         this.mainBody.on("dblclick", this.onDblClick, this);
329         
330         // why is this done????? = it breaks dialogs??
331         //this.parent().el.setStyle('position', 'relative');
332         
333         
334         if (this.footer) {
335             this.footer.parentId = this.id;
336             this.footer.onRender(this.el.select('tfoot tr td').first(), null);
337             
338             if(this.lazyLoad){
339                 this.el.select('tfoot tr td').first().addClass('hide');
340             }
341         } 
342         
343         if(this.loadMask) {
344             this.maskEl = new Roo.LoadMask(this.el, { store : this.ds, msgCls: 'roo-el-mask-msg' });
345         }
346         
347         this.store.on('load', this.onLoad, this);
348         this.store.on('beforeload', this.onBeforeLoad, this);
349         this.store.on('update', this.onUpdate, this);
350         this.store.on('add', this.onAdd, this);
351         this.store.on("clear", this.clear, this);
352         
353         this.el.on("contextmenu", this.onContextMenu, this);
354         
355         this.mainBody.on('scroll', this.onBodyScroll, this);
356         
357         this.cm.on("headerchange", this.onHeaderChange, this);
358         
359         this.cm.on("hiddenchange", this.onHiddenChange, this, arguments);
360         
361     },
362     
363     onContextMenu : function(e, t)
364     {
365         this.processEvent("contextmenu", e);
366     },
367     
368     processEvent : function(name, e)
369     {
370         if (name != 'touchstart' ) {
371             this.fireEvent(name, e);    
372         }
373         
374         var t = e.getTarget();
375         
376         var cell = Roo.get(t);
377         
378         if(!cell){
379             return;
380         }
381         
382         if(cell.findParent('tfoot', false, true)){
383             return;
384         }
385         
386         if(cell.findParent('thead', false, true)){
387             
388             if(e.getTarget().nodeName.toLowerCase() != 'th'){
389                 cell = Roo.get(t).findParent('th', false, true);
390                 if (!cell) {
391                     Roo.log("failed to find th in thead?");
392                     Roo.log(e.getTarget());
393                     return;
394                 }
395             }
396             
397             var cellIndex = cell.dom.cellIndex;
398             
399             var ename = name == 'touchstart' ? 'click' : name;
400             this.fireEvent("header" + ename, this, cellIndex, e);
401             
402             return;
403         }
404         
405         if(e.getTarget().nodeName.toLowerCase() != 'td'){
406             cell = Roo.get(t).findParent('td', false, true);
407             if (!cell) {
408                 Roo.log("failed to find th in tbody?");
409                 Roo.log(e.getTarget());
410                 return;
411             }
412         }
413         
414         var row = cell.findParent('tr', false, true);
415         var cellIndex = cell.dom.cellIndex;
416         var rowIndex = row.dom.rowIndex - 1;
417         
418         if(row !== false){
419             
420             this.fireEvent("row" + name, this, rowIndex, e);
421             
422             if(cell !== false){
423             
424                 this.fireEvent("cell" + name, this, rowIndex, cellIndex, e);
425             }
426         }
427         
428     },
429     
430     onMouseover : function(e, el)
431     {
432         var cell = Roo.get(el);
433         
434         if(!cell){
435             return;
436         }
437         
438         if(e.getTarget().nodeName.toLowerCase() != 'td'){
439             cell = cell.findParent('td', false, true);
440         }
441         
442         var row = cell.findParent('tr', false, true);
443         var cellIndex = cell.dom.cellIndex;
444         var rowIndex = row.dom.rowIndex - 1; // start from 0
445         
446         this.fireEvent('mouseover', this, cell, rowIndex, cellIndex, e);
447         
448     },
449     
450     onMouseout : function(e, el)
451     {
452         var cell = Roo.get(el);
453         
454         if(!cell){
455             return;
456         }
457         
458         if(e.getTarget().nodeName.toLowerCase() != 'td'){
459             cell = cell.findParent('td', false, true);
460         }
461         
462         var row = cell.findParent('tr', false, true);
463         var cellIndex = cell.dom.cellIndex;
464         var rowIndex = row.dom.rowIndex - 1; // start from 0
465         
466         this.fireEvent('mouseout', this, cell, rowIndex, cellIndex, e);
467         
468     },
469     
470     onClick : function(e, el)
471     {
472         var cell = Roo.get(el);
473         
474         if(!cell || (!this.cellSelection && !this.rowSelection)){
475             return;
476         }
477         
478         if(e.getTarget().nodeName.toLowerCase() != 'td'){
479             cell = cell.findParent('td', false, true);
480         }
481         
482         if(!cell || typeof(cell) == 'undefined'){
483             return;
484         }
485         
486         var row = cell.findParent('tr', false, true);
487         
488         if(!row || typeof(row) == 'undefined'){
489             return;
490         }
491         
492         var cellIndex = cell.dom.cellIndex;
493         var rowIndex = this.getRowIndex(row);
494         
495         // why??? - should these not be based on SelectionModel?
496         if(this.cellSelection){
497             this.fireEvent('cellclick', this, cell, rowIndex, cellIndex, e);
498         }
499         
500         if(this.rowSelection){
501             this.fireEvent('rowclick', this, row, rowIndex, e);
502         }
503         
504         
505     },
506         
507     onDblClick : function(e,el)
508     {
509         var cell = Roo.get(el);
510         
511         if(!cell || (!this.cellSelection && !this.rowSelection)){
512             return;
513         }
514         
515         if(e.getTarget().nodeName.toLowerCase() != 'td'){
516             cell = cell.findParent('td', false, true);
517         }
518         
519         if(!cell || typeof(cell) == 'undefined'){
520             return;
521         }
522         
523         var row = cell.findParent('tr', false, true);
524         
525         if(!row || typeof(row) == 'undefined'){
526             return;
527         }
528         
529         var cellIndex = cell.dom.cellIndex;
530         var rowIndex = this.getRowIndex(row);
531         
532         if(this.cellSelection){
533             this.fireEvent('celldblclick', this, cell, rowIndex, cellIndex, e);
534         }
535         
536         if(this.rowSelection){
537             this.fireEvent('rowdblclick', this, row, rowIndex, e);
538         }
539     },
540     
541     sort : function(e,el)
542     {
543         var col = Roo.get(el);
544         
545         if(!col.hasClass('sortable')){
546             return;
547         }
548         
549         var sort = col.attr('sort');
550         var dir = 'ASC';
551         
552         if(col.select('i', true).first().hasClass('glyphicon-arrow-up')){
553             dir = 'DESC';
554         }
555         
556         this.store.sortInfo = {field : sort, direction : dir};
557         
558         if (this.footer) {
559             Roo.log("calling footer first");
560             this.footer.onClick('first');
561         } else {
562         
563             this.store.load({ params : { start : 0 } });
564         }
565     },
566     
567     renderHeader : function()
568     {
569         var header = {
570             tag: 'thead',
571             cn : []
572         };
573         
574         var cm = this.cm;
575         this.totalWidth = 0;
576         
577         for(var i = 0, len = cm.getColumnCount(); i < len; i++){
578             
579             var config = cm.config[i];
580             
581             var c = {
582                 tag: 'th',
583                 cls : 'x-hcol-' + i,
584                 style : '',
585                 html: cm.getColumnHeader(i)
586             };
587             
588             var hh = '';
589             
590             if(typeof(config.sortable) != 'undefined' && config.sortable){
591                 c.cls = 'sortable';
592                 c.html = '<i class="glyphicon"></i>' + c.html;
593             }
594             
595             if(typeof(config.lgHeader) != 'undefined'){
596                 hh += '<span class="hidden-xs hidden-sm hidden-md">' + config.lgHeader + '</span>';
597             }
598             
599             if(typeof(config.mdHeader) != 'undefined'){
600                 hh += '<span class="hidden-xs hidden-sm hidden-lg">' + config.mdHeader + '</span>';
601             }
602             
603             if(typeof(config.smHeader) != 'undefined'){
604                 hh += '<span class="hidden-xs hidden-md hidden-lg">' + config.smHeader + '</span>';
605             }
606             
607             if(typeof(config.xsHeader) != 'undefined'){
608                 hh += '<span class="hidden-sm hidden-md hidden-lg">' + config.xsHeader + '</span>';
609             }
610             
611             if(hh.length){
612                 c.html = hh;
613             }
614             
615             if(typeof(config.tooltip) != 'undefined'){
616                 c.tooltip = config.tooltip;
617             }
618             
619             if(typeof(config.colspan) != 'undefined'){
620                 c.colspan = config.colspan;
621             }
622             
623             if(typeof(config.hidden) != 'undefined' && config.hidden){
624                 c.style += ' display:none;';
625             }
626             
627             if(typeof(config.dataIndex) != 'undefined'){
628                 c.sort = config.dataIndex;
629             }
630             
631            
632             
633             if(typeof(config.align) != 'undefined' && config.align.length){
634                 c.style += ' text-align:' + config.align + ';';
635             }
636             
637             if(typeof(config.width) != 'undefined'){
638                 c.style += ' width:' + config.width + 'px;';
639                 this.totalWidth += config.width;
640             } else {
641                 this.totalWidth += 100; // assume minimum of 100 per column?
642             }
643             
644             if(typeof(config.cls) != 'undefined'){
645                 c.cls = (typeof(c.cls) == 'undefined') ? config.cls : (c.cls + ' ' + config.cls);
646             }
647             
648             ['xs','sm','md','lg'].map(function(size){
649                 
650                 if(typeof(config[size]) == 'undefined'){
651                     return;
652                 }
653                 
654                 if (!config[size]) { // 0 = hidden
655                     c.cls += ' hidden-' + size;
656                     return;
657                 }
658                 
659                 c.cls += ' col-' + size + '-' + config[size];
660
661             });
662             
663             header.cn.push(c)
664         }
665         
666         return header;
667     },
668     
669     renderBody : function()
670     {
671         var body = {
672             tag: 'tbody',
673             cn : [
674                 {
675                     tag: 'tr',
676                     cn : [
677                         {
678                             tag : 'td',
679                             colspan :  this.cm.getColumnCount()
680                         }
681                     ]
682                 }
683             ]
684         };
685         
686         return body;
687     },
688     
689     renderFooter : function()
690     {
691         var footer = {
692             tag: 'tfoot',
693             cn : [
694                 {
695                     tag: 'tr',
696                     cn : [
697                         {
698                             tag : 'td',
699                             colspan :  this.cm.getColumnCount()
700                         }
701                     ]
702                 }
703             ]
704         };
705         
706         return footer;
707     },
708     
709     
710     
711     onLoad : function()
712     {
713 //        Roo.log('ds onload');
714         this.clear();
715         
716         var _this = this;
717         var cm = this.cm;
718         var ds = this.store;
719         
720         Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
721             e.select('i', true).removeClass(['glyphicon-arrow-up', 'glyphicon-arrow-down']);
722             if (_this.store.sortInfo) {
723                     
724                 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
725                     e.select('i', true).addClass(['glyphicon-arrow-up']);
726                 }
727                 
728                 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
729                     e.select('i', true).addClass(['glyphicon-arrow-down']);
730                 }
731             }
732         });
733         
734         var tbody =  this.mainBody;
735               
736         if(ds.getCount() > 0){
737             ds.data.each(function(d,rowIndex){
738                 var row =  this.renderRow(cm, ds, rowIndex);
739                 
740                 tbody.createChild(row);
741                 
742                 var _this = this;
743                 
744                 if(row.cellObjects.length){
745                     Roo.each(row.cellObjects, function(r){
746                         _this.renderCellObject(r);
747                     })
748                 }
749                 
750             }, this);
751         }
752         
753         Roo.each(this.el.select('tbody td', true).elements, function(e){
754             e.on('mouseover', _this.onMouseover, _this);
755         });
756         
757         Roo.each(this.el.select('tbody td', true).elements, function(e){
758             e.on('mouseout', _this.onMouseout, _this);
759         });
760         this.fireEvent('rowsrendered', this);
761         
762         this.autoSize();
763     },
764     
765     
766     onUpdate : function(ds,record)
767     {
768         this.refreshRow(record);
769         this.autoSize();
770     },
771     
772     onRemove : function(ds, record, index, isUpdate){
773         if(isUpdate !== true){
774             this.fireEvent("beforerowremoved", this, index, record);
775         }
776         var bt = this.mainBody.dom;
777         
778         var rows = this.el.select('tbody > tr', true).elements;
779         
780         if(typeof(rows[index]) != 'undefined'){
781             bt.removeChild(rows[index].dom);
782         }
783         
784 //        if(bt.rows[index]){
785 //            bt.removeChild(bt.rows[index]);
786 //        }
787         
788         if(isUpdate !== true){
789             //this.stripeRows(index);
790             //this.syncRowHeights(index, index);
791             //this.layout();
792             this.fireEvent("rowremoved", this, index, record);
793         }
794     },
795     
796     onAdd : function(ds, records, rowIndex)
797     {
798         //Roo.log('on Add called');
799         // - note this does not handle multiple adding very well..
800         var bt = this.mainBody.dom;
801         for (var i =0 ; i < records.length;i++) {
802             //Roo.log('call insert row Add called on ' + rowIndex + ':' + i);
803             //Roo.log(records[i]);
804             //Roo.log(this.store.getAt(rowIndex+i));
805             this.insertRow(this.store, rowIndex + i, false);
806             return;
807         }
808         
809     },
810     
811     
812     refreshRow : function(record){
813         var ds = this.store, index;
814         if(typeof record == 'number'){
815             index = record;
816             record = ds.getAt(index);
817         }else{
818             index = ds.indexOf(record);
819         }
820         this.insertRow(ds, index, true);
821         this.autoSize();
822         this.onRemove(ds, record, index+1, true);
823         this.autoSize();
824         //this.syncRowHeights(index, index);
825         //this.layout();
826         this.fireEvent("rowupdated", this, index, record);
827     },
828     
829     insertRow : function(dm, rowIndex, isUpdate){
830         
831         if(!isUpdate){
832             this.fireEvent("beforerowsinserted", this, rowIndex);
833         }
834             //var s = this.getScrollState();
835         var row = this.renderRow(this.cm, this.store, rowIndex);
836         // insert before rowIndex..
837         var e = this.mainBody.createChild(row,this.getRowDom(rowIndex));
838         
839         var _this = this;
840                 
841         if(row.cellObjects.length){
842             Roo.each(row.cellObjects, function(r){
843                 _this.renderCellObject(r);
844             })
845         }
846             
847         if(!isUpdate){
848             this.fireEvent("rowsinserted", this, rowIndex);
849             //this.syncRowHeights(firstRow, lastRow);
850             //this.stripeRows(firstRow);
851             //this.layout();
852         }
853         
854     },
855     
856     
857     getRowDom : function(rowIndex)
858     {
859         var rows = this.el.select('tbody > tr', true).elements;
860         
861         return (typeof(rows[rowIndex]) == 'undefined') ? false : rows[rowIndex];
862         
863     },
864     // returns the object tree for a tr..
865   
866     
867     renderRow : function(cm, ds, rowIndex) 
868     {
869         var d = ds.getAt(rowIndex);
870         
871         var row = {
872             tag : 'tr',
873             cls : 'x-row-' + rowIndex,
874             cn : []
875         };
876             
877         var cellObjects = [];
878         
879         for(var i = 0, len = cm.getColumnCount(); i < len; i++){
880             var config = cm.config[i];
881             
882             var renderer = cm.getRenderer(i);
883             var value = '';
884             var id = false;
885             
886             if(typeof(renderer) !== 'undefined'){
887                 value = renderer(d.data[cm.getDataIndex(i)], false, d);
888             }
889             // if object are returned, then they are expected to be Roo.bootstrap.Component instances
890             // and are rendered into the cells after the row is rendered - using the id for the element.
891             
892             if(typeof(value) === 'object'){
893                 id = Roo.id();
894                 cellObjects.push({
895                     container : id,
896                     cfg : value 
897                 })
898             }
899             
900             var rowcfg = {
901                 record: d,
902                 rowIndex : rowIndex,
903                 colIndex : i,
904                 rowClass : ''
905             };
906
907             this.fireEvent('rowclass', this, rowcfg);
908             
909             var td = {
910                 tag: 'td',
911                 cls : rowcfg.rowClass + ' x-col-' + i,
912                 style: '',
913                 html: (typeof(value) === 'object') ? '' : value
914             };
915             
916             if (id) {
917                 td.id = id;
918             }
919             
920             if(typeof(config.colspan) != 'undefined'){
921                 td.colspan = config.colspan;
922             }
923             
924             if(typeof(config.hidden) != 'undefined' && config.hidden){
925                 td.style += ' display:none;';
926             }
927             
928             if(typeof(config.align) != 'undefined' && config.align.length){
929                 td.style += ' text-align:' + config.align + ';';
930             }
931             if(typeof(config.valign) != 'undefined' && config.valign.length){
932                 td.style += ' vertical-align:' + config.valign + ';';
933             }
934             
935             if(typeof(config.width) != 'undefined'){
936                 td.style += ' width:' +  config.width + 'px;';
937             }
938             
939             if(typeof(config.cursor) != 'undefined'){
940                 td.style += ' cursor:' +  config.cursor + ';';
941             }
942             
943             if(typeof(config.cls) != 'undefined'){
944                 td.cls = (typeof(td.cls) == 'undefined') ? config.cls : (td.cls + ' ' + config.cls);
945             }
946             
947             ['xs','sm','md','lg'].map(function(size){
948                 
949                 if(typeof(config[size]) == 'undefined'){
950                     return;
951                 }
952                 
953                 if (!config[size]) { // 0 = hidden
954                     td.cls += ' hidden-' + size;
955                     return;
956                 }
957                 
958                 td.cls += ' col-' + size + '-' + config[size];
959
960             });
961             
962             row.cn.push(td);
963            
964         }
965         
966         row.cellObjects = cellObjects;
967         
968         return row;
969           
970     },
971     
972     
973     
974     onBeforeLoad : function()
975     {
976         
977     },
978      /**
979      * Remove all rows
980      */
981     clear : function()
982     {
983         this.el.select('tbody', true).first().dom.innerHTML = '';
984     },
985     /**
986      * Show or hide a row.
987      * @param {Number} rowIndex to show or hide
988      * @param {Boolean} state hide
989      */
990     setRowVisibility : function(rowIndex, state)
991     {
992         var bt = this.mainBody.dom;
993         
994         var rows = this.el.select('tbody > tr', true).elements;
995         
996         if(typeof(rows[rowIndex]) == 'undefined'){
997             return;
998         }
999         rows[rowIndex].dom.style.display = state ? '' : 'none';
1000     },
1001     
1002     
1003     getSelectionModel : function(){
1004         if(!this.selModel){
1005             this.selModel = new Roo.bootstrap.Table.RowSelectionModel({grid: this});
1006         }
1007         return this.selModel;
1008     },
1009     /*
1010      * Render the Roo.bootstrap object from renderder
1011      */
1012     renderCellObject : function(r)
1013     {
1014         var _this = this;
1015         
1016         r.cfg.parentId = (typeof(r.container) == 'string') ? r.container : r.container.id;
1017         
1018         var t = r.cfg.render(r.container);
1019         
1020         if(r.cfg.cn){
1021             Roo.each(r.cfg.cn, function(c){
1022                 var child = {
1023                     container: t.getChildContainer(),
1024                     cfg: c
1025                 };
1026                 _this.renderCellObject(child);
1027             })
1028         }
1029     },
1030     
1031     getRowIndex : function(row)
1032     {
1033         var rowIndex = -1;
1034         
1035         Roo.each(this.el.select('tbody > tr', true).elements, function(el, index){
1036             if(el != row){
1037                 return;
1038             }
1039             
1040             rowIndex = index;
1041         });
1042         
1043         return rowIndex;
1044     },
1045      /**
1046      * Returns the grid's underlying element = used by panel.Grid
1047      * @return {Element} The element
1048      */
1049     getGridEl : function(){
1050         return this.el;
1051     },
1052      /**
1053      * Forces a resize - used by panel.Grid
1054      * @return {Element} The element
1055      */
1056     autoSize : function()
1057     {
1058         //var ctr = Roo.get(this.container.dom.parentElement);
1059         var ctr = Roo.get(this.el.dom);
1060         
1061         var thd = this.getGridEl().select('thead',true).first();
1062         var tbd = this.getGridEl().select('tbody', true).first();
1063         var tfd = this.getGridEl().select('tfoot', true).first();
1064         
1065         var cw = ctr.getWidth();
1066         
1067         if (tbd) {
1068             
1069             tbd.setSize(ctr.getWidth(),
1070                         ctr.getHeight() - ((thd ? thd.getHeight() : 0) + (tfd ? tfd.getHeight() : 0))
1071             );
1072             var barsize = (tbd.dom.offsetWidth - tbd.dom.clientWidth);
1073             cw -= barsize;
1074         }
1075         cw = Math.max(cw, this.totalWidth);
1076         this.getGridEl().select('tr',true).setWidth(cw);
1077         // resize 'expandable coloumn?
1078         
1079         return; // we doe not have a view in this design..
1080         
1081     },
1082     onBodyScroll: function()
1083     {
1084         //Roo.log("body scrolled');" + this.mainBody.dom.scrollLeft);
1085         if(this.mainHead){
1086             this.mainHead.setStyle({
1087                 'position' : 'relative',
1088                 'left': (-1* this.mainBody.dom.scrollLeft) + 'px'
1089             });
1090         }
1091         
1092         if(this.lazyLoad){
1093             
1094             var scrollHeight = this.mainBody.dom.scrollHeight;
1095             
1096             var scrollTop = Math.ceil(this.mainBody.getScroll().top);
1097             
1098             var height = this.mainBody.getHeight();
1099             
1100             if(scrollHeight - height == scrollTop) {
1101                 
1102                 var total = this.ds.getTotalCount();
1103                 
1104                 if(this.footer.cursor + this.footer.pageSize < total){
1105                     
1106                     this.footer.ds.load({
1107                         params : {
1108                             start : this.footer.cursor + this.footer.pageSize,
1109                             limit : this.footer.pageSize
1110                         },
1111                         add : true
1112                     });
1113                 }
1114             }
1115             
1116         }
1117     },
1118     
1119     onHeaderChange : function()
1120     {
1121         var header = this.renderHeader();
1122         var table = this.el.select('table', true).first();
1123         
1124         this.mainHead.remove();
1125         this.mainHead = table.createChild(header, this.mainBody, false);
1126     },
1127     
1128     onHiddenChange : function(colModel, colIndex, hidden)
1129     {
1130         var thSelector = '#' + this.id + ' .x-hcol-' + colIndex;
1131         var tdSelector = '#' + this.id + ' .x-col-' + colIndex;
1132         
1133         this.CSS.updateRule(thSelector, "display", "");
1134         this.CSS.updateRule(tdSelector, "display", "");
1135         
1136         if(hidden){
1137             this.CSS.updateRule(thSelector, "display", "none");
1138             this.CSS.updateRule(tdSelector, "display", "none");
1139         }
1140         
1141         this.onHeaderChange();
1142         this.onLoad();
1143         
1144     }
1145     
1146 });
1147
1148  
1149
1150