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);
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                 style : '',
593                 html: cm.getColumnHeader(i)
594             };
595             
596             var hh = '';
597             
598             if(typeof(config.sortable) != 'undefined' && config.sortable){
599                 c.cls = 'sortable';
600                 c.html = '<i class="glyphicon"></i>' + c.html;
601             }
602             
603             if(typeof(config.lgHeader) != 'undefined'){
604                 hh += '<span class="hidden-xs hidden-sm hidden-md">' + config.lgHeader + '</span>';
605             }
606             
607             if(typeof(config.mdHeader) != 'undefined'){
608                 hh += '<span class="hidden-xs hidden-sm hidden-lg">' + config.mdHeader + '</span>';
609             }
610             
611             if(typeof(config.smHeader) != 'undefined'){
612                 hh += '<span class="hidden-xs hidden-md hidden-lg">' + config.smHeader + '</span>';
613             }
614             
615             if(typeof(config.xsHeader) != 'undefined'){
616                 hh += '<span class="hidden-sm hidden-md hidden-lg">' + config.xsHeader + '</span>';
617             }
618             
619             if(hh.length){
620                 c.html = hh;
621             }
622             
623             if(typeof(config.tooltip) != 'undefined'){
624                 c.tooltip = config.tooltip;
625             }
626             
627             if(typeof(config.colspan) != 'undefined'){
628                 c.colspan = config.colspan;
629             }
630             
631             if(typeof(config.hidden) != 'undefined' && config.hidden){
632                 c.style += ' display:none;';
633             }
634             
635             if(typeof(config.dataIndex) != 'undefined'){
636                 c.sort = config.dataIndex;
637             }
638             
639            
640             
641             if(typeof(config.align) != 'undefined' && config.align.length){
642                 c.style += ' text-align:' + config.align + ';';
643             }
644             
645             if(typeof(config.width) != 'undefined'){
646                 c.style += ' width:' + config.width + 'px;';
647                 this.totalWidth += config.width;
648             } else {
649                 this.totalWidth += 100; // assume minimum of 100 per column?
650             }
651             
652             if(typeof(config.cls) != 'undefined'){
653                 c.cls = (typeof(c.cls) == 'undefined') ? config.cls : (c.cls + ' ' + config.cls);
654             }
655             
656             ['xs','sm','md','lg'].map(function(size){
657                 
658                 if(typeof(config[size]) == 'undefined'){
659                     return;
660                 }
661                 
662                 if (!config[size]) { // 0 = hidden
663                     c.cls += ' hidden-' + size;
664                     return;
665                 }
666                 
667                 c.cls += ' col-' + size + '-' + config[size];
668
669             });
670             
671             if(config.hidden){
672                 c.cls += ' hidden';
673             }
674             
675             header.cn.push(c)
676         }
677         
678         return header;
679     },
680     
681     renderBody : function()
682     {
683         var body = {
684             tag: 'tbody',
685             cn : [
686                 {
687                     tag: 'tr',
688                     cn : [
689                         {
690                             tag : 'td',
691                             colspan :  this.cm.getColumnCount()
692                         }
693                     ]
694                 }
695             ]
696         };
697         
698         return body;
699     },
700     
701     renderFooter : function()
702     {
703         var footer = {
704             tag: 'tfoot',
705             cn : [
706                 {
707                     tag: 'tr',
708                     cn : [
709                         {
710                             tag : 'td',
711                             colspan :  this.cm.getColumnCount()
712                         }
713                     ]
714                 }
715             ]
716         };
717         
718         return footer;
719     },
720     
721     
722     
723     onLoad : function()
724     {
725 //        Roo.log('ds onload');
726         this.clear();
727         
728         var _this = this;
729         var cm = this.cm;
730         var ds = this.store;
731         
732         Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
733             e.select('i', true).removeClass(['glyphicon-arrow-up', 'glyphicon-arrow-down']);
734             if (_this.store.sortInfo) {
735                     
736                 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
737                     e.select('i', true).addClass(['glyphicon-arrow-up']);
738                 }
739                 
740                 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
741                     e.select('i', true).addClass(['glyphicon-arrow-down']);
742                 }
743             }
744         });
745         
746         var tbody =  this.mainBody;
747               
748         if(ds.getCount() > 0){
749             ds.data.each(function(d,rowIndex){
750                 var row =  this.renderRow(cm, ds, rowIndex);
751                 
752                 tbody.createChild(row);
753                 
754                 var _this = this;
755                 
756                 if(row.cellObjects.length){
757                     Roo.each(row.cellObjects, function(r){
758                         _this.renderCellObject(r);
759                     })
760                 }
761                 
762             }, this);
763         }
764         
765         Roo.each(this.el.select('tbody td', true).elements, function(e){
766             e.on('mouseover', _this.onMouseover, _this);
767         });
768         
769         Roo.each(this.el.select('tbody td', true).elements, function(e){
770             e.on('mouseout', _this.onMouseout, _this);
771         });
772         this.fireEvent('rowsrendered', this);
773         //if(this.loadMask){
774         //    this.maskEl.hide();
775         //}
776         
777         this.autoSize();
778     },
779     
780     
781     onUpdate : function(ds,record)
782     {
783         this.refreshRow(record);
784         this.autoSize();
785     },
786     
787     onRemove : function(ds, record, index, isUpdate){
788         if(isUpdate !== true){
789             this.fireEvent("beforerowremoved", this, index, record);
790         }
791         var bt = this.mainBody.dom;
792         
793         var rows = this.el.select('tbody > tr', true).elements;
794         
795         if(typeof(rows[index]) != 'undefined'){
796             bt.removeChild(rows[index].dom);
797         }
798         
799 //        if(bt.rows[index]){
800 //            bt.removeChild(bt.rows[index]);
801 //        }
802         
803         if(isUpdate !== true){
804             //this.stripeRows(index);
805             //this.syncRowHeights(index, index);
806             //this.layout();
807             this.fireEvent("rowremoved", this, index, record);
808         }
809     },
810     
811     onAdd : function(ds, records, rowIndex)
812     {
813         //Roo.log('on Add called');
814         // - note this does not handle multiple adding very well..
815         var bt = this.mainBody.dom;
816         for (var i =0 ; i < records.length;i++) {
817             //Roo.log('call insert row Add called on ' + rowIndex + ':' + i);
818             //Roo.log(records[i]);
819             //Roo.log(this.store.getAt(rowIndex+i));
820             this.insertRow(this.store, rowIndex + i, false);
821             return;
822         }
823         
824     },
825     
826     
827     refreshRow : function(record){
828         var ds = this.store, index;
829         if(typeof record == 'number'){
830             index = record;
831             record = ds.getAt(index);
832         }else{
833             index = ds.indexOf(record);
834         }
835         this.insertRow(ds, index, true);
836         this.autoSize();
837         this.onRemove(ds, record, index+1, true);
838         this.autoSize();
839         //this.syncRowHeights(index, index);
840         //this.layout();
841         this.fireEvent("rowupdated", this, index, record);
842     },
843     
844     insertRow : function(dm, rowIndex, isUpdate){
845         
846         if(!isUpdate){
847             this.fireEvent("beforerowsinserted", this, rowIndex);
848         }
849             //var s = this.getScrollState();
850         var row = this.renderRow(this.cm, this.store, rowIndex);
851         // insert before rowIndex..
852         var e = this.mainBody.createChild(row,this.getRowDom(rowIndex));
853         
854         var _this = this;
855                 
856         if(row.cellObjects.length){
857             Roo.each(row.cellObjects, function(r){
858                 _this.renderCellObject(r);
859             })
860         }
861             
862         if(!isUpdate){
863             this.fireEvent("rowsinserted", this, rowIndex);
864             //this.syncRowHeights(firstRow, lastRow);
865             //this.stripeRows(firstRow);
866             //this.layout();
867         }
868         
869     },
870     
871     
872     getRowDom : function(rowIndex)
873     {
874         var rows = this.el.select('tbody > tr', true).elements;
875         
876         return (typeof(rows[rowIndex]) == 'undefined') ? false : rows[rowIndex];
877         
878     },
879     // returns the object tree for a tr..
880   
881     
882     renderRow : function(cm, ds, rowIndex) 
883     {
884         var d = ds.getAt(rowIndex);
885         
886         var row = {
887             tag : 'tr',
888             cls : 'roo-bootstrap-tbody-row-' + rowIndex,
889             cn : []
890         };
891             
892         var cellObjects = [];
893         
894         for(var i = 0, len = cm.getColumnCount(); i < len; i++){
895             var config = cm.config[i];
896             
897             var renderer = cm.getRenderer(i);
898             var value = '';
899             var id = false;
900             
901             if(typeof(renderer) !== 'undefined'){
902                 value = renderer(d.data[cm.getDataIndex(i)], false, d);
903             }
904             // if object are returned, then they are expected to be Roo.bootstrap.Component instances
905             // and are rendered into the cells after the row is rendered - using the id for the element.
906             
907             if(typeof(value) === 'object'){
908                 id = Roo.id();
909                 cellObjects.push({
910                     container : id,
911                     cfg : value 
912                 })
913             }
914             
915             var rowcfg = {
916                 record: d,
917                 rowIndex : rowIndex,
918                 colIndex : i,
919                 rowClass : ''
920             };
921
922             this.fireEvent('rowclass', this, rowcfg);
923             
924             var td = {
925                 tag: 'td',
926                 cls : rowcfg.rowClass + ' roo-bootstrap-tbody-col-' + i,
927                 style: '',
928                 html: (typeof(value) === 'object') ? '' : value
929             };
930             
931             if (id) {
932                 td.id = id;
933             }
934             
935             if(typeof(config.colspan) != 'undefined'){
936                 td.colspan = config.colspan;
937             }
938             
939             if(typeof(config.hidden) != 'undefined' && config.hidden){
940                 td.style += ' display:none;';
941             }
942             
943             if(typeof(config.align) != 'undefined' && config.align.length){
944                 td.style += ' text-align:' + config.align + ';';
945             }
946             
947             if(typeof(config.width) != 'undefined'){
948                 td.style += ' width:' +  config.width + 'px;';
949             }
950             
951             if(typeof(config.cursor) != 'undefined'){
952                 td.style += ' cursor:' +  config.cursor + ';';
953             }
954             
955             if(typeof(config.cls) != 'undefined'){
956                 td.cls = (typeof(td.cls) == 'undefined') ? config.cls : (td.cls + ' ' + config.cls);
957             }
958             
959             ['xs','sm','md','lg'].map(function(size){
960                 
961                 if(typeof(config[size]) == 'undefined'){
962                     return;
963                 }
964                 
965                 if (!config[size]) { // 0 = hidden
966                     td.cls += ' hidden-' + size;
967                     return;
968                 }
969                 
970                 td.cls += ' col-' + size + '-' + config[size];
971
972             });
973             
974             if(config.hidden){
975                 td.cls += ' hidden';
976             }
977              
978             row.cn.push(td);
979            
980         }
981         
982         row.cellObjects = cellObjects;
983         
984         return row;
985           
986     },
987     
988     
989     
990     onBeforeLoad : function()
991     {
992         //Roo.log('ds onBeforeLoad');
993         
994         //this.clear();
995         
996         //if(this.loadMask){
997         //    this.maskEl.show();
998         //}
999     },
1000      /**
1001      * Remove all rows
1002      */
1003     clear : function()
1004     {
1005         this.el.select('tbody', true).first().dom.innerHTML = '';
1006     },
1007     /**
1008      * Show or hide a row.
1009      * @param {Number} rowIndex to show or hide
1010      * @param {Boolean} state hide
1011      */
1012     setRowVisibility : function(rowIndex, state)
1013     {
1014         var bt = this.mainBody.dom;
1015         
1016         var rows = this.el.select('tbody > tr', true).elements;
1017         
1018         if(typeof(rows[rowIndex]) == 'undefined'){
1019             return;
1020         }
1021         rows[rowIndex].dom.style.display = state ? '' : 'none';
1022     },
1023     
1024     
1025     getSelectionModel : function(){
1026         if(!this.selModel){
1027             this.selModel = new Roo.bootstrap.Table.RowSelectionModel({grid: this});
1028         }
1029         return this.selModel;
1030     },
1031     /*
1032      * Render the Roo.bootstrap object from renderder
1033      */
1034     renderCellObject : function(r)
1035     {
1036         var _this = this;
1037         
1038         r.cfg.parentId = (typeof(r.container) == 'string') ? r.container : r.container.id;
1039         
1040         var t = r.cfg.render(r.container);
1041         
1042         if(r.cfg.cn){
1043             Roo.each(r.cfg.cn, function(c){
1044                 var child = {
1045                     container: t.getChildContainer(),
1046                     cfg: c
1047                 };
1048                 _this.renderCellObject(child);
1049             })
1050         }
1051     },
1052     
1053     getRowIndex : function(row)
1054     {
1055         var rowIndex = -1;
1056         
1057         Roo.each(this.el.select('tbody > tr', true).elements, function(el, index){
1058             if(el != row){
1059                 return;
1060             }
1061             
1062             rowIndex = index;
1063         });
1064         
1065         return rowIndex;
1066     },
1067      /**
1068      * Returns the grid's underlying element = used by panel.Grid
1069      * @return {Element} The element
1070      */
1071     getGridEl : function(){
1072         return this.el;
1073     },
1074      /**
1075      * Forces a resize - used by panel.Grid
1076      * @return {Element} The element
1077      */
1078     autoSize : function()
1079     {
1080         //var ctr = Roo.get(this.container.dom.parentElement);
1081         var ctr = Roo.get(this.el.dom);
1082         
1083         var thd = this.getGridEl().select('thead',true).first();
1084         var tbd = this.getGridEl().select('tbody', true).first();
1085         var tfd = this.getGridEl().select('tfoot', true).first();
1086         
1087         var cw = ctr.getWidth();
1088         
1089         if (tbd) {
1090             
1091             tbd.setSize(ctr.getWidth(),
1092                         ctr.getHeight() - ((thd ? thd.getHeight() : 0) + (tfd ? tfd.getHeight() : 0))
1093             );
1094             var barsize = (tbd.dom.offsetWidth - tbd.dom.clientWidth);
1095             cw -= barsize;
1096         }
1097         cw = Math.max(cw, this.totalWidth);
1098         this.getGridEl().select('tr',true).setWidth(cw);
1099         // resize 'expandable coloumn?
1100         
1101         return; // we doe not have a view in this design..
1102         
1103     },
1104     onBodyScroll: function()
1105     {
1106         //Roo.log("body scrolled');" + this.mainBody.dom.scrollLeft);
1107         if(this.mainHead){
1108             this.mainHead.setStyle({
1109                 'position' : 'relative',
1110                 'left': (-1* this.mainBody.dom.scrollLeft) + 'px'
1111             });
1112         }
1113         
1114         if(this.lazyLoad){
1115             
1116             var scrollHeight = this.mainBody.dom.scrollHeight;
1117             
1118             var scrollTop = Math.ceil(this.mainBody.getScroll().top);
1119             
1120             var height = this.mainBody.getHeight();
1121             
1122             if(scrollHeight - height == scrollTop) {
1123                 
1124                 var total = this.ds.getTotalCount();
1125                 
1126                 if(this.footer.cursor + this.footer.pageSize < total){
1127                     
1128                     this.footer.ds.load({
1129                         params : {
1130                             start : this.footer.cursor + this.footer.pageSize,
1131                             limit : this.footer.pageSize
1132                         },
1133                         add : true
1134                     });
1135                 }
1136             }
1137             
1138         }
1139     },
1140     
1141     onHeaderChange : function()
1142     {
1143         
1144         var header = this.renderHeader();
1145         var table = this.el.select('table', true).first();
1146         
1147         this.mainHead.remove();
1148         this.mainHead = table.createChild(header, this.mainBody, false);
1149     },
1150     
1151     onHiddenChange : function()
1152     {
1153         /*
1154         this.onHeaderChange();
1155         this.onLoad();
1156         */
1157     }
1158     
1159 });
1160
1161  
1162
1163