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