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