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