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