5dc5426c6cf97d29ea717e5e6b07198e2aadc300
[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  * @cfg {Boolean} auto_hide_footer  auto hide footer if only one page (default false)
39  
40  * 
41  * @constructor
42  * Create a new Table
43  * @param {Object} config The config object
44  */
45
46 Roo.bootstrap.Table = function(config){
47     Roo.bootstrap.Table.superclass.constructor.call(this, config);
48     
49   
50     
51     // BC...
52     this.rowSelection = (typeof(config.rowSelection) != 'undefined') ? config.rowSelection : this.rowSelection;
53     this.cellSelection = (typeof(config.cellSelection) != 'undefined') ? config.cellSelection : this.cellSelection;
54     this.headerShow = (typeof(config.thead) != 'undefined') ? config.thead : this.headerShow;
55     this.footerShow = (typeof(config.tfoot) != 'undefined') ? config.tfoot : this.footerShow;
56     
57     this.sm = this.sm || {xtype: 'RowSelectionModel'};
58     if (this.sm) {
59         this.sm.grid = this;
60         this.selModel = Roo.factory(this.sm, Roo.bootstrap.Table);
61         this.sm = this.selModel;
62         this.sm.xmodule = this.xmodule || false;
63     }
64     
65     if (this.cm && typeof(this.cm.config) == 'undefined') {
66         this.colModel = new Roo.grid.ColumnModel(this.cm);
67         this.cm = this.colModel;
68         this.cm.xmodule = this.xmodule || false;
69     }
70     if (this.store) {
71         this.store= Roo.factory(this.store, Roo.data);
72         this.ds = this.store;
73         this.ds.xmodule = this.xmodule || false;
74          
75     }
76     if (this.footer && this.store) {
77         this.footer.dataSource = this.ds;
78         this.footer = Roo.factory(this.footer);
79     }
80     
81     /** @private */
82     this.addEvents({
83         /**
84          * @event cellclick
85          * Fires when a cell is clicked
86          * @param {Roo.bootstrap.Table} this
87          * @param {Roo.Element} el
88          * @param {Number} rowIndex
89          * @param {Number} columnIndex
90          * @param {Roo.EventObject} e
91          */
92         "cellclick" : true,
93         /**
94          * @event celldblclick
95          * Fires when a cell is double clicked
96          * @param {Roo.bootstrap.Table} this
97          * @param {Roo.Element} el
98          * @param {Number} rowIndex
99          * @param {Number} columnIndex
100          * @param {Roo.EventObject} e
101          */
102         "celldblclick" : true,
103         /**
104          * @event rowclick
105          * Fires when a row is clicked
106          * @param {Roo.bootstrap.Table} this
107          * @param {Roo.Element} el
108          * @param {Number} rowIndex
109          * @param {Roo.EventObject} e
110          */
111         "rowclick" : true,
112         /**
113          * @event rowdblclick
114          * Fires when a row is double clicked
115          * @param {Roo.bootstrap.Table} this
116          * @param {Roo.Element} el
117          * @param {Number} rowIndex
118          * @param {Roo.EventObject} e
119          */
120         "rowdblclick" : true,
121         /**
122          * @event mouseover
123          * Fires when a mouseover occur
124          * @param {Roo.bootstrap.Table} this
125          * @param {Roo.Element} el
126          * @param {Number} rowIndex
127          * @param {Number} columnIndex
128          * @param {Roo.EventObject} e
129          */
130         "mouseover" : true,
131         /**
132          * @event mouseout
133          * Fires when a mouseout occur
134          * @param {Roo.bootstrap.Table} this
135          * @param {Roo.Element} el
136          * @param {Number} rowIndex
137          * @param {Number} columnIndex
138          * @param {Roo.EventObject} e
139          */
140         "mouseout" : true,
141         /**
142          * @event rowclass
143          * Fires when a row is rendered, so you can change add a style to it.
144          * @param {Roo.bootstrap.Table} this
145          * @param {Object} rowcfg   contains record  rowIndex colIndex and rowClass - set rowClass to add a style.
146          */
147         'rowclass' : true,
148           /**
149          * @event rowsrendered
150          * Fires when all the  rows have been rendered
151          * @param {Roo.bootstrap.Table} this
152          */
153         'rowsrendered' : true,
154         /**
155          * @event contextmenu
156          * The raw contextmenu event for the entire grid.
157          * @param {Roo.EventObject} e
158          */
159         "contextmenu" : true,
160         /**
161          * @event rowcontextmenu
162          * Fires when a row is right clicked
163          * @param {Roo.bootstrap.Table} this
164          * @param {Number} rowIndex
165          * @param {Roo.EventObject} e
166          */
167         "rowcontextmenu" : true,
168         /**
169          * @event cellcontextmenu
170          * Fires when a cell is right clicked
171          * @param {Roo.bootstrap.Table} this
172          * @param {Number} rowIndex
173          * @param {Number} cellIndex
174          * @param {Roo.EventObject} e
175          */
176          "cellcontextmenu" : true,
177          /**
178          * @event headercontextmenu
179          * Fires when a header is right clicked
180          * @param {Roo.bootstrap.Table} this
181          * @param {Number} columnIndex
182          * @param {Roo.EventObject} e
183          */
184         "headercontextmenu" : true
185     });
186 };
187
188 Roo.extend(Roo.bootstrap.Table, Roo.bootstrap.Component,  {
189     
190     cls: false,
191     align: false,
192     bgcolor: false,
193     border: false,
194     cellpadding: false,
195     cellspacing: false,
196     frame: false,
197     rules: false,
198     sortable: false,
199     summary: false,
200     width: false,
201     striped : false,
202     scrollBody : false,
203     bordered: false,
204     hover:  false,
205     condensed : false,
206     responsive : false,
207     sm : false,
208     cm : false,
209     store : false,
210     loadMask : false,
211     footerShow : true,
212     headerShow : true,
213   
214     rowSelection : false,
215     cellSelection : false,
216     layout : false,
217     
218     // Roo.Element - the tbody
219     mainBody: false,
220     // Roo.Element - thead element
221     mainHead: false,
222     
223     container: false, // used by gridpanel...
224     
225     lazyLoad : false,
226     
227     CSS : Roo.util.CSS,
228     
229     auto_hide_footer : false,
230     
231     getAutoCreate : function()
232     {
233         var cfg = Roo.apply({}, Roo.bootstrap.Table.superclass.getAutoCreate.call(this));
234         
235         cfg = {
236             tag: 'table',
237             cls : 'table',
238             cn : []
239         };
240         if (this.scrollBody) {
241             cfg.cls += ' table-body-fixed';
242         }    
243         if (this.striped) {
244             cfg.cls += ' table-striped';
245         }
246         
247         if (this.hover) {
248             cfg.cls += ' table-hover';
249         }
250         if (this.bordered) {
251             cfg.cls += ' table-bordered';
252         }
253         if (this.condensed) {
254             cfg.cls += ' table-condensed';
255         }
256         if (this.responsive) {
257             cfg.cls += ' table-responsive';
258         }
259         
260         if (this.cls) {
261             cfg.cls+=  ' ' +this.cls;
262         }
263         
264         // this lot should be simplifed...
265         var _t = this;
266         var cp = [
267             'align',
268             'bgcolor',
269             'border',
270             'cellpadding',
271             'cellspacing',
272             'frame',
273             'rules',
274             'sortable',
275             'summary',
276             'width'
277         ].forEach(function(k) {
278             if (_t[k]) {
279                 cfg[k] = _t[k];
280             }
281         });
282         
283         
284         if (this.layout) {
285             cfg.style = (typeof(cfg.style) == 'undefined') ? ('table-layout:' + this.layout + ';') : (cfg.style + ('table-layout:' + this.layout + ';'));
286         }
287         
288         if(this.store || this.cm){
289             if(this.headerShow){
290                 cfg.cn.push(this.renderHeader());
291             }
292             
293             cfg.cn.push(this.renderBody());
294             
295             if(this.footerShow){
296                 cfg.cn.push(this.renderFooter());
297             }
298             // where does this come from?
299             //cfg.cls+=  ' TableGrid';
300         }
301         
302         return { cn : [ cfg ] };
303     },
304     
305     initEvents : function()
306     {   
307         if(!this.store || !this.cm){
308             return;
309         }
310         if (this.selModel) {
311             this.selModel.initEvents();
312         }
313         
314         
315         //Roo.log('initEvents with ds!!!!');
316         
317         this.mainBody = this.el.select('tbody', true).first();
318         this.mainHead = this.el.select('thead', true).first();
319         this.mainFoot = this.el.select('tfoot', true).first();
320         
321         
322         
323         var _this = this;
324         
325         Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
326             e.on('click', _this.sort, _this);
327         });
328         
329         this.mainBody.on("click", this.onClick, this);
330         this.mainBody.on("dblclick", this.onDblClick, this);
331         
332         // why is this done????? = it breaks dialogs??
333         //this.parent().el.setStyle('position', 'relative');
334         
335         
336         if (this.footer) {
337             this.footer.parentId = this.id;
338             this.footer.onRender(this.el.select('tfoot tr td').first(), null);
339             
340             if(this.lazyLoad){
341                 this.el.select('tfoot tr td').first().addClass('hide');
342             }
343         } 
344         
345         if(this.loadMask) {
346             this.maskEl = new Roo.LoadMask(this.el, { store : this.ds, msgCls: 'roo-el-mask-msg' });
347         }
348         
349         this.store.on('load', this.onLoad, this);
350         this.store.on('beforeload', this.onBeforeLoad, this);
351         this.store.on('update', this.onUpdate, this);
352         this.store.on('add', this.onAdd, this);
353         this.store.on("clear", this.clear, this);
354         
355         this.el.on("contextmenu", this.onContextMenu, this);
356         
357         this.mainBody.on('scroll', this.onBodyScroll, this);
358         
359         this.cm.on("headerchange", this.onHeaderChange, this);
360         
361         this.cm.on("hiddenchange", this.onHiddenChange, this, arguments);
362         
363     },
364     
365     onContextMenu : function(e, t)
366     {
367         this.processEvent("contextmenu", e);
368     },
369     
370     processEvent : function(name, e)
371     {
372         if (name != 'touchstart' ) {
373             this.fireEvent(name, e);    
374         }
375         
376         var t = e.getTarget();
377         
378         var cell = Roo.get(t);
379         
380         if(!cell){
381             return;
382         }
383         
384         if(cell.findParent('tfoot', false, true)){
385             return;
386         }
387         
388         if(cell.findParent('thead', false, true)){
389             
390             if(e.getTarget().nodeName.toLowerCase() != 'th'){
391                 cell = Roo.get(t).findParent('th', false, true);
392                 if (!cell) {
393                     Roo.log("failed to find th in thead?");
394                     Roo.log(e.getTarget());
395                     return;
396                 }
397             }
398             
399             var cellIndex = cell.dom.cellIndex;
400             
401             var ename = name == 'touchstart' ? 'click' : name;
402             this.fireEvent("header" + ename, this, cellIndex, e);
403             
404             return;
405         }
406         
407         if(e.getTarget().nodeName.toLowerCase() != 'td'){
408             cell = Roo.get(t).findParent('td', false, true);
409             if (!cell) {
410                 Roo.log("failed to find th in tbody?");
411                 Roo.log(e.getTarget());
412                 return;
413             }
414         }
415         
416         var row = cell.findParent('tr', false, true);
417         var cellIndex = cell.dom.cellIndex;
418         var rowIndex = row.dom.rowIndex - 1;
419         
420         if(row !== false){
421             
422             this.fireEvent("row" + name, this, rowIndex, e);
423             
424             if(cell !== false){
425             
426                 this.fireEvent("cell" + name, this, rowIndex, cellIndex, e);
427             }
428         }
429         
430     },
431     
432     onMouseover : function(e, el)
433     {
434         var cell = Roo.get(el);
435         
436         if(!cell){
437             return;
438         }
439         
440         if(e.getTarget().nodeName.toLowerCase() != 'td'){
441             cell = cell.findParent('td', false, true);
442         }
443         
444         var row = cell.findParent('tr', false, true);
445         var cellIndex = cell.dom.cellIndex;
446         var rowIndex = row.dom.rowIndex - 1; // start from 0
447         
448         this.fireEvent('mouseover', this, cell, rowIndex, cellIndex, e);
449         
450     },
451     
452     onMouseout : function(e, el)
453     {
454         var cell = Roo.get(el);
455         
456         if(!cell){
457             return;
458         }
459         
460         if(e.getTarget().nodeName.toLowerCase() != 'td'){
461             cell = cell.findParent('td', false, true);
462         }
463         
464         var row = cell.findParent('tr', false, true);
465         var cellIndex = cell.dom.cellIndex;
466         var rowIndex = row.dom.rowIndex - 1; // start from 0
467         
468         this.fireEvent('mouseout', this, cell, rowIndex, cellIndex, e);
469         
470     },
471     
472     onClick : function(e, el)
473     {
474         var cell = Roo.get(el);
475         
476         if(!cell || (!this.cellSelection && !this.rowSelection)){
477             return;
478         }
479         
480         if(e.getTarget().nodeName.toLowerCase() != 'td'){
481             cell = cell.findParent('td', false, true);
482         }
483         
484         if(!cell || typeof(cell) == 'undefined'){
485             return;
486         }
487         
488         var row = cell.findParent('tr', false, true);
489         
490         if(!row || typeof(row) == 'undefined'){
491             return;
492         }
493         
494         var cellIndex = cell.dom.cellIndex;
495         var rowIndex = this.getRowIndex(row);
496         
497         // why??? - should these not be based on SelectionModel?
498         if(this.cellSelection){
499             this.fireEvent('cellclick', this, cell, rowIndex, cellIndex, e);
500         }
501         
502         if(this.rowSelection){
503             this.fireEvent('rowclick', this, row, rowIndex, e);
504         }
505         
506         
507     },
508         
509     onDblClick : function(e,el)
510     {
511         var cell = Roo.get(el);
512         
513         if(!cell || (!this.cellSelection && !this.rowSelection)){
514             return;
515         }
516         
517         if(e.getTarget().nodeName.toLowerCase() != 'td'){
518             cell = cell.findParent('td', false, true);
519         }
520         
521         if(!cell || typeof(cell) == 'undefined'){
522             return;
523         }
524         
525         var row = cell.findParent('tr', false, true);
526         
527         if(!row || typeof(row) == 'undefined'){
528             return;
529         }
530         
531         var cellIndex = cell.dom.cellIndex;
532         var rowIndex = this.getRowIndex(row);
533         
534         if(this.cellSelection){
535             this.fireEvent('celldblclick', this, cell, rowIndex, cellIndex, e);
536         }
537         
538         if(this.rowSelection){
539             this.fireEvent('rowdblclick', this, row, rowIndex, e);
540         }
541     },
542     
543     sort : function(e,el)
544     {
545         var col = Roo.get(el);
546         
547         if(!col.hasClass('sortable')){
548             return;
549         }
550         
551         var sort = col.attr('sort');
552         var dir = 'ASC';
553         
554         if(col.select('i', true).first().hasClass('glyphicon-arrow-up')){
555             dir = 'DESC';
556         }
557         
558         this.store.sortInfo = {field : sort, direction : dir};
559         
560         if (this.footer) {
561             Roo.log("calling footer first");
562             this.footer.onClick('first');
563         } else {
564         
565             this.store.load({ params : { start : 0 } });
566         }
567     },
568     
569     renderHeader : function()
570     {
571         var header = {
572             tag: 'thead',
573             cn : []
574         };
575         
576         var cm = this.cm;
577         this.totalWidth = 0;
578         
579         for(var i = 0, len = cm.getColumnCount(); i < len; i++){
580             
581             var config = cm.config[i];
582             
583             var c = {
584                 tag: 'th',
585                 cls : 'x-hcol-' + i,
586                 style : '',
587                 html: cm.getColumnHeader(i)
588             };
589             
590             var hh = '';
591             
592             if(typeof(config.sortable) != 'undefined' && config.sortable){
593                 c.cls = 'sortable';
594                 c.html = '<i class="glyphicon"></i>' + c.html;
595             }
596             
597             // could use BS4 hidden-..-down 
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                     // BS 4 '0' is treated as hide that column and below.
660                     c.cls += ' hidden-' + size + ' hidden' + size + '-down';
661                     return;
662                 }
663                 
664                 c.cls += ' col-' + size + '-' + config[size] + (
665                     size == 'xs' ? (' col-' + config[size] ) : '' // bs4 col-{num} replaces col-xs
666                 );
667                 
668                 
669             });
670             
671             header.cn.push(c)
672         }
673         
674         return header;
675     },
676     
677     renderBody : function()
678     {
679         var body = {
680             tag: 'tbody',
681             cn : [
682                 {
683                     tag: 'tr',
684                     cn : [
685                         {
686                             tag : 'td',
687                             colspan :  this.cm.getColumnCount()
688                         }
689                     ]
690                 }
691             ]
692         };
693         
694         return body;
695     },
696     
697     renderFooter : function()
698     {
699         var footer = {
700             tag: 'tfoot',
701             cn : [
702                 {
703                     tag: 'tr',
704                     cn : [
705                         {
706                             tag : 'td',
707                             colspan :  this.cm.getColumnCount()
708                         }
709                     ]
710                 }
711             ]
712         };
713         
714         return footer;
715     },
716     
717     
718     
719     onLoad : function()
720     {
721 //        Roo.log('ds onload');
722         this.clear();
723         
724         var _this = this;
725         var cm = this.cm;
726         var ds = this.store;
727         
728         Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
729             e.select('i', true).removeClass(['glyphicon-arrow-up', 'glyphicon-arrow-down']);
730             if (_this.store.sortInfo) {
731                     
732                 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
733                     e.select('i', true).addClass(['glyphicon-arrow-up']);
734                 }
735                 
736                 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
737                     e.select('i', true).addClass(['glyphicon-arrow-down']);
738                 }
739             }
740         });
741         
742         var tbody =  this.mainBody;
743               
744         if(ds.getCount() > 0){
745             ds.data.each(function(d,rowIndex){
746                 var row =  this.renderRow(cm, ds, rowIndex);
747                 
748                 tbody.createChild(row);
749                 
750                 var _this = this;
751                 
752                 if(row.cellObjects.length){
753                     Roo.each(row.cellObjects, function(r){
754                         _this.renderCellObject(r);
755                     })
756                 }
757                 
758             }, this);
759         }
760         
761         var tfoot = this.el.select('tfoot', true).first();
762         
763         if(this.footerShow && this.auto_hide_footer && this.mainFoot){
764             
765             this.mainFoot.setVisibilityMode(Roo.Element.DISPLAY).hide();
766             
767             var total = this.ds.getTotalCount();
768             
769             if(this.footer.pageSize < total){
770                 this.mainFoot.show();
771             }
772         }
773         
774         Roo.each(this.el.select('tbody td', true).elements, function(e){
775             e.on('mouseover', _this.onMouseover, _this);
776         });
777         
778         Roo.each(this.el.select('tbody td', true).elements, function(e){
779             e.on('mouseout', _this.onMouseout, _this);
780         });
781         this.fireEvent('rowsrendered', this);
782         
783         this.autoSize();
784     },
785     
786     
787     onUpdate : function(ds,record)
788     {
789         this.refreshRow(record);
790         this.autoSize();
791     },
792     
793     onRemove : function(ds, record, index, isUpdate){
794         if(isUpdate !== true){
795             this.fireEvent("beforerowremoved", this, index, record);
796         }
797         var bt = this.mainBody.dom;
798         
799         var rows = this.el.select('tbody > tr', true).elements;
800         
801         if(typeof(rows[index]) != 'undefined'){
802             bt.removeChild(rows[index].dom);
803         }
804         
805 //        if(bt.rows[index]){
806 //            bt.removeChild(bt.rows[index]);
807 //        }
808         
809         if(isUpdate !== true){
810             //this.stripeRows(index);
811             //this.syncRowHeights(index, index);
812             //this.layout();
813             this.fireEvent("rowremoved", this, index, record);
814         }
815     },
816     
817     onAdd : function(ds, records, rowIndex)
818     {
819         //Roo.log('on Add called');
820         // - note this does not handle multiple adding very well..
821         var bt = this.mainBody.dom;
822         for (var i =0 ; i < records.length;i++) {
823             //Roo.log('call insert row Add called on ' + rowIndex + ':' + i);
824             //Roo.log(records[i]);
825             //Roo.log(this.store.getAt(rowIndex+i));
826             this.insertRow(this.store, rowIndex + i, false);
827             return;
828         }
829         
830     },
831     
832     
833     refreshRow : function(record){
834         var ds = this.store, index;
835         if(typeof record == 'number'){
836             index = record;
837             record = ds.getAt(index);
838         }else{
839             index = ds.indexOf(record);
840             if (index < 0) {
841                 return; // should not happen - but seems to 
842             }
843         }
844         this.insertRow(ds, index, true);
845         this.autoSize();
846         this.onRemove(ds, record, index+1, true);
847         this.autoSize();
848         //this.syncRowHeights(index, index);
849         //this.layout();
850         this.fireEvent("rowupdated", this, index, record);
851     },
852     
853     insertRow : function(dm, rowIndex, isUpdate){
854         
855         if(!isUpdate){
856             this.fireEvent("beforerowsinserted", this, rowIndex);
857         }
858             //var s = this.getScrollState();
859         var row = this.renderRow(this.cm, this.store, rowIndex);
860         // insert before rowIndex..
861         var e = this.mainBody.createChild(row,this.getRowDom(rowIndex));
862         
863         var _this = this;
864                 
865         if(row.cellObjects.length){
866             Roo.each(row.cellObjects, function(r){
867                 _this.renderCellObject(r);
868             })
869         }
870             
871         if(!isUpdate){
872             this.fireEvent("rowsinserted", this, rowIndex);
873             //this.syncRowHeights(firstRow, lastRow);
874             //this.stripeRows(firstRow);
875             //this.layout();
876         }
877         
878     },
879     
880     
881     getRowDom : function(rowIndex)
882     {
883         var rows = this.el.select('tbody > tr', true).elements;
884         
885         return (typeof(rows[rowIndex]) == 'undefined') ? false : rows[rowIndex];
886         
887     },
888     // returns the object tree for a tr..
889   
890     
891     renderRow : function(cm, ds, rowIndex) 
892     {
893         var d = ds.getAt(rowIndex);
894         
895         var row = {
896             tag : 'tr',
897             cls : 'x-row-' + rowIndex,
898             cn : []
899         };
900             
901         var cellObjects = [];
902         
903         for(var i = 0, len = cm.getColumnCount(); i < len; i++){
904             var config = cm.config[i];
905             
906             var renderer = cm.getRenderer(i);
907             var value = '';
908             var id = false;
909             
910             if(typeof(renderer) !== 'undefined'){
911                 value = renderer(d.data[cm.getDataIndex(i)], false, d);
912             }
913             // if object are returned, then they are expected to be Roo.bootstrap.Component instances
914             // and are rendered into the cells after the row is rendered - using the id for the element.
915             
916             if(typeof(value) === 'object'){
917                 id = Roo.id();
918                 cellObjects.push({
919                     container : id,
920                     cfg : value 
921                 })
922             }
923             
924             var rowcfg = {
925                 record: d,
926                 rowIndex : rowIndex,
927                 colIndex : i,
928                 rowClass : ''
929             };
930
931             this.fireEvent('rowclass', this, rowcfg);
932             
933             var td = {
934                 tag: 'td',
935                 cls : rowcfg.rowClass + ' x-col-' + i,
936                 style: '',
937                 html: (typeof(value) === 'object') ? '' : value
938             };
939             
940             if (id) {
941                 td.id = id;
942             }
943             
944             if(typeof(config.colspan) != 'undefined'){
945                 td.colspan = config.colspan;
946             }
947             
948             if(typeof(config.hidden) != 'undefined' && config.hidden){
949                 td.style += ' display:none;';
950             }
951             
952             if(typeof(config.align) != 'undefined' && config.align.length){
953                 td.style += ' text-align:' + config.align + ';';
954             }
955             if(typeof(config.valign) != 'undefined' && config.valign.length){
956                 td.style += ' vertical-align:' + config.valign + ';';
957             }
958             
959             if(typeof(config.width) != 'undefined'){
960                 td.style += ' width:' +  config.width + 'px;';
961             }
962             
963             if(typeof(config.cursor) != 'undefined'){
964                 td.style += ' cursor:' +  config.cursor + ';';
965             }
966             
967             if(typeof(config.cls) != 'undefined'){
968                 td.cls = (typeof(td.cls) == 'undefined') ? config.cls : (td.cls + ' ' + config.cls);
969             }
970             
971             ['xs','sm','md','lg'].map(function(size){
972                 
973                 if(typeof(config[size]) == 'undefined'){
974                     return;
975                 }
976                 
977                 
978                   
979                 if (!config[size]) { // 0 = hidden
980                     // BS 4 '0' is treated as hide that column and below.
981                     td.cls += ' hidden-' + size + ' hidden' + size + '-down';
982                     return;
983                 }
984                 
985                 td.cls += ' col-' + size + '-' + config[size] + (
986                     size == 'xs' ? (' col-' +   config[size] ) : '' // bs4 col-{num} replaces col-xs
987                 );
988                  
989
990             });
991             
992             row.cn.push(td);
993            
994         }
995         
996         row.cellObjects = cellObjects;
997         
998         return row;
999           
1000     },
1001     
1002     
1003     
1004     onBeforeLoad : function()
1005     {
1006         
1007     },
1008      /**
1009      * Remove all rows
1010      */
1011     clear : function()
1012     {
1013         this.el.select('tbody', true).first().dom.innerHTML = '';
1014     },
1015     /**
1016      * Show or hide a row.
1017      * @param {Number} rowIndex to show or hide
1018      * @param {Boolean} state hide
1019      */
1020     setRowVisibility : function(rowIndex, state)
1021     {
1022         var bt = this.mainBody.dom;
1023         
1024         var rows = this.el.select('tbody > tr', true).elements;
1025         
1026         if(typeof(rows[rowIndex]) == 'undefined'){
1027             return;
1028         }
1029         rows[rowIndex].dom.style.display = state ? '' : 'none';
1030     },
1031     
1032     
1033     getSelectionModel : function(){
1034         if(!this.selModel){
1035             this.selModel = new Roo.bootstrap.Table.RowSelectionModel({grid: this});
1036         }
1037         return this.selModel;
1038     },
1039     /*
1040      * Render the Roo.bootstrap object from renderder
1041      */
1042     renderCellObject : function(r)
1043     {
1044         var _this = this;
1045         
1046         r.cfg.parentId = (typeof(r.container) == 'string') ? r.container : r.container.id;
1047         
1048         var t = r.cfg.render(r.container);
1049         
1050         if(r.cfg.cn){
1051             Roo.each(r.cfg.cn, function(c){
1052                 var child = {
1053                     container: t.getChildContainer(),
1054                     cfg: c
1055                 };
1056                 _this.renderCellObject(child);
1057             })
1058         }
1059     },
1060     
1061     getRowIndex : function(row)
1062     {
1063         var rowIndex = -1;
1064         
1065         Roo.each(this.el.select('tbody > tr', true).elements, function(el, index){
1066             if(el != row){
1067                 return;
1068             }
1069             
1070             rowIndex = index;
1071         });
1072         
1073         return rowIndex;
1074     },
1075      /**
1076      * Returns the grid's underlying element = used by panel.Grid
1077      * @return {Element} The element
1078      */
1079     getGridEl : function(){
1080         return this.el;
1081     },
1082      /**
1083      * Forces a resize - used by panel.Grid
1084      * @return {Element} The element
1085      */
1086     autoSize : function()
1087     {
1088         //var ctr = Roo.get(this.container.dom.parentElement);
1089         var ctr = Roo.get(this.el.dom);
1090         
1091         var thd = this.getGridEl().select('thead',true).first();
1092         var tbd = this.getGridEl().select('tbody', true).first();
1093         var tfd = this.getGridEl().select('tfoot', true).first();
1094         
1095         var cw = ctr.getWidth();
1096         this.getGridEl().select('tfoot tr, tfoot  td',true).setWidth(cw);
1097         
1098         if (tbd) {
1099             
1100             tbd.setWidth(ctr.getWidth());
1101             // if the body has a max height - and then scrolls - we should perhaps set up the height here
1102             // this needs fixing for various usage - currently only hydra job advers I think..
1103             //tdb.setHeight(
1104             //        ctr.getHeight() - ((thd ? thd.getHeight() : 0) + (tfd ? tfd.getHeight() : 0))
1105             //); 
1106             var barsize = (tbd.dom.offsetWidth - tbd.dom.clientWidth);
1107             cw -= barsize;
1108         }
1109         cw = Math.max(cw, this.totalWidth);
1110         this.getGridEl().select('tbody tr',true).setWidth(cw);
1111         
1112         // resize 'expandable coloumn?
1113         
1114         return; // we doe not have a view in this design..
1115         
1116     },
1117     onBodyScroll: function()
1118     {
1119         //Roo.log("body scrolled');" + this.mainBody.dom.scrollLeft);
1120         if(this.mainHead){
1121             this.mainHead.setStyle({
1122                 'position' : 'relative',
1123                 'left': (-1* this.mainBody.dom.scrollLeft) + 'px'
1124             });
1125         }
1126         
1127         if(this.lazyLoad){
1128             
1129             var scrollHeight = this.mainBody.dom.scrollHeight;
1130             
1131             var scrollTop = Math.ceil(this.mainBody.getScroll().top);
1132             
1133             var height = this.mainBody.getHeight();
1134             
1135             if(scrollHeight - height == scrollTop) {
1136                 
1137                 var total = this.ds.getTotalCount();
1138                 
1139                 if(this.footer.cursor + this.footer.pageSize < total){
1140                     
1141                     this.footer.ds.load({
1142                         params : {
1143                             start : this.footer.cursor + this.footer.pageSize,
1144                             limit : this.footer.pageSize
1145                         },
1146                         add : true
1147                     });
1148                 }
1149             }
1150             
1151         }
1152     },
1153     
1154     onHeaderChange : function()
1155     {
1156         var header = this.renderHeader();
1157         var table = this.el.select('table', true).first();
1158         
1159         this.mainHead.remove();
1160         this.mainHead = table.createChild(header, this.mainBody, false);
1161     },
1162     
1163     onHiddenChange : function(colModel, colIndex, hidden)
1164     {
1165         var thSelector = '#' + this.id + ' .x-hcol-' + colIndex;
1166         var tdSelector = '#' + this.id + ' .x-col-' + colIndex;
1167         
1168         this.CSS.updateRule(thSelector, "display", "");
1169         this.CSS.updateRule(tdSelector, "display", "");
1170         
1171         if(hidden){
1172             this.CSS.updateRule(thSelector, "display", "none");
1173             this.CSS.updateRule(tdSelector, "display", "none");
1174         }
1175         
1176         this.onHeaderChange();
1177         this.onLoad();
1178     },
1179     
1180     setColumnWidth: function(col_index, width)
1181     {
1182         // width = "md-2 xs-2..."
1183         if(!this.colModel.config[col_index]) {
1184             return;
1185         }
1186         
1187         var w = width.split(" ");
1188         
1189         var rows = this.el.dom.getElementsByClassName("x-col-"+col_index);
1190         
1191         var h_row = this.el.dom.getElementsByClassName("x-hcol-"+col_index);
1192         
1193         
1194         for(var j = 0; j < w.length; j++) {
1195             
1196             if(!w[j]) {
1197                 continue;
1198             }
1199             
1200             var size_cls = w[j].split("-");
1201             
1202             if(!Number.isInteger(size_cls[1] * 1)) {
1203                 continue;
1204             }
1205             
1206             if(!this.colModel.config[col_index][size_cls[0]]) {
1207                 continue;
1208             }
1209             
1210             if(!h_row[0].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1211                 continue;
1212             }
1213             
1214             h_row[0].classList.replace(
1215                 "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1216                 "col-"+size_cls[0]+"-"+size_cls[1]
1217             );
1218             
1219             for(var i = 0; i < rows.length; i++) {
1220                 
1221                 var size_cls = w[j].split("-");
1222                 
1223                 if(!Number.isInteger(size_cls[1] * 1)) {
1224                     continue;
1225                 }
1226                 
1227                 if(!this.colModel.config[col_index][size_cls[0]]) {
1228                     continue;
1229                 }
1230                 
1231                 if(!rows[i].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1232                     continue;
1233                 }
1234                 
1235                 rows[i].classList.replace(
1236                     "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1237                     "col-"+size_cls[0]+"-"+size_cls[1]
1238                 );
1239             }
1240             
1241             this.colModel.config[col_index][size_cls[0]] = size_cls[1];
1242         }
1243     }
1244 });
1245
1246  
1247
1248