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