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