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