b57151e2d5b8cd77f32802b53423ab312df2381e
[roojs1] / Roo / bootstrap / Table.js
1 /**
2  * @class Roo.bootstrap.Table
3  * @licence LGBL
4  * @extends Roo.bootstrap.Component
5  * Bootstrap Table class.  This class represents the primary interface of a component based grid control.
6  * Similar to Roo.grid.Grid
7  * <pre><code>
8  var table = Roo.factory({
9     xtype : 'Table',
10     xns : Roo.bootstrap,
11     autoSizeColumns: true,
12     
13     
14     store : {
15         xtype : 'Store',
16         xns : Roo.data,
17         remoteSort : true,
18         sortInfo : { direction : 'ASC', field: 'name' },
19         proxy : {
20            xtype : 'HttpProxy',
21            xns : Roo.data,
22            method : 'GET',
23            url : 'https://example.com/some.data.url.json'
24         },
25         reader : {
26            xtype : 'JsonReader',
27            xns : Roo.data,
28            fields : [ 'id', 'name', whatever' ],
29            id : 'id',
30            root : 'data'
31         }
32     },
33     cm : [
34         {
35             xtype : 'ColumnModel',
36             xns : Roo.grid,
37             align : 'center',
38             cursor : 'pointer',
39             dataIndex : 'is_in_group',
40             header : "Name",
41             sortable : true,
42             renderer : function(v, x , r) {  
43             
44                 return String.format("{0}", v)
45             }
46             width : 3
47         } // more columns..
48     ],
49     selModel : {
50         xtype : 'RowSelectionModel',
51         xns : Roo.bootstrap.Table
52         // you can add listeners to catch selection change here....
53     }
54      
55
56  });
57  // set any options
58  grid.render(Roo.get("some-div"));
59 </code></pre>
60
61 Currently the Table  uses multiple headers to try and handle XL / Medium etc... styling
62
63
64
65  *
66  * @cfg {Roo.grid.RowSelectionModel|Roo.grid.CellSelectionModel} sm The selection model to use (cell selection is not supported yet)
67  * @cfg {Roo.data.Store|Roo.data.SimpleStore} store The data store to use
68  * @cfg {Roo.grid.ColumnModel} cm[] A column for th grid.
69  * 
70  * @cfg {String} cls table class
71  *
72  * 
73  * @cfg {boolean} striped Should the rows be alternative striped
74  * @cfg {boolean} bordered Add borders to the table
75  * @cfg {boolean} hover Add hover highlighting
76  * @cfg {boolean} condensed Format condensed
77  * @cfg {boolean} responsive Format condensed
78  * @cfg {Boolean} loadMask (true|false) default false
79  * @cfg {Boolean} footerShow (true|false) generate tfoot, default true
80  * @cfg {Boolean} headerShow (true|false) generate thead, default true
81  * @cfg {Boolean} rowSelection (true|false) default false
82  * @cfg {Boolean} cellSelection (true|false) default false
83  * @cfg {Boolean} scrollBody (true|false) default false - body scrolled / fixed header
84  * @cfg {Roo.bootstrap.PagingToolbar} footer  a paging toolbar
85  * @cfg {Boolean} lazyLoad  auto load data while scrolling to the end (default false)
86  * @cfg {Boolean} auto_hide_footer  auto hide footer if only one page (default false)
87  * @cfg {Boolean} enableColumnResize default true if columns can be resized (drag/drop)
88  * @cfg {Number} minColumnWidth default 50 pixels minimum column width 
89  * 
90  * @constructor
91  * Create a new Table
92  * @param {Object} config The config object
93  */
94
95 Roo.bootstrap.Table = function(config)
96 {
97     Roo.bootstrap.Table.superclass.constructor.call(this, config);
98      
99     // BC...
100     this.rowSelection = (typeof(config.rowSelection) != 'undefined') ? config.rowSelection : this.rowSelection;
101     this.cellSelection = (typeof(config.cellSelection) != 'undefined') ? config.cellSelection : this.cellSelection;
102     this.headerShow = (typeof(config.thead) != 'undefined') ? config.thead : this.headerShow;
103     this.footerShow = (typeof(config.tfoot) != 'undefined') ? config.tfoot : this.footerShow;
104     
105     this.view = this; // compat with grid.
106     
107     this.sm = this.sm || {xtype: 'RowSelectionModel'};
108     if (this.sm) {
109         this.sm.grid = this;
110         this.selModel = Roo.factory(this.sm, Roo.grid);
111         this.sm = this.selModel;
112         this.sm.xmodule = this.xmodule || false;
113     }
114     
115     if (this.cm && typeof(this.cm.config) == 'undefined') {
116         this.colModel = new Roo.grid.ColumnModel(this.cm);
117         this.cm = this.colModel;
118         this.cm.xmodule = this.xmodule || false;
119     }
120     if (this.store) {
121         this.store= Roo.factory(this.store, Roo.data);
122         this.ds = this.store;
123         this.ds.xmodule = this.xmodule || false;
124          
125     }
126     if (this.footer && this.store) {
127         this.footer.dataSource = this.ds;
128         this.footer = Roo.factory(this.footer);
129     }
130     
131     /** @private */
132     this.addEvents({
133         /**
134          * @event cellclick
135          * Fires when a cell is clicked
136          * @param {Roo.bootstrap.Table} this
137          * @param {Roo.Element} el
138          * @param {Number} rowIndex
139          * @param {Number} columnIndex
140          * @param {Roo.EventObject} e
141          */
142         "cellclick" : true,
143         /**
144          * @event celldblclick
145          * Fires when a cell is double clicked
146          * @param {Roo.bootstrap.Table} this
147          * @param {Roo.Element} el
148          * @param {Number} rowIndex
149          * @param {Number} columnIndex
150          * @param {Roo.EventObject} e
151          */
152         "celldblclick" : true,
153         /**
154          * @event rowclick
155          * Fires when a row is clicked
156          * @param {Roo.bootstrap.Table} this
157          * @param {Roo.Element} el
158          * @param {Number} rowIndex
159          * @param {Roo.EventObject} e
160          */
161         "rowclick" : true,
162         /**
163          * @event rowdblclick
164          * Fires when a row is double clicked
165          * @param {Roo.bootstrap.Table} this
166          * @param {Roo.Element} el
167          * @param {Number} rowIndex
168          * @param {Roo.EventObject} e
169          */
170         "rowdblclick" : true,
171         /**
172          * @event mouseover
173          * Fires when a mouseover occur
174          * @param {Roo.bootstrap.Table} this
175          * @param {Roo.Element} el
176          * @param {Number} rowIndex
177          * @param {Number} columnIndex
178          * @param {Roo.EventObject} e
179          */
180         "mouseover" : true,
181         /**
182          * @event mouseout
183          * Fires when a mouseout occur
184          * @param {Roo.bootstrap.Table} this
185          * @param {Roo.Element} el
186          * @param {Number} rowIndex
187          * @param {Number} columnIndex
188          * @param {Roo.EventObject} e
189          */
190         "mouseout" : true,
191         /**
192          * @event rowclass
193          * Fires when a row is rendered, so you can change add a style to it.
194          * @param {Roo.bootstrap.Table} this
195          * @param {Object} rowcfg   contains record  rowIndex colIndex and rowClass - set rowClass to add a style.
196          */
197         'rowclass' : true,
198           /**
199          * @event rowsrendered
200          * Fires when all the  rows have been rendered
201          * @param {Roo.bootstrap.Table} this
202          */
203         'rowsrendered' : true,
204         /**
205          * @event contextmenu
206          * The raw contextmenu event for the entire grid.
207          * @param {Roo.EventObject} e
208          */
209         "contextmenu" : true,
210         /**
211          * @event rowcontextmenu
212          * Fires when a row is right clicked
213          * @param {Roo.bootstrap.Table} this
214          * @param {Number} rowIndex
215          * @param {Roo.EventObject} e
216          */
217         "rowcontextmenu" : true,
218         /**
219          * @event cellcontextmenu
220          * Fires when a cell is right clicked
221          * @param {Roo.bootstrap.Table} this
222          * @param {Number} rowIndex
223          * @param {Number} cellIndex
224          * @param {Roo.EventObject} e
225          */
226          "cellcontextmenu" : true,
227          /**
228          * @event headercontextmenu
229          * Fires when a header is right clicked
230          * @param {Roo.bootstrap.Table} this
231          * @param {Number} columnIndex
232          * @param {Roo.EventObject} e
233          */
234         "headercontextmenu" : true,
235         /**
236          * @event mousedown
237          * The raw mousedown event for the entire grid.
238          * @param {Roo.EventObject} e
239          */
240         "mousedown" : true
241         
242     });
243 };
244
245 Roo.extend(Roo.bootstrap.Table, Roo.bootstrap.Component,  {
246     
247     cls: false,
248     
249     striped : false,
250     scrollBody : false,
251     bordered: false,
252     hover:  false,
253     condensed : false,
254     responsive : false,
255     sm : false,
256     cm : false,
257     store : false,
258     loadMask : false,
259     footerShow : true,
260     headerShow : true,
261     enableColumnResize: true,
262   
263     rowSelection : false,
264     cellSelection : false,
265     layout : false,
266
267     minColumnWidth : 50,
268     
269     // Roo.Element - the tbody
270     bodyEl: false,  // <tbody> Roo.Element - thead element    
271     headEl: false,  // <thead> Roo.Element - thead element
272     resizeProxy : false, // proxy element for dragging?
273
274
275     
276     container: false, // used by gridpanel...
277     
278     lazyLoad : false,
279     
280     CSS : Roo.util.CSS,
281     
282     auto_hide_footer : false,
283     
284     view: false, // actually points to this..
285     
286     getAutoCreate : function()
287     {
288         var cfg = Roo.apply({}, Roo.bootstrap.Table.superclass.getAutoCreate.call(this));
289         
290         cfg = {
291             tag: 'table',
292             cls : 'table', 
293             cn : []
294         };
295         // this get's auto added by panel.Grid
296         if (this.scrollBody) {
297             cfg.cls += ' table-body-fixed';
298         }    
299         if (this.striped) {
300             cfg.cls += ' table-striped';
301         }
302         
303         if (this.hover) {
304             cfg.cls += ' table-hover';
305         }
306         if (this.bordered) {
307             cfg.cls += ' table-bordered';
308         }
309         if (this.condensed) {
310             cfg.cls += ' table-condensed';
311         }
312         
313         if (this.responsive) {
314             cfg.cls += ' table-responsive';
315         }
316         
317         if (this.cls) {
318             cfg.cls+=  ' ' +this.cls;
319         }
320         
321         
322         
323         if (this.layout) {
324             cfg.style = (typeof(cfg.style) == 'undefined') ? ('table-layout:' + this.layout + ';') : (cfg.style + ('table-layout:' + this.layout + ';'));
325         }
326         
327         if(this.store || this.cm){
328             if(this.headerShow){
329                 cfg.cn.push(this.renderHeader());
330             }
331             
332             cfg.cn.push(this.renderBody());
333             
334             if(this.footerShow){
335                 cfg.cn.push(this.renderFooter());
336             }
337             // where does this come from?
338             //cfg.cls+=  ' TableGrid';
339         }
340         
341         return { cn : [ cfg ] };
342     },
343     
344     initEvents : function()
345     {   
346         if(!this.store || !this.cm){
347             return;
348         }
349         if (this.selModel) {
350             this.selModel.initEvents();
351         }
352         
353         
354         //Roo.log('initEvents with ds!!!!');
355         
356         this.bodyEl = this.el.select('tbody', true).first();
357         this.headEl = this.el.select('thead', true).first();
358         this.mainFoot = this.el.select('tfoot', true).first();
359         
360         
361         
362         
363         Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
364             e.on('click', this.sort, this);
365         }, this);
366         
367         
368         // why is this done????? = it breaks dialogs??
369         //this.parent().el.setStyle('position', 'relative');
370         
371         
372         if (this.footer) {
373             this.footer.parentId = this.id;
374             this.footer.onRender(this.el.select('tfoot tr td').first(), null);
375             
376             if(this.lazyLoad){
377                 this.el.select('tfoot tr td').first().addClass('hide');
378             }
379         } 
380         
381         if(this.loadMask) {
382             this.maskEl = new Roo.LoadMask(this.el, { store : this.ds, msgCls: 'roo-el-mask-msg' });
383         }
384         
385         this.store.on('load', this.onLoad, this);
386         this.store.on('beforeload', this.onBeforeLoad, this);
387         this.store.on('update', this.onUpdate, this);
388         this.store.on('add', this.onAdd, this);
389         this.store.on("clear", this.clear, this);
390         
391         this.el.on("contextmenu", this.onContextMenu, this);
392         
393         
394         this.cm.on("headerchange", this.onHeaderChange, this);
395         this.cm.on("hiddenchange", this.onHiddenChange, this, arguments);
396
397  //?? does bodyEl get replaced on render?
398         this.bodyEl.on("click", this.onClick, this);
399         this.bodyEl.on("dblclick", this.onDblClick, this);        
400         this.bodyEl.on('scroll', this.onBodyScroll, this);
401
402         // guessing mainbody will work - this relays usually caught by selmodel at present.
403         this.relayEvents(this.bodyEl, ["mousedown","mouseup","mouseover","mouseout","keypress"]);
404   
405   
406         this.resizeProxy = Roo.get(document.body).createChild({ cls:"x-grid-resize-proxy", html: '&#160;' });
407         
408   
409         if(this.headEl && this.enableColumnResize !== false && Roo.grid.SplitDragZone){
410             new Roo.grid.SplitDragZone(this, this.headEl.dom, false); // not sure what 'lockedHd is for this implementation..)
411         }
412         
413         this.initCSS();
414     },
415     // Compatibility with grid - we implement all the view features at present.
416     getView : function()
417     {
418         return this;
419     },
420     
421     initCSS : function()
422     {
423         
424         
425         var cm = this.cm, styles = [];
426         this.CSS.removeStyleSheet(this.id + '-cssrules');
427         
428         // we can honour xs/sm/md/xl  as widths...
429         // we first have to decide what widht we are currently at...
430         var sz = Roo.getGridSize();
431         
432         var total = 0;
433         var last = -1;
434         var cols = []; // visable cols.
435         for(var i = 0, len = cm.getColumnCount(); i < len; i++) {
436             var w = cm.getColumnWidth(i, sz);
437             if (w > 0) {
438                 last = i
439             }
440             total += w;
441             cols.push(w);
442         }
443         
444         
445         var unitWidth = Math.floor(this.bodyEl.dom.clientWidth / total);
446         var rem = this.bodyEl.dom.clientWidth - (unitWidth * total);
447         
448         var hidden, width;
449         for(var i = 0, len = cm.getColumnCount(); i < len; i++) {
450             
451             var hidden = 'display:none;';
452             var width  = 'width:0px';
453             if(!cm.isHidden(i)){
454                 hidden = '';
455                 
456                 // we can honour xs/sm/md/xl ?
457                 var w = cols[i] * unitWidth;
458                 if (w===0) {
459                     hidden = 'display:none;';
460                 }
461                 // width should return a small number...
462                 if (i == last) {
463                     w+=rem; // add the remaining with..
464                 }
465                 
466                 width = "width:" + w+ "px;";
467             }
468             
469             styles.push(
470                     '#' , this.id , ' .x-col-' , i, " {\n", cm.config[i].css, width, hidden, "\n}\n",
471                     '#' , this.id , ' .x-hcol-' , i, " {\n", width, hidden,"}\n"
472             );
473         }
474         Roo.log(styles.join(''));
475         this.CSS.createStyleSheet( styles.join(''), this.id + '-cssrules');
476         
477     },
478     
479     
480     
481     onContextMenu : function(e, t)
482     {
483         this.processEvent("contextmenu", e);
484     },
485     
486     processEvent : function(name, e)
487     {
488         if (name != 'touchstart' ) {
489             this.fireEvent(name, e);    
490         }
491         
492         var t = e.getTarget();
493         
494         var cell = Roo.get(t);
495         
496         if(!cell){
497             return;
498         }
499         
500         if(cell.findParent('tfoot', false, true)){
501             return;
502         }
503         
504         if(cell.findParent('thead', false, true)){
505             
506             if(e.getTarget().nodeName.toLowerCase() != 'th'){
507                 cell = Roo.get(t).findParent('th', false, true);
508                 if (!cell) {
509                     Roo.log("failed to find th in thead?");
510                     Roo.log(e.getTarget());
511                     return;
512                 }
513             }
514             
515             var cellIndex = cell.dom.cellIndex;
516             
517             var ename = name == 'touchstart' ? 'click' : name;
518             this.fireEvent("header" + ename, this, cellIndex, e);
519             
520             return;
521         }
522         
523         if(e.getTarget().nodeName.toLowerCase() != 'td'){
524             cell = Roo.get(t).findParent('td', false, true);
525             if (!cell) {
526                 Roo.log("failed to find th in tbody?");
527                 Roo.log(e.getTarget());
528                 return;
529             }
530         }
531         
532         var row = cell.findParent('tr', false, true);
533         var cellIndex = cell.dom.cellIndex;
534         var rowIndex = row.dom.rowIndex - 1;
535         
536         if(row !== false){
537             
538             this.fireEvent("row" + name, this, rowIndex, e);
539             
540             if(cell !== false){
541             
542                 this.fireEvent("cell" + name, this, rowIndex, cellIndex, e);
543             }
544         }
545         
546     },
547     
548     onMouseover : function(e, el)
549     {
550         var cell = Roo.get(el);
551         
552         if(!cell){
553             return;
554         }
555         
556         if(e.getTarget().nodeName.toLowerCase() != 'td'){
557             cell = cell.findParent('td', false, true);
558         }
559         
560         var row = cell.findParent('tr', false, true);
561         var cellIndex = cell.dom.cellIndex;
562         var rowIndex = row.dom.rowIndex - 1; // start from 0
563         
564         this.fireEvent('mouseover', this, cell, rowIndex, cellIndex, e);
565         
566     },
567     
568     onMouseout : function(e, el)
569     {
570         var cell = Roo.get(el);
571         
572         if(!cell){
573             return;
574         }
575         
576         if(e.getTarget().nodeName.toLowerCase() != 'td'){
577             cell = cell.findParent('td', false, true);
578         }
579         
580         var row = cell.findParent('tr', false, true);
581         var cellIndex = cell.dom.cellIndex;
582         var rowIndex = row.dom.rowIndex - 1; // start from 0
583         
584         this.fireEvent('mouseout', this, cell, rowIndex, cellIndex, e);
585         
586     },
587     
588     onClick : function(e, el)
589     {
590         var cell = Roo.get(el);
591         
592         if(!cell || (!this.cellSelection && !this.rowSelection)){
593             return;
594         }
595         
596         if(e.getTarget().nodeName.toLowerCase() != 'td'){
597             cell = cell.findParent('td', false, true);
598         }
599         
600         if(!cell || typeof(cell) == 'undefined'){
601             return;
602         }
603         
604         var row = cell.findParent('tr', false, true);
605         
606         if(!row || typeof(row) == 'undefined'){
607             return;
608         }
609         
610         var cellIndex = cell.dom.cellIndex;
611         var rowIndex = this.getRowIndex(row);
612         
613         // why??? - should these not be based on SelectionModel?
614         //if(this.cellSelection){
615             this.fireEvent('cellclick', this, cell, rowIndex, cellIndex, e);
616         //}
617         
618         //if(this.rowSelection){
619             this.fireEvent('rowclick', this, row, rowIndex, e);
620         //}
621          
622     },
623         
624     onDblClick : function(e,el)
625     {
626         var cell = Roo.get(el);
627         
628         if(!cell || (!this.cellSelection && !this.rowSelection)){
629             return;
630         }
631         
632         if(e.getTarget().nodeName.toLowerCase() != 'td'){
633             cell = cell.findParent('td', false, true);
634         }
635         
636         if(!cell || typeof(cell) == 'undefined'){
637             return;
638         }
639         
640         var row = cell.findParent('tr', false, true);
641         
642         if(!row || typeof(row) == 'undefined'){
643             return;
644         }
645         
646         var cellIndex = cell.dom.cellIndex;
647         var rowIndex = this.getRowIndex(row);
648         
649         if(this.cellSelection){
650             this.fireEvent('celldblclick', this, cell, rowIndex, cellIndex, e);
651         }
652         
653         if(this.rowSelection){
654             this.fireEvent('rowdblclick', this, row, rowIndex, e);
655         }
656     },
657     findRowIndex : function(el)
658     {
659         var cell = Roo.get(el);
660         if(!cell) {
661             return false;
662         }
663         var row = cell.findParent('tr', false, true);
664         
665         if(!row || typeof(row) == 'undefined'){
666             return false;
667         }
668         return this.getRowIndex(row);
669     },
670     sort : function(e,el)
671     {
672         var col = Roo.get(el);
673         
674         if(!col.hasClass('sortable')){
675             return;
676         }
677         
678         var sort = col.attr('sort');
679         var dir = 'ASC';
680         
681         if(col.select('i', true).first().hasClass('fa-arrow-up')){
682             dir = 'DESC';
683         }
684         
685         this.store.sortInfo = {field : sort, direction : dir};
686         
687         if (this.footer) {
688             Roo.log("calling footer first");
689             this.footer.onClick('first');
690         } else {
691         
692             this.store.load({ params : { start : 0 } });
693         }
694     },
695     
696     renderHeader : function()
697     {
698         var header = {
699             tag: 'thead',
700             cn : []
701         };
702         
703         var cm = this.cm;
704         this.totalWidth = 0;
705         
706         for(var i = 0, len = cm.getColumnCount(); i < len; i++){
707             
708             var config = cm.config[i];
709             
710             var c = {
711                 tag: 'th',
712                 cls : 'x-hcol-' + i,
713                 style : '',
714                 
715                 html: cm.getColumnHeader(i)
716             };
717             
718             var tooltip = cm.getColumnTooltip(i);
719             if (tooltip) {
720                 c.tooltip = tooltip;
721             }
722             
723             
724             var hh = '';
725             
726             if(typeof(config.sortable) != 'undefined' && config.sortable){
727                 c.cls += ' sortable';
728                 c.html = '<i class="fa"></i>' + c.html;
729             }
730             
731             // could use BS4 hidden-..-down 
732             
733             if(typeof(config.lgHeader) != 'undefined'){
734                 hh += '<span class="hidden-xs hidden-sm hidden-md ">' + config.lgHeader + '</span>';
735             }
736             
737             if(typeof(config.mdHeader) != 'undefined'){
738                 hh += '<span class="hidden-xs hidden-sm hidden-lg">' + config.mdHeader + '</span>';
739             }
740             
741             if(typeof(config.smHeader) != 'undefined'){
742                 hh += '<span class="hidden-xs hidden-md hidden-lg">' + config.smHeader + '</span>';
743             }
744             
745             if(typeof(config.xsHeader) != 'undefined'){
746                 hh += '<span class="hidden-sm hidden-md hidden-lg">' + config.xsHeader + '</span>';
747             }
748             
749             if(hh.length){
750                 c.html = hh;
751             }
752             
753             if(typeof(config.tooltip) != 'undefined'){
754                 c.tooltip = config.tooltip;
755             }
756             
757             if(typeof(config.colspan) != 'undefined'){
758                 c.colspan = config.colspan;
759             }
760             
761             // hidden is handled by CSS now
762             
763             if(typeof(config.dataIndex) != 'undefined'){
764                 c.sort = config.dataIndex;
765             }
766             
767            
768             
769             if(typeof(config.align) != 'undefined' && config.align.length){
770                 c.style += ' text-align:' + config.align + ';';
771             }
772             
773             if(typeof(config.width) != 'undefined'){
774                 c.style += ' width:' + config.width + 'px;';
775                 this.totalWidth += config.width;
776             } else {
777                 this.totalWidth += 100; // assume minimum of 100 per column?
778             }
779             
780             if(typeof(config.cls) != 'undefined'){
781                 c.cls = (typeof(c.cls) == 'undefined') ? config.cls : (c.cls + ' ' + config.cls);
782             }
783             // this is the bit that doesnt reall work at all...
784             
785            /*
786             
787             ['xs','sm','md','lg'].map(function(size){
788                 
789                 if(typeof(config[size]) == 'undefined'){
790                     return;
791                 }
792                  
793                 if (!config[size]) { // 0 = hidden
794                     // BS 4 '0' is treated as hide that column and below.
795                     c.cls += ' hidden-' + size + ' hidden' + size + '-down';
796                     return;
797                 }
798                 
799                 c.cls += ' col-' + size + '-' + config[size] + (
800                     size == 'xs' ? (' col-' + config[size] ) : '' // bs4 col-{num} replaces col-xs
801                 );
802                 
803                 
804             });
805             */
806             // at the end?
807             
808             c.html +=' <span class="x-grid-split x-grid-split-' + i + '"></span>';
809             
810             
811             
812             
813             header.cn.push(c)
814         }
815         
816         return header;
817     },
818     
819     renderBody : function()
820     {
821         var body = {
822             tag: 'tbody',
823             cn : [
824                 {
825                     tag: 'tr',
826                     cn : [
827                         {
828                             tag : 'td',
829                             colspan :  this.cm.getColumnCount()
830                         }
831                     ]
832                 }
833             ]
834         };
835         
836         return body;
837     },
838     
839     renderFooter : function()
840     {
841         var footer = {
842             tag: 'tfoot',
843             cn : [
844                 {
845                     tag: 'tr',
846                     cn : [
847                         {
848                             tag : 'td',
849                             colspan :  this.cm.getColumnCount()
850                         }
851                     ]
852                 }
853             ]
854         };
855         
856         return footer;
857     },
858     
859     
860     
861     onLoad : function()
862     {
863 //        Roo.log('ds onload');
864         this.clear();
865         
866         var _this = this;
867         var cm = this.cm;
868         var ds = this.store;
869         
870         Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
871             e.select('i', true).removeClass(['fa-arrow-up', 'fa-arrow-down']);
872             if (_this.store.sortInfo) {
873                     
874                 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
875                     e.select('i', true).addClass(['fa-arrow-up']);
876                 }
877                 
878                 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
879                     e.select('i', true).addClass(['fa-arrow-down']);
880                 }
881             }
882         });
883         
884         var tbody =  this.bodyEl;
885               
886         if(ds.getCount() > 0){
887             ds.data.each(function(d,rowIndex){
888                 var row =  this.renderRow(cm, ds, rowIndex);
889                 
890                 tbody.createChild(row);
891                 
892                 var _this = this;
893                 
894                 if(row.cellObjects.length){
895                     Roo.each(row.cellObjects, function(r){
896                         _this.renderCellObject(r);
897                     })
898                 }
899                 
900             }, this);
901         }
902         
903         var tfoot = this.el.select('tfoot', true).first();
904         
905         if(this.footerShow && this.auto_hide_footer && this.mainFoot){
906             
907             this.mainFoot.setVisibilityMode(Roo.Element.DISPLAY).hide();
908             
909             var total = this.ds.getTotalCount();
910             
911             if(this.footer.pageSize < total){
912                 this.mainFoot.show();
913             }
914         }
915         
916         Roo.each(this.el.select('tbody td', true).elements, function(e){
917             e.on('mouseover', _this.onMouseover, _this);
918         });
919         
920         Roo.each(this.el.select('tbody td', true).elements, function(e){
921             e.on('mouseout', _this.onMouseout, _this);
922         });
923         this.fireEvent('rowsrendered', this);
924         
925         this.autoSize();
926     },
927     
928     
929     onUpdate : function(ds,record)
930     {
931         this.refreshRow(record);
932         this.autoSize();
933     },
934     
935     onRemove : function(ds, record, index, isUpdate){
936         if(isUpdate !== true){
937             this.fireEvent("beforerowremoved", this, index, record);
938         }
939         var bt = this.bodyEl.dom;
940         
941         var rows = this.el.select('tbody > tr', true).elements;
942         
943         if(typeof(rows[index]) != 'undefined'){
944             bt.removeChild(rows[index].dom);
945         }
946         
947 //        if(bt.rows[index]){
948 //            bt.removeChild(bt.rows[index]);
949 //        }
950         
951         if(isUpdate !== true){
952             //this.stripeRows(index);
953             //this.syncRowHeights(index, index);
954             //this.layout();
955             this.fireEvent("rowremoved", this, index, record);
956         }
957     },
958     
959     onAdd : function(ds, records, rowIndex)
960     {
961         //Roo.log('on Add called');
962         // - note this does not handle multiple adding very well..
963         var bt = this.bodyEl.dom;
964         for (var i =0 ; i < records.length;i++) {
965             //Roo.log('call insert row Add called on ' + rowIndex + ':' + i);
966             //Roo.log(records[i]);
967             //Roo.log(this.store.getAt(rowIndex+i));
968             this.insertRow(this.store, rowIndex + i, false);
969             return;
970         }
971         
972     },
973     
974     
975     refreshRow : function(record){
976         var ds = this.store, index;
977         if(typeof record == 'number'){
978             index = record;
979             record = ds.getAt(index);
980         }else{
981             index = ds.indexOf(record);
982             if (index < 0) {
983                 return; // should not happen - but seems to 
984             }
985         }
986         this.insertRow(ds, index, true);
987         this.autoSize();
988         this.onRemove(ds, record, index+1, true);
989         this.autoSize();
990         //this.syncRowHeights(index, index);
991         //this.layout();
992         this.fireEvent("rowupdated", this, index, record);
993     },
994     
995     onRowSelect : function(rowIndex){
996         var row = this.getRowDom(rowIndex);
997         row.addClass(['bg-info','info']);
998     },
999
1000     onRowDeselect : function(rowIndex){
1001         var row = this.getRowDom(rowIndex);
1002         row.removeClass(['bg-info','info']);
1003     },
1004       /**
1005      * Focuses the specified row.
1006      * @param {Number} row The row index
1007      */
1008     focusRow : function(row)
1009     {
1010         //Roo.log('GridView.focusRow');
1011         var x = this.bodyEl.dom.scrollLeft;
1012         this.focusCell(row, 0, false);
1013         this.bodyEl.dom.scrollLeft = x;
1014
1015     },
1016      /**
1017      * Focuses the specified cell.
1018      * @param {Number} row The row index
1019      * @param {Number} col The column index
1020      * @param {Boolean} hscroll false to disable horizontal scrolling
1021      */
1022     focusCell : function(row, col, hscroll)
1023     {
1024         //Roo.log('GridView.focusCell');
1025         var el = this.ensureVisible(row, col, hscroll);
1026         // not sure what focusEL achives = it's a <a> pos relative 
1027         //this.focusEl.alignTo(el, "tl-tl");
1028         //if(Roo.isGecko){
1029         //    this.focusEl.focus();
1030         //}else{
1031         //    this.focusEl.focus.defer(1, this.focusEl);
1032         //}
1033     },
1034     
1035      /**
1036      * Scrolls the specified cell into view
1037      * @param {Number} row The row index
1038      * @param {Number} col The column index
1039      * @param {Boolean} hscroll false to disable horizontal scrolling
1040      */
1041     ensureVisible : function(row, col, hscroll)
1042     {
1043         //Roo.log('GridView.ensureVisible,' + row + ',' + col);
1044         //return null; //disable for testing.
1045         if(typeof row != "number"){
1046             row = row.rowIndex;
1047         }
1048         if(row < 0 && row >= this.ds.getCount()){
1049             return  null;
1050         }
1051         col = (col !== undefined ? col : 0);
1052         var cm = this.cm;
1053         while(cm.isHidden(col)){
1054             col++;
1055         }
1056
1057         var el = this.getCellDom(row, col);
1058         if(!el){
1059             return null;
1060         }
1061         var c = this.bodyEl.dom;
1062
1063         var ctop = parseInt(el.offsetTop, 10);
1064         var cleft = parseInt(el.offsetLeft, 10);
1065         var cbot = ctop + el.offsetHeight;
1066         var cright = cleft + el.offsetWidth;
1067
1068         //var ch = c.clientHeight - this.mainHd.dom.offsetHeight;
1069         var ch = 0; //?? header is not withing the area?
1070         var stop = parseInt(c.scrollTop, 10);
1071         var sleft = parseInt(c.scrollLeft, 10);
1072         var sbot = stop + ch;
1073         var sright = sleft + c.clientWidth;
1074         /*
1075         Roo.log('GridView.ensureVisible:' +
1076                 ' ctop:' + ctop +
1077                 ' c.clientHeight:' + c.clientHeight +
1078                 ' this.mainHd.dom.offsetHeight:' + this.mainHd.dom.offsetHeight +
1079                 ' stop:' + stop +
1080                 ' cbot:' + cbot +
1081                 ' sbot:' + sbot +
1082                 ' ch:' + ch  
1083                 );
1084         */
1085         if(ctop < stop){
1086             c.scrollTop = ctop;
1087             //Roo.log("set scrolltop to ctop DISABLE?");
1088         }else if(cbot > sbot){
1089             //Roo.log("set scrolltop to cbot-ch");
1090             c.scrollTop = cbot-ch;
1091         }
1092
1093         if(hscroll !== false){
1094             if(cleft < sleft){
1095                 c.scrollLeft = cleft;
1096             }else if(cright > sright){
1097                 c.scrollLeft = cright-c.clientWidth;
1098             }
1099         }
1100
1101         return el;
1102     },
1103     
1104     
1105     insertRow : function(dm, rowIndex, isUpdate){
1106         
1107         if(!isUpdate){
1108             this.fireEvent("beforerowsinserted", this, rowIndex);
1109         }
1110             //var s = this.getScrollState();
1111         var row = this.renderRow(this.cm, this.store, rowIndex);
1112         // insert before rowIndex..
1113         var e = this.bodyEl.createChild(row,this.getRowDom(rowIndex));
1114         
1115         var _this = this;
1116                 
1117         if(row.cellObjects.length){
1118             Roo.each(row.cellObjects, function(r){
1119                 _this.renderCellObject(r);
1120             })
1121         }
1122             
1123         if(!isUpdate){
1124             this.fireEvent("rowsinserted", this, rowIndex);
1125             //this.syncRowHeights(firstRow, lastRow);
1126             //this.stripeRows(firstRow);
1127             //this.layout();
1128         }
1129         
1130     },
1131     
1132     
1133     getRowDom : function(rowIndex)
1134     {
1135         var rows = this.el.select('tbody > tr', true).elements;
1136         
1137         return (typeof(rows[rowIndex]) == 'undefined') ? false : rows[rowIndex];
1138         
1139     },
1140     getCellDom : function(rowIndex, colIndex)
1141     {
1142         var row = this.getRowDom(rowIndex);
1143         if (row === false) {
1144             return false;
1145         }
1146         var cols = row.select('td', true).elements;
1147         return (typeof(cols[colIndex]) == 'undefined') ? false : cols[colIndex];
1148         
1149     },
1150     
1151     // returns the object tree for a tr..
1152   
1153     
1154     renderRow : function(cm, ds, rowIndex) 
1155     {
1156         var d = ds.getAt(rowIndex);
1157         
1158         var row = {
1159             tag : 'tr',
1160             cls : 'x-row-' + rowIndex,
1161             cn : []
1162         };
1163             
1164         var cellObjects = [];
1165         
1166         for(var i = 0, len = cm.getColumnCount(); i < len; i++){
1167             var config = cm.config[i];
1168             
1169             var renderer = cm.getRenderer(i);
1170             var value = '';
1171             var id = false;
1172             
1173             if(typeof(renderer) !== 'undefined'){
1174                 value = renderer(d.data[cm.getDataIndex(i)], false, d);
1175             }
1176             // if object are returned, then they are expected to be Roo.bootstrap.Component instances
1177             // and are rendered into the cells after the row is rendered - using the id for the element.
1178             
1179             if(typeof(value) === 'object'){
1180                 id = Roo.id();
1181                 cellObjects.push({
1182                     container : id,
1183                     cfg : value 
1184                 })
1185             }
1186             
1187             var rowcfg = {
1188                 record: d,
1189                 rowIndex : rowIndex,
1190                 colIndex : i,
1191                 rowClass : ''
1192             };
1193
1194             this.fireEvent('rowclass', this, rowcfg);
1195             
1196             var td = {
1197                 tag: 'td',
1198                 // this might end up displaying HTML?
1199                 // this is too messy... - better to only do it on columsn you know are going to be too long
1200                 //tooltip : (typeof(value) === 'object') ? '' : value,
1201                 cls : rowcfg.rowClass + ' x-col-' + i,
1202                 style: '',
1203                 html: (typeof(value) === 'object') ? '' : value
1204             };
1205             
1206             if (id) {
1207                 td.id = id;
1208             }
1209             
1210             if(typeof(config.colspan) != 'undefined'){
1211                 td.colspan = config.colspan;
1212             }
1213             
1214             
1215             
1216             if(typeof(config.align) != 'undefined' && config.align.length){
1217                 td.style += ' text-align:' + config.align + ';';
1218             }
1219             if(typeof(config.valign) != 'undefined' && config.valign.length){
1220                 td.style += ' vertical-align:' + config.valign + ';';
1221             }
1222             
1223             if(typeof(config.width) != 'undefined'){
1224                 td.style += ' width:' +  config.width + 'px;';
1225             }
1226             
1227             if(typeof(config.cursor) != 'undefined'){
1228                 td.style += ' cursor:' +  config.cursor + ';';
1229             }
1230             
1231             if(typeof(config.cls) != 'undefined'){
1232                 td.cls = (typeof(td.cls) == 'undefined') ? config.cls : (td.cls + ' ' + config.cls);
1233             }
1234             /*
1235             ['xs','sm','md','lg'].map(function(size){
1236                 
1237                 if(typeof(config[size]) == 'undefined'){
1238                     return;
1239                 }
1240                 
1241                 
1242                   
1243                 if (!config[size]) { // 0 = hidden
1244                     // BS 4 '0' is treated as hide that column and below.
1245                     td.cls += ' hidden-' + size + ' hidden' + size + '-down';
1246                     return;
1247                 }
1248                 
1249                 td.cls += ' col-' + size + '-' + config[size] + (
1250                     size == 'xs' ? (' col-' +   config[size] ) : '' // bs4 col-{num} replaces col-xs
1251                 );
1252                  
1253
1254             });
1255             */
1256             row.cn.push(td);
1257            
1258         }
1259         
1260         row.cellObjects = cellObjects;
1261         
1262         return row;
1263           
1264     },
1265     
1266     
1267     
1268     onBeforeLoad : function()
1269     {
1270         
1271     },
1272      /**
1273      * Remove all rows
1274      */
1275     clear : function()
1276     {
1277         this.el.select('tbody', true).first().dom.innerHTML = '';
1278     },
1279     /**
1280      * Show or hide a row.
1281      * @param {Number} rowIndex to show or hide
1282      * @param {Boolean} state hide
1283      */
1284     setRowVisibility : function(rowIndex, state)
1285     {
1286         var bt = this.bodyEl.dom;
1287         
1288         var rows = this.el.select('tbody > tr', true).elements;
1289         
1290         if(typeof(rows[rowIndex]) == 'undefined'){
1291             return;
1292         }
1293         rows[rowIndex][ state ? 'removeClass' : 'addClass']('d-none');
1294         
1295     },
1296     
1297     
1298     getSelectionModel : function(){
1299         if(!this.selModel){
1300             this.selModel = new Roo.bootstrap.Table.RowSelectionModel({grid: this});
1301         }
1302         return this.selModel;
1303     },
1304     /*
1305      * Render the Roo.bootstrap object from renderder
1306      */
1307     renderCellObject : function(r)
1308     {
1309         var _this = this;
1310         
1311         r.cfg.parentId = (typeof(r.container) == 'string') ? r.container : r.container.id;
1312         
1313         var t = r.cfg.render(r.container);
1314         
1315         if(r.cfg.cn){
1316             Roo.each(r.cfg.cn, function(c){
1317                 var child = {
1318                     container: t.getChildContainer(),
1319                     cfg: c
1320                 };
1321                 _this.renderCellObject(child);
1322             })
1323         }
1324     },
1325     /**
1326      * get the Row Index from a dom element.
1327      * @param {Roo.Element} row The row to look for
1328      * @returns {Number} the row
1329      */
1330     getRowIndex : function(row)
1331     {
1332         var rowIndex = -1;
1333         
1334         Roo.each(this.el.select('tbody > tr', true).elements, function(el, index){
1335             if(el != row){
1336                 return;
1337             }
1338             
1339             rowIndex = index;
1340         });
1341         
1342         return rowIndex;
1343     },
1344     /**
1345      * get the header TH element for columnIndex
1346      * @param {Number} columnIndex
1347      * @returns {Roo.Element}
1348      */
1349     getHeaderIndex: function(colIndex)
1350     {
1351         var cols = this.headEl.select('th', true).elements;
1352         return cols[colIndex]; 
1353     },
1354     /**
1355      * get the Column Index from a dom element. (using regex on x-hcol-{colid})
1356      * @param {domElement} cell to look for
1357      * @returns {Number} the column
1358      */
1359     getCellIndex : function(cell)
1360     {
1361         var id = String(cell.className).match(Roo.bootstrap.Table.cellRE);
1362         if(id){
1363             return parseInt(id[1], 10);
1364         }
1365         return 0;
1366     },
1367      /**
1368      * Returns the grid's underlying element = used by panel.Grid
1369      * @return {Element} The element
1370      */
1371     getGridEl : function(){
1372         return this.el;
1373     },
1374      /**
1375      * Forces a resize - used by panel.Grid
1376      * @return {Element} The element
1377      */
1378     autoSize : function()
1379     {
1380         //var ctr = Roo.get(this.container.dom.parentElement);
1381         var ctr = Roo.get(this.el.dom);
1382         
1383         var thd = this.getGridEl().select('thead',true).first();
1384         var tbd = this.getGridEl().select('tbody', true).first();
1385         var tfd = this.getGridEl().select('tfoot', true).first();
1386         
1387         var cw = ctr.getWidth();
1388         this.getGridEl().select('tfoot tr, tfoot  td',true).setWidth(cw);
1389         
1390         if (tbd) {
1391             
1392             tbd.setWidth(ctr.getWidth());
1393             // if the body has a max height - and then scrolls - we should perhaps set up the height here
1394             // this needs fixing for various usage - currently only hydra job advers I think..
1395             //tdb.setHeight(
1396             //        ctr.getHeight() - ((thd ? thd.getHeight() : 0) + (tfd ? tfd.getHeight() : 0))
1397             //); 
1398             var barsize = (tbd.dom.offsetWidth - tbd.dom.clientWidth);
1399             cw -= barsize;
1400         }
1401         cw = Math.max(cw, this.totalWidth);
1402         this.getGridEl().select('tbody tr',true).setWidth(cw);
1403         
1404         // resize 'expandable coloumn?
1405         
1406         return; // we doe not have a view in this design..
1407         
1408     },
1409     onBodyScroll: function()
1410     {
1411         //Roo.log("body scrolled');" + this.bodyEl.dom.scrollLeft);
1412         if(this.headEl){
1413             this.headEl.setStyle({
1414                 'position' : 'relative',
1415                 'left': (-1* this.bodyEl.dom.scrollLeft) + 'px'
1416             });
1417         }
1418         
1419         if(this.lazyLoad){
1420             
1421             var scrollHeight = this.bodyEl.dom.scrollHeight;
1422             
1423             var scrollTop = Math.ceil(this.bodyEl.getScroll().top);
1424             
1425             var height = this.bodyEl.getHeight();
1426             
1427             if(scrollHeight - height == scrollTop) {
1428                 
1429                 var total = this.ds.getTotalCount();
1430                 
1431                 if(this.footer.cursor + this.footer.pageSize < total){
1432                     
1433                     this.footer.ds.load({
1434                         params : {
1435                             start : this.footer.cursor + this.footer.pageSize,
1436                             limit : this.footer.pageSize
1437                         },
1438                         add : true
1439                     });
1440                 }
1441             }
1442             
1443         }
1444     },
1445     onColumnSplitterMoved : function(i, w)
1446     {
1447         this.userResized = true;
1448         
1449         var cm = this.colModel;
1450         
1451         cm.setColumnWidth(i, w, true);
1452         //var cid = cm.getColumnId(i); << not used in this version?
1453         Roo.log(['#' + this.id + ' .x-col-' + i, "width", w + "px"]);
1454         
1455         this.CSS.updateRule( '#' + this.id + ' .x-col-' + i, "width", w + "px");
1456         this.CSS.updateRule('#' + this.id + ' .x-hcol-' + i, "width", w + "px");
1457         
1458         //this.updateSplitters();
1459         //this.layout(); << ??
1460         this.fireEvent("columnresize", i, w);
1461     },
1462     onHeaderChange : function()
1463     {
1464         var header = this.renderHeader();
1465         var table = this.el.select('table', true).first();
1466         
1467         this.headEl.remove();
1468         this.headEl = table.createChild(header, this.bodyEl, false);
1469         
1470         Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
1471             e.on('click', this.sort, this);
1472         }, this);
1473         
1474         if(this.enableColumnResize !== false && Roo.grid.SplitDragZone){
1475             new Roo.grid.SplitDragZone(this, this.headEl.dom, false); // not sure what 'lockedHd is for this implementation..)
1476         }
1477         
1478     },
1479     
1480     onHiddenChange : function(colModel, colIndex, hidden)
1481     {
1482         var thSelector = '#' + this.id + ' .x-hcol-' + colIndex;
1483         var tdSelector = '#' + this.id + ' .x-col-' + colIndex;
1484         
1485         this.CSS.updateRule(thSelector, "display", "");
1486         this.CSS.updateRule(tdSelector, "display", "");
1487         
1488         if(hidden){
1489             this.CSS.updateRule(thSelector, "display", "none");
1490             this.CSS.updateRule(tdSelector, "display", "none");
1491         }
1492         
1493         this.onHeaderChange();
1494         this.onLoad();
1495     },
1496     
1497     setColumnWidth: function(col_index, width)
1498     {
1499         // width = "md-2 xs-2..."
1500         if(!this.colModel.config[col_index]) {
1501             return;
1502         }
1503         
1504         var w = width.split(" ");
1505         
1506         var rows = this.el.dom.getElementsByClassName("x-col-"+col_index);
1507         
1508         var h_row = this.el.dom.getElementsByClassName("x-hcol-"+col_index);
1509         
1510         
1511         for(var j = 0; j < w.length; j++) {
1512             
1513             if(!w[j]) {
1514                 continue;
1515             }
1516             
1517             var size_cls = w[j].split("-");
1518             
1519             if(!Number.isInteger(size_cls[1] * 1)) {
1520                 continue;
1521             }
1522             
1523             if(!this.colModel.config[col_index][size_cls[0]]) {
1524                 continue;
1525             }
1526             
1527             if(!h_row[0].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1528                 continue;
1529             }
1530             
1531             h_row[0].classList.replace(
1532                 "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1533                 "col-"+size_cls[0]+"-"+size_cls[1]
1534             );
1535             
1536             for(var i = 0; i < rows.length; i++) {
1537                 
1538                 var size_cls = w[j].split("-");
1539                 
1540                 if(!Number.isInteger(size_cls[1] * 1)) {
1541                     continue;
1542                 }
1543                 
1544                 if(!this.colModel.config[col_index][size_cls[0]]) {
1545                     continue;
1546                 }
1547                 
1548                 if(!rows[i].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1549                     continue;
1550                 }
1551                 
1552                 rows[i].classList.replace(
1553                     "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1554                     "col-"+size_cls[0]+"-"+size_cls[1]
1555                 );
1556             }
1557             
1558             this.colModel.config[col_index][size_cls[0]] = size_cls[1];
1559         }
1560     }
1561 });
1562
1563 // currently only used to find the split on drag.. 
1564 Roo.bootstrap.Table.cellRE = /(?:.*?)x-grid-(?:hd|cell|split)-([\d]+)(?:.*?)/;
1565
1566 /**
1567  * @depricated
1568 */
1569 Roo.bootstrap.Table.AbstractSelectionModel = Roo.grid.AbstractSelectionModel;
1570 Roo.bootstrap.Table.RowSelectionModel = Roo.grid.RowSelectionModel;