sync
[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         
414     },
415     // Compatibility with grid - we implement all the view features at present.
416     getView : function()
417     {
418         return this;
419     },
420     
421     onContextMenu : function(e, t)
422     {
423         this.processEvent("contextmenu", e);
424     },
425     
426     processEvent : function(name, e)
427     {
428         if (name != 'touchstart' ) {
429             this.fireEvent(name, e);    
430         }
431         
432         var t = e.getTarget();
433         
434         var cell = Roo.get(t);
435         
436         if(!cell){
437             return;
438         }
439         
440         if(cell.findParent('tfoot', false, true)){
441             return;
442         }
443         
444         if(cell.findParent('thead', false, true)){
445             
446             if(e.getTarget().nodeName.toLowerCase() != 'th'){
447                 cell = Roo.get(t).findParent('th', false, true);
448                 if (!cell) {
449                     Roo.log("failed to find th in thead?");
450                     Roo.log(e.getTarget());
451                     return;
452                 }
453             }
454             
455             var cellIndex = cell.dom.cellIndex;
456             
457             var ename = name == 'touchstart' ? 'click' : name;
458             this.fireEvent("header" + ename, this, cellIndex, e);
459             
460             return;
461         }
462         
463         if(e.getTarget().nodeName.toLowerCase() != 'td'){
464             cell = Roo.get(t).findParent('td', false, true);
465             if (!cell) {
466                 Roo.log("failed to find th in tbody?");
467                 Roo.log(e.getTarget());
468                 return;
469             }
470         }
471         
472         var row = cell.findParent('tr', false, true);
473         var cellIndex = cell.dom.cellIndex;
474         var rowIndex = row.dom.rowIndex - 1;
475         
476         if(row !== false){
477             
478             this.fireEvent("row" + name, this, rowIndex, e);
479             
480             if(cell !== false){
481             
482                 this.fireEvent("cell" + name, this, rowIndex, cellIndex, e);
483             }
484         }
485         
486     },
487     
488     onMouseover : function(e, el)
489     {
490         var cell = Roo.get(el);
491         
492         if(!cell){
493             return;
494         }
495         
496         if(e.getTarget().nodeName.toLowerCase() != 'td'){
497             cell = cell.findParent('td', false, true);
498         }
499         
500         var row = cell.findParent('tr', false, true);
501         var cellIndex = cell.dom.cellIndex;
502         var rowIndex = row.dom.rowIndex - 1; // start from 0
503         
504         this.fireEvent('mouseover', this, cell, rowIndex, cellIndex, e);
505         
506     },
507     
508     onMouseout : function(e, el)
509     {
510         var cell = Roo.get(el);
511         
512         if(!cell){
513             return;
514         }
515         
516         if(e.getTarget().nodeName.toLowerCase() != 'td'){
517             cell = cell.findParent('td', false, true);
518         }
519         
520         var row = cell.findParent('tr', false, true);
521         var cellIndex = cell.dom.cellIndex;
522         var rowIndex = row.dom.rowIndex - 1; // start from 0
523         
524         this.fireEvent('mouseout', this, cell, rowIndex, cellIndex, e);
525         
526     },
527     
528     onClick : function(e, el)
529     {
530         var cell = Roo.get(el);
531         
532         if(!cell || (!this.cellSelection && !this.rowSelection)){
533             return;
534         }
535         
536         if(e.getTarget().nodeName.toLowerCase() != 'td'){
537             cell = cell.findParent('td', false, true);
538         }
539         
540         if(!cell || typeof(cell) == 'undefined'){
541             return;
542         }
543         
544         var row = cell.findParent('tr', false, true);
545         
546         if(!row || typeof(row) == 'undefined'){
547             return;
548         }
549         
550         var cellIndex = cell.dom.cellIndex;
551         var rowIndex = this.getRowIndex(row);
552         
553         // why??? - should these not be based on SelectionModel?
554         //if(this.cellSelection){
555             this.fireEvent('cellclick', this, cell, rowIndex, cellIndex, e);
556         //}
557         
558         //if(this.rowSelection){
559             this.fireEvent('rowclick', this, row, rowIndex, e);
560         //}
561          
562     },
563         
564     onDblClick : function(e,el)
565     {
566         var cell = Roo.get(el);
567         
568         if(!cell || (!this.cellSelection && !this.rowSelection)){
569             return;
570         }
571         
572         if(e.getTarget().nodeName.toLowerCase() != 'td'){
573             cell = cell.findParent('td', false, true);
574         }
575         
576         if(!cell || typeof(cell) == 'undefined'){
577             return;
578         }
579         
580         var row = cell.findParent('tr', false, true);
581         
582         if(!row || typeof(row) == 'undefined'){
583             return;
584         }
585         
586         var cellIndex = cell.dom.cellIndex;
587         var rowIndex = this.getRowIndex(row);
588         
589         if(this.cellSelection){
590             this.fireEvent('celldblclick', this, cell, rowIndex, cellIndex, e);
591         }
592         
593         if(this.rowSelection){
594             this.fireEvent('rowdblclick', this, row, rowIndex, e);
595         }
596     },
597     findRowIndex : function(el)
598     {
599         var cell = Roo.get(el);
600         if(!cell) {
601             return false;
602         }
603         var row = cell.findParent('tr', false, true);
604         
605         if(!row || typeof(row) == 'undefined'){
606             return false;
607         }
608         return this.getRowIndex(row);
609     },
610     sort : function(e,el)
611     {
612         var col = Roo.get(el);
613         
614         if(!col.hasClass('sortable')){
615             return;
616         }
617         
618         var sort = col.attr('sort');
619         var dir = 'ASC';
620         
621         if(col.select('i', true).first().hasClass('fa-arrow-up')){
622             dir = 'DESC';
623         }
624         
625         this.store.sortInfo = {field : sort, direction : dir};
626         
627         if (this.footer) {
628             Roo.log("calling footer first");
629             this.footer.onClick('first');
630         } else {
631         
632             this.store.load({ params : { start : 0 } });
633         }
634     },
635     
636     renderHeader : function()
637     {
638         var header = {
639             tag: 'thead',
640             cn : []
641         };
642         
643         var cm = this.cm;
644         this.totalWidth = 0;
645         
646         for(var i = 0, len = cm.getColumnCount(); i < len; i++){
647             
648             var config = cm.config[i];
649             
650             var c = {
651                 tag: 'th',
652                 cls : 'x-hcol-' + i,
653                 style : '',
654                 
655                 html: cm.getColumnHeader(i)
656             };
657             
658             var tooltip = cm.getColumnTooltip(i);
659             if (tooltip) {
660                 c.tooltip = tooltip;
661             }
662             
663             
664             var hh = '';
665             
666             if(typeof(config.sortable) != 'undefined' && config.sortable){
667                 c.cls += ' sortable';
668                 c.html = '<i class="fa"></i>' + c.html;
669             }
670             
671             // could use BS4 hidden-..-down 
672             
673             if(typeof(config.lgHeader) != 'undefined'){
674                 hh += '<span class="hidden-xs hidden-sm hidden-md ">' + config.lgHeader + '</span>';
675             }
676             
677             if(typeof(config.mdHeader) != 'undefined'){
678                 hh += '<span class="hidden-xs hidden-sm hidden-lg">' + config.mdHeader + '</span>';
679             }
680             
681             if(typeof(config.smHeader) != 'undefined'){
682                 hh += '<span class="hidden-xs hidden-md hidden-lg">' + config.smHeader + '</span>';
683             }
684             
685             if(typeof(config.xsHeader) != 'undefined'){
686                 hh += '<span class="hidden-sm hidden-md hidden-lg">' + config.xsHeader + '</span>';
687             }
688             
689             if(hh.length){
690                 c.html = hh;
691             }
692             
693             if(typeof(config.tooltip) != 'undefined'){
694                 c.tooltip = config.tooltip;
695             }
696             
697             if(typeof(config.colspan) != 'undefined'){
698                 c.colspan = config.colspan;
699             }
700             
701             if(typeof(config.hidden) != 'undefined' && config.hidden){
702                 c.cls += ' d-none';
703             } else {
704                 c.cls += ' d-block';
705             }
706             
707             if(typeof(config.dataIndex) != 'undefined'){
708                 c.sort = config.dataIndex;
709             }
710             
711            
712             
713             if(typeof(config.align) != 'undefined' && config.align.length){
714                 c.style += ' text-align:' + config.align + ';';
715             }
716             
717             if(typeof(config.width) != 'undefined'){
718                 c.style += ' width:' + config.width + 'px;';
719                 this.totalWidth += config.width;
720             } else {
721                 this.totalWidth += 100; // assume minimum of 100 per column?
722             }
723             
724             if(typeof(config.cls) != 'undefined'){
725                 c.cls = (typeof(c.cls) == 'undefined') ? config.cls : (c.cls + ' ' + config.cls);
726             }
727             // this is the bit that doesnt reall work at all...
728             
729             
730             
731             ['xs','sm','md','lg'].map(function(size){
732                 
733                 if(typeof(config[size]) == 'undefined'){
734                     return;
735                 }
736                  
737                 if (!config[size]) { // 0 = hidden
738                     // BS 4 '0' is treated as hide that column and below.
739                     c.cls += ' hidden-' + size + ' hidden' + size + '-down';
740                     return;
741                 }
742                 
743                 c.cls += ' col-' + size + '-' + config[size] + (
744                     size == 'xs' ? (' col-' + config[size] ) : '' // bs4 col-{num} replaces col-xs
745                 );
746                 
747                 
748             });
749             // at the end?
750             
751             c.html +=' <span class="x-grid-split x-grid-split-' + i + '"></span>';
752             
753             
754             
755             
756             header.cn.push(c)
757         }
758         
759         return header;
760     },
761     
762     renderBody : function()
763     {
764         var body = {
765             tag: 'tbody',
766             cn : [
767                 {
768                     tag: 'tr',
769                     cn : [
770                         {
771                             tag : 'td',
772                             colspan :  this.cm.getColumnCount()
773                         }
774                     ]
775                 }
776             ]
777         };
778         
779         return body;
780     },
781     
782     renderFooter : function()
783     {
784         var footer = {
785             tag: 'tfoot',
786             cn : [
787                 {
788                     tag: 'tr',
789                     cn : [
790                         {
791                             tag : 'td',
792                             colspan :  this.cm.getColumnCount()
793                         }
794                     ]
795                 }
796             ]
797         };
798         
799         return footer;
800     },
801     
802     
803     
804     onLoad : function()
805     {
806 //        Roo.log('ds onload');
807         this.clear();
808         
809         var _this = this;
810         var cm = this.cm;
811         var ds = this.store;
812         
813         Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
814             e.select('i', true).removeClass(['fa-arrow-up', 'fa-arrow-down']);
815             if (_this.store.sortInfo) {
816                     
817                 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
818                     e.select('i', true).addClass(['fa-arrow-up']);
819                 }
820                 
821                 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
822                     e.select('i', true).addClass(['fa-arrow-down']);
823                 }
824             }
825         });
826         
827         var tbody =  this.bodyEl;
828               
829         if(ds.getCount() > 0){
830             ds.data.each(function(d,rowIndex){
831                 var row =  this.renderRow(cm, ds, rowIndex);
832                 
833                 tbody.createChild(row);
834                 
835                 var _this = this;
836                 
837                 if(row.cellObjects.length){
838                     Roo.each(row.cellObjects, function(r){
839                         _this.renderCellObject(r);
840                     })
841                 }
842                 
843             }, this);
844         }
845         
846         var tfoot = this.el.select('tfoot', true).first();
847         
848         if(this.footerShow && this.auto_hide_footer && this.mainFoot){
849             
850             this.mainFoot.setVisibilityMode(Roo.Element.DISPLAY).hide();
851             
852             var total = this.ds.getTotalCount();
853             
854             if(this.footer.pageSize < total){
855                 this.mainFoot.show();
856             }
857         }
858         
859         Roo.each(this.el.select('tbody td', true).elements, function(e){
860             e.on('mouseover', _this.onMouseover, _this);
861         });
862         
863         Roo.each(this.el.select('tbody td', true).elements, function(e){
864             e.on('mouseout', _this.onMouseout, _this);
865         });
866         this.fireEvent('rowsrendered', this);
867         
868         this.autoSize();
869     },
870     
871     
872     onUpdate : function(ds,record)
873     {
874         this.refreshRow(record);
875         this.autoSize();
876     },
877     
878     onRemove : function(ds, record, index, isUpdate){
879         if(isUpdate !== true){
880             this.fireEvent("beforerowremoved", this, index, record);
881         }
882         var bt = this.bodyEl.dom;
883         
884         var rows = this.el.select('tbody > tr', true).elements;
885         
886         if(typeof(rows[index]) != 'undefined'){
887             bt.removeChild(rows[index].dom);
888         }
889         
890 //        if(bt.rows[index]){
891 //            bt.removeChild(bt.rows[index]);
892 //        }
893         
894         if(isUpdate !== true){
895             //this.stripeRows(index);
896             //this.syncRowHeights(index, index);
897             //this.layout();
898             this.fireEvent("rowremoved", this, index, record);
899         }
900     },
901     
902     onAdd : function(ds, records, rowIndex)
903     {
904         //Roo.log('on Add called');
905         // - note this does not handle multiple adding very well..
906         var bt = this.bodyEl.dom;
907         for (var i =0 ; i < records.length;i++) {
908             //Roo.log('call insert row Add called on ' + rowIndex + ':' + i);
909             //Roo.log(records[i]);
910             //Roo.log(this.store.getAt(rowIndex+i));
911             this.insertRow(this.store, rowIndex + i, false);
912             return;
913         }
914         
915     },
916     
917     
918     refreshRow : function(record){
919         var ds = this.store, index;
920         if(typeof record == 'number'){
921             index = record;
922             record = ds.getAt(index);
923         }else{
924             index = ds.indexOf(record);
925             if (index < 0) {
926                 return; // should not happen - but seems to 
927             }
928         }
929         this.insertRow(ds, index, true);
930         this.autoSize();
931         this.onRemove(ds, record, index+1, true);
932         this.autoSize();
933         //this.syncRowHeights(index, index);
934         //this.layout();
935         this.fireEvent("rowupdated", this, index, record);
936     },
937     
938     onRowSelect : function(rowIndex){
939         var row = this.getRowDom(rowIndex);
940         row.addClass(['bg-info','info']);
941     },
942
943     onRowDeselect : function(rowIndex){
944         var row = this.getRowDom(rowIndex);
945         row.removeClass(['bg-info','info']);
946     },
947       /**
948      * Focuses the specified row.
949      * @param {Number} row The row index
950      */
951     focusRow : function(row)
952     {
953         //Roo.log('GridView.focusRow');
954         var x = this.bodyEl.dom.scrollLeft;
955         this.focusCell(row, 0, false);
956         this.bodyEl.dom.scrollLeft = x;
957
958     },
959      /**
960      * Focuses the specified cell.
961      * @param {Number} row The row index
962      * @param {Number} col The column index
963      * @param {Boolean} hscroll false to disable horizontal scrolling
964      */
965     focusCell : function(row, col, hscroll)
966     {
967         //Roo.log('GridView.focusCell');
968         var el = this.ensureVisible(row, col, hscroll);
969         // not sure what focusEL achives = it's a <a> pos relative 
970         //this.focusEl.alignTo(el, "tl-tl");
971         //if(Roo.isGecko){
972         //    this.focusEl.focus();
973         //}else{
974         //    this.focusEl.focus.defer(1, this.focusEl);
975         //}
976     },
977     
978      /**
979      * Scrolls the specified cell into view
980      * @param {Number} row The row index
981      * @param {Number} col The column index
982      * @param {Boolean} hscroll false to disable horizontal scrolling
983      */
984     ensureVisible : function(row, col, hscroll)
985     {
986         //Roo.log('GridView.ensureVisible,' + row + ',' + col);
987         //return null; //disable for testing.
988         if(typeof row != "number"){
989             row = row.rowIndex;
990         }
991         if(row < 0 && row >= this.ds.getCount()){
992             return  null;
993         }
994         col = (col !== undefined ? col : 0);
995         var cm = this.cm;
996         while(cm.isHidden(col)){
997             col++;
998         }
999
1000         var el = this.getCellDom(row, col);
1001         if(!el){
1002             return null;
1003         }
1004         var c = this.bodyEl.dom;
1005
1006         var ctop = parseInt(el.offsetTop, 10);
1007         var cleft = parseInt(el.offsetLeft, 10);
1008         var cbot = ctop + el.offsetHeight;
1009         var cright = cleft + el.offsetWidth;
1010
1011         //var ch = c.clientHeight - this.mainHd.dom.offsetHeight;
1012         var ch = 0; //?? header is not withing the area?
1013         var stop = parseInt(c.scrollTop, 10);
1014         var sleft = parseInt(c.scrollLeft, 10);
1015         var sbot = stop + ch;
1016         var sright = sleft + c.clientWidth;
1017         /*
1018         Roo.log('GridView.ensureVisible:' +
1019                 ' ctop:' + ctop +
1020                 ' c.clientHeight:' + c.clientHeight +
1021                 ' this.mainHd.dom.offsetHeight:' + this.mainHd.dom.offsetHeight +
1022                 ' stop:' + stop +
1023                 ' cbot:' + cbot +
1024                 ' sbot:' + sbot +
1025                 ' ch:' + ch  
1026                 );
1027         */
1028         if(ctop < stop){
1029             c.scrollTop = ctop;
1030             //Roo.log("set scrolltop to ctop DISABLE?");
1031         }else if(cbot > sbot){
1032             //Roo.log("set scrolltop to cbot-ch");
1033             c.scrollTop = cbot-ch;
1034         }
1035
1036         if(hscroll !== false){
1037             if(cleft < sleft){
1038                 c.scrollLeft = cleft;
1039             }else if(cright > sright){
1040                 c.scrollLeft = cright-c.clientWidth;
1041             }
1042         }
1043
1044         return el;
1045     },
1046     
1047     
1048     insertRow : function(dm, rowIndex, isUpdate){
1049         
1050         if(!isUpdate){
1051             this.fireEvent("beforerowsinserted", this, rowIndex);
1052         }
1053             //var s = this.getScrollState();
1054         var row = this.renderRow(this.cm, this.store, rowIndex);
1055         // insert before rowIndex..
1056         var e = this.bodyEl.createChild(row,this.getRowDom(rowIndex));
1057         
1058         var _this = this;
1059                 
1060         if(row.cellObjects.length){
1061             Roo.each(row.cellObjects, function(r){
1062                 _this.renderCellObject(r);
1063             })
1064         }
1065             
1066         if(!isUpdate){
1067             this.fireEvent("rowsinserted", this, rowIndex);
1068             //this.syncRowHeights(firstRow, lastRow);
1069             //this.stripeRows(firstRow);
1070             //this.layout();
1071         }
1072         
1073     },
1074     
1075     
1076     getRowDom : function(rowIndex)
1077     {
1078         var rows = this.el.select('tbody > tr', true).elements;
1079         
1080         return (typeof(rows[rowIndex]) == 'undefined') ? false : rows[rowIndex];
1081         
1082     },
1083     getCellDom : function(rowIndex, colIndex)
1084     {
1085         var row = this.getRowDom(rowIndex);
1086         if (row === false) {
1087             return false;
1088         }
1089         var cols = row.select('td', true).elements;
1090         return (typeof(cols[colIndex]) == 'undefined') ? false : cols[colIndex];
1091         
1092     },
1093     
1094     // returns the object tree for a tr..
1095   
1096     
1097     renderRow : function(cm, ds, rowIndex) 
1098     {
1099         var d = ds.getAt(rowIndex);
1100         
1101         var row = {
1102             tag : 'tr',
1103             cls : 'x-row-' + rowIndex,
1104             cn : []
1105         };
1106             
1107         var cellObjects = [];
1108         
1109         for(var i = 0, len = cm.getColumnCount(); i < len; i++){
1110             var config = cm.config[i];
1111             
1112             var renderer = cm.getRenderer(i);
1113             var value = '';
1114             var id = false;
1115             
1116             if(typeof(renderer) !== 'undefined'){
1117                 value = renderer(d.data[cm.getDataIndex(i)], false, d);
1118             }
1119             // if object are returned, then they are expected to be Roo.bootstrap.Component instances
1120             // and are rendered into the cells after the row is rendered - using the id for the element.
1121             
1122             if(typeof(value) === 'object'){
1123                 id = Roo.id();
1124                 cellObjects.push({
1125                     container : id,
1126                     cfg : value 
1127                 })
1128             }
1129             
1130             var rowcfg = {
1131                 record: d,
1132                 rowIndex : rowIndex,
1133                 colIndex : i,
1134                 rowClass : ''
1135             };
1136
1137             this.fireEvent('rowclass', this, rowcfg);
1138             
1139             var td = {
1140                 tag: 'td',
1141                 // this might end up displaying HTML?
1142                 // this is too messy... - better to only do it on columsn you know are going to be too long
1143                 //tooltip : (typeof(value) === 'object') ? '' : value,
1144                 cls : rowcfg.rowClass + ' x-col-' + i,
1145                 style: '',
1146                 html: (typeof(value) === 'object') ? '' : value
1147             };
1148             
1149             if (id) {
1150                 td.id = id;
1151             }
1152             
1153             if(typeof(config.colspan) != 'undefined'){
1154                 td.colspan = config.colspan;
1155             }
1156             
1157             if(typeof(config.hidden) != 'undefined' && config.hidden){
1158                 td.cls += ' d-none';
1159             } else {
1160                 td.cls += ' d-block';
1161             }
1162             
1163             if(typeof(config.align) != 'undefined' && config.align.length){
1164                 td.style += ' text-align:' + config.align + ';';
1165             }
1166             if(typeof(config.valign) != 'undefined' && config.valign.length){
1167                 td.style += ' vertical-align:' + config.valign + ';';
1168             }
1169             
1170             if(typeof(config.width) != 'undefined'){
1171                 td.style += ' width:' +  config.width + 'px;';
1172             }
1173             
1174             if(typeof(config.cursor) != 'undefined'){
1175                 td.style += ' cursor:' +  config.cursor + ';';
1176             }
1177             
1178             if(typeof(config.cls) != 'undefined'){
1179                 td.cls = (typeof(td.cls) == 'undefined') ? config.cls : (td.cls + ' ' + config.cls);
1180             }
1181             
1182             ['xs','sm','md','lg'].map(function(size){
1183                 
1184                 if(typeof(config[size]) == 'undefined'){
1185                     return;
1186                 }
1187                 
1188                 
1189                   
1190                 if (!config[size]) { // 0 = hidden
1191                     // BS 4 '0' is treated as hide that column and below.
1192                     td.cls += ' hidden-' + size + ' hidden' + size + '-down';
1193                     return;
1194                 }
1195                 
1196                 td.cls += ' col-' + size + '-' + config[size] + (
1197                     size == 'xs' ? (' col-' +   config[size] ) : '' // bs4 col-{num} replaces col-xs
1198                 );
1199                  
1200
1201             });
1202             
1203             row.cn.push(td);
1204            
1205         }
1206         
1207         row.cellObjects = cellObjects;
1208         
1209         return row;
1210           
1211     },
1212     
1213     
1214     
1215     onBeforeLoad : function()
1216     {
1217         
1218     },
1219      /**
1220      * Remove all rows
1221      */
1222     clear : function()
1223     {
1224         this.el.select('tbody', true).first().dom.innerHTML = '';
1225     },
1226     /**
1227      * Show or hide a row.
1228      * @param {Number} rowIndex to show or hide
1229      * @param {Boolean} state hide
1230      */
1231     setRowVisibility : function(rowIndex, state)
1232     {
1233         var bt = this.bodyEl.dom;
1234         
1235         var rows = this.el.select('tbody > tr', true).elements;
1236         
1237         if(typeof(rows[rowIndex]) == 'undefined'){
1238             return;
1239         }
1240         rows[rowIndex][ state ? 'removeClass' : 'addClass']('d-none');
1241         
1242     },
1243     
1244     
1245     getSelectionModel : function(){
1246         if(!this.selModel){
1247             this.selModel = new Roo.bootstrap.Table.RowSelectionModel({grid: this});
1248         }
1249         return this.selModel;
1250     },
1251     /*
1252      * Render the Roo.bootstrap object from renderder
1253      */
1254     renderCellObject : function(r)
1255     {
1256         var _this = this;
1257         
1258         r.cfg.parentId = (typeof(r.container) == 'string') ? r.container : r.container.id;
1259         
1260         var t = r.cfg.render(r.container);
1261         
1262         if(r.cfg.cn){
1263             Roo.each(r.cfg.cn, function(c){
1264                 var child = {
1265                     container: t.getChildContainer(),
1266                     cfg: c
1267                 };
1268                 _this.renderCellObject(child);
1269             })
1270         }
1271     },
1272     /**
1273      * get the Row Index from a dom element.
1274      * @param {Roo.Element} row The row to look for
1275      * @returns {Number} the row
1276      */
1277     getRowIndex : function(row)
1278     {
1279         var rowIndex = -1;
1280         
1281         Roo.each(this.el.select('tbody > tr', true).elements, function(el, index){
1282             if(el != row){
1283                 return;
1284             }
1285             
1286             rowIndex = index;
1287         });
1288         
1289         return rowIndex;
1290     },
1291     /**
1292      * get the header TH element for columnIndex
1293      * @param {Number} columnIndex
1294      * @returns {Roo.Element}
1295      */
1296     getHeaderIndex: function(colIndex)
1297     {
1298         var cols = this.headEl.select('th', true).elements;
1299         return cols[colIndex]; 
1300     },
1301     /**
1302      * get the Column Index from a dom element. (using regex on x-hcol-{colid})
1303      * @param {domElement} cell to look for
1304      * @returns {Number} the column
1305      */
1306     getCellIndex : function(cell)
1307     {
1308         var id = String(cell.className).match(Roo.bootstrap.Table.cellRE);
1309         if(id){
1310             return parseInt(id[1], 10);
1311         }
1312         return 0;
1313     },
1314      /**
1315      * Returns the grid's underlying element = used by panel.Grid
1316      * @return {Element} The element
1317      */
1318     getGridEl : function(){
1319         return this.el;
1320     },
1321      /**
1322      * Forces a resize - used by panel.Grid
1323      * @return {Element} The element
1324      */
1325     autoSize : function()
1326     {
1327         //var ctr = Roo.get(this.container.dom.parentElement);
1328         var ctr = Roo.get(this.el.dom);
1329         
1330         var thd = this.getGridEl().select('thead',true).first();
1331         var tbd = this.getGridEl().select('tbody', true).first();
1332         var tfd = this.getGridEl().select('tfoot', true).first();
1333         
1334         var cw = ctr.getWidth();
1335         this.getGridEl().select('tfoot tr, tfoot  td',true).setWidth(cw);
1336         
1337         if (tbd) {
1338             
1339             tbd.setWidth(ctr.getWidth());
1340             // if the body has a max height - and then scrolls - we should perhaps set up the height here
1341             // this needs fixing for various usage - currently only hydra job advers I think..
1342             //tdb.setHeight(
1343             //        ctr.getHeight() - ((thd ? thd.getHeight() : 0) + (tfd ? tfd.getHeight() : 0))
1344             //); 
1345             var barsize = (tbd.dom.offsetWidth - tbd.dom.clientWidth);
1346             cw -= barsize;
1347         }
1348         cw = Math.max(cw, this.totalWidth);
1349         this.getGridEl().select('tbody tr',true).setWidth(cw);
1350         
1351         // resize 'expandable coloumn?
1352         
1353         return; // we doe not have a view in this design..
1354         
1355     },
1356     onBodyScroll: function()
1357     {
1358         //Roo.log("body scrolled');" + this.bodyEl.dom.scrollLeft);
1359         if(this.headEl){
1360             this.headEl.setStyle({
1361                 'position' : 'relative',
1362                 'left': (-1* this.bodyEl.dom.scrollLeft) + 'px'
1363             });
1364         }
1365         
1366         if(this.lazyLoad){
1367             
1368             var scrollHeight = this.bodyEl.dom.scrollHeight;
1369             
1370             var scrollTop = Math.ceil(this.bodyEl.getScroll().top);
1371             
1372             var height = this.bodyEl.getHeight();
1373             
1374             if(scrollHeight - height == scrollTop) {
1375                 
1376                 var total = this.ds.getTotalCount();
1377                 
1378                 if(this.footer.cursor + this.footer.pageSize < total){
1379                     
1380                     this.footer.ds.load({
1381                         params : {
1382                             start : this.footer.cursor + this.footer.pageSize,
1383                             limit : this.footer.pageSize
1384                         },
1385                         add : true
1386                     });
1387                 }
1388             }
1389             
1390         }
1391     },
1392     onColumnSplitterMoved : function(i, w)
1393     {
1394         this.userResized = true;
1395         
1396         var cm = this.grid.colModel;
1397         
1398         cm.setColumnWidth(i, w, true);
1399         var cid = cm.getColumnId(i);
1400         this.css.updateRule(this.colSelector + this.idToCssName(cid), "width", (w-this.borderWidth) + "px");
1401         this.css.updateRule(this.hdSelector + this.idToCssName(cid), "width", (w-this.borderWidth) + "px");
1402         
1403         //this.updateSplitters();
1404         //this.layout(); << ??
1405         this.grid.fireEvent("columnresize", i, w);
1406     },
1407     onHeaderChange : function()
1408     {
1409         var header = this.renderHeader();
1410         var table = this.el.select('table', true).first();
1411         
1412         this.headEl.remove();
1413         this.headEl = table.createChild(header, this.bodyEl, false);
1414         
1415         Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
1416             e.on('click', this.sort, this);
1417         }, this);
1418         
1419         if(this.enableColumnResize !== false && Roo.grid.SplitDragZone){
1420             new Roo.grid.SplitDragZone(this, this.headEl.dom, false); // not sure what 'lockedHd is for this implementation..)
1421         }
1422         
1423     },
1424     
1425     onHiddenChange : function(colModel, colIndex, hidden)
1426     {
1427         var thSelector = '#' + this.id + ' .x-hcol-' + colIndex;
1428         var tdSelector = '#' + this.id + ' .x-col-' + colIndex;
1429         
1430         //this.CSS.updateRule(thSelector, "display", "");
1431         var cols = this.headEl.select('th', true).elements;
1432         if (typeof(cols[colIndex]) != 'undefined') {
1433             cols[colIndex].removeClass(['d-none', 'd-block']);
1434             cols[colIndex].addClass( hidden ? 'd-none' : 'd-block');
1435         }
1436         this.CSS.updateRule(tdSelector, "display", "");
1437         
1438         if(hidden){
1439           //  this.CSS.updateRule(thSelector, "display", "none");
1440             this.CSS.updateRule(tdSelector, "display", "none");
1441         }
1442         
1443         this.onHeaderChange();
1444         this.onLoad();
1445     },
1446     
1447     setColumnWidth: function(col_index, width)
1448     {
1449         // width = "md-2 xs-2..."
1450         if(!this.colModel.config[col_index]) {
1451             return;
1452         }
1453         
1454         var w = width.split(" ");
1455         
1456         var rows = this.el.dom.getElementsByClassName("x-col-"+col_index);
1457         
1458         var h_row = this.el.dom.getElementsByClassName("x-hcol-"+col_index);
1459         
1460         
1461         for(var j = 0; j < w.length; j++) {
1462             
1463             if(!w[j]) {
1464                 continue;
1465             }
1466             
1467             var size_cls = w[j].split("-");
1468             
1469             if(!Number.isInteger(size_cls[1] * 1)) {
1470                 continue;
1471             }
1472             
1473             if(!this.colModel.config[col_index][size_cls[0]]) {
1474                 continue;
1475             }
1476             
1477             if(!h_row[0].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1478                 continue;
1479             }
1480             
1481             h_row[0].classList.replace(
1482                 "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1483                 "col-"+size_cls[0]+"-"+size_cls[1]
1484             );
1485             
1486             for(var i = 0; i < rows.length; i++) {
1487                 
1488                 var size_cls = w[j].split("-");
1489                 
1490                 if(!Number.isInteger(size_cls[1] * 1)) {
1491                     continue;
1492                 }
1493                 
1494                 if(!this.colModel.config[col_index][size_cls[0]]) {
1495                     continue;
1496                 }
1497                 
1498                 if(!rows[i].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1499                     continue;
1500                 }
1501                 
1502                 rows[i].classList.replace(
1503                     "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1504                     "col-"+size_cls[0]+"-"+size_cls[1]
1505                 );
1506             }
1507             
1508             this.colModel.config[col_index][size_cls[0]] = size_cls[1];
1509         }
1510     }
1511 });
1512
1513 // currently only used to find the split on drag.. 
1514 Roo.bootstrap.Table.cellRE = /(?:.*?)x-grid-(?:hd|cell|split)-([\d]+)(?:.*?)/;
1515
1516 /**
1517  * @depricated
1518 */
1519 Roo.bootstrap.Table.AbstractSelectionModel = Roo.grid.AbstractSelectionModel;
1520 Roo.bootstrap.Table.RowSelectionModel = Roo.grid.RowSelectionModel;