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