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)
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)
42 * @param {Object} config The config object
45 Roo.bootstrap.Table = function(config){
46 Roo.bootstrap.Table.superclass.constructor.call(this, config);
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;
56 this.sm = this.sm || {xtype: 'RowSelectionModel'};
59 this.selModel = Roo.factory(this.sm, Roo.bootstrap.Table);
60 this.sm = this.selModel;
61 this.sm.xmodule = this.xmodule || false;
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;
70 this.store= Roo.factory(this.store, Roo.data);
72 this.ds.xmodule = this.xmodule || false;
75 if (this.footer && this.store) {
76 this.footer.dataSource = this.ds;
77 this.footer = Roo.factory(this.footer);
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
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
101 "celldblclick" : true,
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
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
119 "rowdblclick" : true,
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
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
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.
148 * @event rowsrendered
149 * Fires when all the rows have been rendered
150 * @param {Roo.bootstrap.Table} this
152 'rowsrendered' : true,
155 * The raw contextmenu event for the entire grid.
156 * @param {Roo.EventObject} e
158 "contextmenu" : true,
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
166 "rowcontextmenu" : true,
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
175 "cellcontextmenu" : true,
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
183 "headercontextmenu" : true
187 Roo.extend(Roo.bootstrap.Table, Roo.bootstrap.Component, {
213 rowSelection : false,
214 cellSelection : false,
217 // Roo.Element - the tbody
219 // Roo.Element - thead element
222 container: false, // used by gridpanel...
228 getAutoCreate : function()
230 var cfg = Roo.apply({}, Roo.bootstrap.Table.superclass.getAutoCreate.call(this));
237 if (this.scrollBody) {
238 cfg.cls += ' table-body-fixed';
241 cfg.cls += ' table-striped';
245 cfg.cls += ' table-hover';
248 cfg.cls += ' table-bordered';
250 if (this.condensed) {
251 cfg.cls += ' table-condensed';
253 if (this.responsive) {
254 cfg.cls += ' table-responsive';
258 cfg.cls+= ' ' +this.cls;
261 // this lot should be simplifed...
264 cfg.align=this.align;
267 cfg.bgcolor=this.bgcolor;
270 cfg.border=this.border;
272 if (this.cellpadding) {
273 cfg.cellpadding=this.cellpadding;
275 if (this.cellspacing) {
276 cfg.cellspacing=this.cellspacing;
279 cfg.frame=this.frame;
282 cfg.rules=this.rules;
285 cfg.sortable=this.sortable;
288 cfg.summary=this.summary;
291 cfg.width=this.width;
294 cfg.style = (typeof(cfg.style) == 'undefined') ? ('table-layout:' + this.layout + ';') : (cfg.style + ('table-layout:' + this.layout + ';'));
297 if(this.store || this.cm){
299 cfg.cn.push(this.renderHeader());
302 cfg.cn.push(this.renderBody());
305 cfg.cn.push(this.renderFooter());
307 // where does this come from?
308 //cfg.cls+= ' TableGrid';
311 return { cn : [ cfg ] };
314 initEvents : function()
316 if(!this.store || !this.cm){
320 this.selModel.initEvents();
324 //Roo.log('initEvents with ds!!!!');
326 this.mainBody = this.el.select('tbody', true).first();
327 this.mainHead = this.el.select('thead', true).first();
334 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
335 e.on('click', _this.sort, _this);
338 this.mainBody.on("click", this.onClick, this);
339 this.mainBody.on("dblclick", this.onDblClick, this);
341 // why is this done????? = it breaks dialogs??
342 //this.parent().el.setStyle('position', 'relative');
346 this.footer.parentId = this.id;
347 this.footer.onRender(this.el.select('tfoot tr td').first(), null);
350 this.el.select('tfoot tr td').first().addClass('hide');
354 this.maskEl = new Roo.LoadMask(this.el, { store : this.ds, msgCls: 'roo-el-mask-msg' });
356 this.store.on('load', this.onLoad, this);
357 this.store.on('beforeload', this.onBeforeLoad, this);
358 this.store.on('update', this.onUpdate, this);
359 this.store.on('add', this.onAdd, this);
360 this.store.on("clear", this.clear, this);
362 this.el.on("contextmenu", this.onContextMenu, this);
364 this.mainBody.on('scroll', this.onBodyScroll, this);
366 this.cm.on("headerchange", this.onHeaderChange, this);
368 this.cm.on("hiddenchange", this.onHiddenChange, this, arguments);
372 onContextMenu : function(e, t)
374 this.processEvent("contextmenu", e);
377 processEvent : function(name, e)
379 if (name != 'touchstart' ) {
380 this.fireEvent(name, e);
383 var t = e.getTarget();
385 var cell = Roo.get(t);
391 if(cell.findParent('tfoot', false, true)){
395 if(cell.findParent('thead', false, true)){
397 if(e.getTarget().nodeName.toLowerCase() != 'th'){
398 cell = Roo.get(t).findParent('th', false, true);
400 Roo.log("failed to find th in thead?");
401 Roo.log(e.getTarget());
406 var cellIndex = cell.dom.cellIndex;
408 var ename = name == 'touchstart' ? 'click' : name;
409 this.fireEvent("header" + ename, this, cellIndex, e);
414 if(e.getTarget().nodeName.toLowerCase() != 'td'){
415 cell = Roo.get(t).findParent('td', false, true);
417 Roo.log("failed to find th in tbody?");
418 Roo.log(e.getTarget());
423 var row = cell.findParent('tr', false, true);
424 var cellIndex = cell.dom.cellIndex;
425 var rowIndex = row.dom.rowIndex - 1;
429 this.fireEvent("row" + name, this, rowIndex, e);
433 this.fireEvent("cell" + name, this, rowIndex, cellIndex, e);
439 onMouseover : function(e, el)
441 var cell = Roo.get(el);
447 if(e.getTarget().nodeName.toLowerCase() != 'td'){
448 cell = cell.findParent('td', false, true);
451 var row = cell.findParent('tr', false, true);
452 var cellIndex = cell.dom.cellIndex;
453 var rowIndex = row.dom.rowIndex - 1; // start from 0
455 this.fireEvent('mouseover', this, cell, rowIndex, cellIndex, e);
459 onMouseout : function(e, el)
461 var cell = Roo.get(el);
467 if(e.getTarget().nodeName.toLowerCase() != 'td'){
468 cell = cell.findParent('td', false, true);
471 var row = cell.findParent('tr', false, true);
472 var cellIndex = cell.dom.cellIndex;
473 var rowIndex = row.dom.rowIndex - 1; // start from 0
475 this.fireEvent('mouseout', this, cell, rowIndex, cellIndex, e);
479 onClick : function(e, el)
481 var cell = Roo.get(el);
483 if(!cell || (!this.cellSelection && !this.rowSelection)){
487 if(e.getTarget().nodeName.toLowerCase() != 'td'){
488 cell = cell.findParent('td', false, true);
491 if(!cell || typeof(cell) == 'undefined'){
495 var row = cell.findParent('tr', false, true);
497 if(!row || typeof(row) == 'undefined'){
501 var cellIndex = cell.dom.cellIndex;
502 var rowIndex = this.getRowIndex(row);
504 // why??? - should these not be based on SelectionModel?
505 if(this.cellSelection){
506 this.fireEvent('cellclick', this, cell, rowIndex, cellIndex, e);
509 if(this.rowSelection){
510 this.fireEvent('rowclick', this, row, rowIndex, e);
516 onDblClick : function(e,el)
518 var cell = Roo.get(el);
520 if(!cell || (!this.cellSelection && !this.rowSelection)){
524 if(e.getTarget().nodeName.toLowerCase() != 'td'){
525 cell = cell.findParent('td', false, true);
528 if(!cell || typeof(cell) == 'undefined'){
532 var row = cell.findParent('tr', false, true);
534 if(!row || typeof(row) == 'undefined'){
538 var cellIndex = cell.dom.cellIndex;
539 var rowIndex = this.getRowIndex(row);
541 if(this.cellSelection){
542 this.fireEvent('celldblclick', this, cell, rowIndex, cellIndex, e);
545 if(this.rowSelection){
546 this.fireEvent('rowdblclick', this, row, rowIndex, e);
550 sort : function(e,el)
552 var col = Roo.get(el);
554 if(!col.hasClass('sortable')){
558 var sort = col.attr('sort');
561 if(col.select('i', true).first().hasClass('glyphicon-arrow-up')){
565 this.store.sortInfo = {field : sort, direction : dir};
568 Roo.log("calling footer first");
569 this.footer.onClick('first');
572 this.store.load({ params : { start : 0 } });
576 renderHeader : function()
586 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
588 var config = cm.config[i];
594 html: cm.getColumnHeader(i)
599 if(typeof(config.sortable) != 'undefined' && config.sortable){
601 c.html = '<i class="glyphicon"></i>' + c.html;
604 if(typeof(config.lgHeader) != 'undefined'){
605 hh += '<span class="hidden-xs hidden-sm hidden-md">' + config.lgHeader + '</span>';
608 if(typeof(config.mdHeader) != 'undefined'){
609 hh += '<span class="hidden-xs hidden-sm hidden-lg">' + config.mdHeader + '</span>';
612 if(typeof(config.smHeader) != 'undefined'){
613 hh += '<span class="hidden-xs hidden-md hidden-lg">' + config.smHeader + '</span>';
616 if(typeof(config.xsHeader) != 'undefined'){
617 hh += '<span class="hidden-sm hidden-md hidden-lg">' + config.xsHeader + '</span>';
624 if(typeof(config.tooltip) != 'undefined'){
625 c.tooltip = config.tooltip;
628 if(typeof(config.colspan) != 'undefined'){
629 c.colspan = config.colspan;
632 if(typeof(config.hidden) != 'undefined' && config.hidden){
633 c.style += ' display:none;';
636 if(typeof(config.dataIndex) != 'undefined'){
637 c.sort = config.dataIndex;
642 if(typeof(config.align) != 'undefined' && config.align.length){
643 c.style += ' text-align:' + config.align + ';';
646 if(typeof(config.width) != 'undefined'){
647 c.style += ' width:' + config.width + 'px;';
648 this.totalWidth += config.width;
650 this.totalWidth += 100; // assume minimum of 100 per column?
653 if(typeof(config.cls) != 'undefined'){
654 c.cls = (typeof(c.cls) == 'undefined') ? config.cls : (c.cls + ' ' + config.cls);
657 ['xs','sm','md','lg'].map(function(size){
659 if(typeof(config[size]) == 'undefined'){
663 if (!config[size]) { // 0 = hidden
664 c.cls += ' hidden-' + size;
668 c.cls += ' col-' + size + '-' + config[size];
678 renderBody : function()
688 colspan : this.cm.getColumnCount()
698 renderFooter : function()
708 colspan : this.cm.getColumnCount()
722 // Roo.log('ds onload');
729 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
730 e.select('i', true).removeClass(['glyphicon-arrow-up', 'glyphicon-arrow-down']);
731 if (_this.store.sortInfo) {
733 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
734 e.select('i', true).addClass(['glyphicon-arrow-up']);
737 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
738 e.select('i', true).addClass(['glyphicon-arrow-down']);
743 var tbody = this.mainBody;
745 if(ds.getCount() > 0){
746 ds.data.each(function(d,rowIndex){
747 var row = this.renderRow(cm, ds, rowIndex);
749 tbody.createChild(row);
753 if(row.cellObjects.length){
754 Roo.each(row.cellObjects, function(r){
755 _this.renderCellObject(r);
762 Roo.each(this.el.select('tbody td', true).elements, function(e){
763 e.on('mouseover', _this.onMouseover, _this);
766 Roo.each(this.el.select('tbody td', true).elements, function(e){
767 e.on('mouseout', _this.onMouseout, _this);
769 this.fireEvent('rowsrendered', this);
771 // this.maskEl.hide();
778 onUpdate : function(ds,record)
780 this.refreshRow(record);
784 onRemove : function(ds, record, index, isUpdate){
785 if(isUpdate !== true){
786 this.fireEvent("beforerowremoved", this, index, record);
788 var bt = this.mainBody.dom;
790 var rows = this.el.select('tbody > tr', true).elements;
792 if(typeof(rows[index]) != 'undefined'){
793 bt.removeChild(rows[index].dom);
796 // if(bt.rows[index]){
797 // bt.removeChild(bt.rows[index]);
800 if(isUpdate !== true){
801 //this.stripeRows(index);
802 //this.syncRowHeights(index, index);
804 this.fireEvent("rowremoved", this, index, record);
808 onAdd : function(ds, records, rowIndex)
810 //Roo.log('on Add called');
811 // - note this does not handle multiple adding very well..
812 var bt = this.mainBody.dom;
813 for (var i =0 ; i < records.length;i++) {
814 //Roo.log('call insert row Add called on ' + rowIndex + ':' + i);
815 //Roo.log(records[i]);
816 //Roo.log(this.store.getAt(rowIndex+i));
817 this.insertRow(this.store, rowIndex + i, false);
824 refreshRow : function(record){
825 var ds = this.store, index;
826 if(typeof record == 'number'){
828 record = ds.getAt(index);
830 index = ds.indexOf(record);
832 this.insertRow(ds, index, true);
834 this.onRemove(ds, record, index+1, true);
836 //this.syncRowHeights(index, index);
838 this.fireEvent("rowupdated", this, index, record);
841 insertRow : function(dm, rowIndex, isUpdate){
844 this.fireEvent("beforerowsinserted", this, rowIndex);
846 //var s = this.getScrollState();
847 var row = this.renderRow(this.cm, this.store, rowIndex);
848 // insert before rowIndex..
849 var e = this.mainBody.createChild(row,this.getRowDom(rowIndex));
853 if(row.cellObjects.length){
854 Roo.each(row.cellObjects, function(r){
855 _this.renderCellObject(r);
860 this.fireEvent("rowsinserted", this, rowIndex);
861 //this.syncRowHeights(firstRow, lastRow);
862 //this.stripeRows(firstRow);
869 getRowDom : function(rowIndex)
871 var rows = this.el.select('tbody > tr', true).elements;
873 return (typeof(rows[rowIndex]) == 'undefined') ? false : rows[rowIndex];
876 // returns the object tree for a tr..
879 renderRow : function(cm, ds, rowIndex)
881 var d = ds.getAt(rowIndex);
885 cls : 'x-row-' + rowIndex,
889 var cellObjects = [];
891 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
892 var config = cm.config[i];
894 var renderer = cm.getRenderer(i);
898 if(typeof(renderer) !== 'undefined'){
899 value = renderer(d.data[cm.getDataIndex(i)], false, d);
901 // if object are returned, then they are expected to be Roo.bootstrap.Component instances
902 // and are rendered into the cells after the row is rendered - using the id for the element.
904 if(typeof(value) === 'object'){
919 this.fireEvent('rowclass', this, rowcfg);
923 cls : rowcfg.rowClass + ' x-col-' + i,
925 html: (typeof(value) === 'object') ? '' : value
932 if(typeof(config.colspan) != 'undefined'){
933 td.colspan = config.colspan;
936 if(typeof(config.hidden) != 'undefined' && config.hidden){
937 td.style += ' display:none;';
940 if(typeof(config.align) != 'undefined' && config.align.length){
941 td.style += ' text-align:' + config.align + ';';
944 if(typeof(config.width) != 'undefined'){
945 td.style += ' width:' + config.width + 'px;';
948 if(typeof(config.cursor) != 'undefined'){
949 td.style += ' cursor:' + config.cursor + ';';
952 if(typeof(config.cls) != 'undefined'){
953 td.cls = (typeof(td.cls) == 'undefined') ? config.cls : (td.cls + ' ' + config.cls);
956 ['xs','sm','md','lg'].map(function(size){
958 if(typeof(config[size]) == 'undefined'){
962 if (!config[size]) { // 0 = hidden
963 td.cls += ' hidden-' + size;
967 td.cls += ' col-' + size + '-' + config[size];
975 row.cellObjects = cellObjects;
983 onBeforeLoad : function()
985 //Roo.log('ds onBeforeLoad');
990 // this.maskEl.show();
998 this.el.select('tbody', true).first().dom.innerHTML = '';
1001 * Show or hide a row.
1002 * @param {Number} rowIndex to show or hide
1003 * @param {Boolean} state hide
1005 setRowVisibility : function(rowIndex, state)
1007 var bt = this.mainBody.dom;
1009 var rows = this.el.select('tbody > tr', true).elements;
1011 if(typeof(rows[rowIndex]) == 'undefined'){
1014 rows[rowIndex].dom.style.display = state ? '' : 'none';
1018 getSelectionModel : function(){
1020 this.selModel = new Roo.bootstrap.Table.RowSelectionModel({grid: this});
1022 return this.selModel;
1025 * Render the Roo.bootstrap object from renderder
1027 renderCellObject : function(r)
1031 r.cfg.parentId = (typeof(r.container) == 'string') ? r.container : r.container.id;
1033 var t = r.cfg.render(r.container);
1036 Roo.each(r.cfg.cn, function(c){
1038 container: t.getChildContainer(),
1041 _this.renderCellObject(child);
1046 getRowIndex : function(row)
1050 Roo.each(this.el.select('tbody > tr', true).elements, function(el, index){
1061 * Returns the grid's underlying element = used by panel.Grid
1062 * @return {Element} The element
1064 getGridEl : function(){
1068 * Forces a resize - used by panel.Grid
1069 * @return {Element} The element
1071 autoSize : function()
1073 //var ctr = Roo.get(this.container.dom.parentElement);
1074 var ctr = Roo.get(this.el.dom);
1076 var thd = this.getGridEl().select('thead',true).first();
1077 var tbd = this.getGridEl().select('tbody', true).first();
1078 var tfd = this.getGridEl().select('tfoot', true).first();
1080 var cw = ctr.getWidth();
1084 tbd.setSize(ctr.getWidth(),
1085 ctr.getHeight() - ((thd ? thd.getHeight() : 0) + (tfd ? tfd.getHeight() : 0))
1087 var barsize = (tbd.dom.offsetWidth - tbd.dom.clientWidth);
1090 cw = Math.max(cw, this.totalWidth);
1091 this.getGridEl().select('tr',true).setWidth(cw);
1092 // resize 'expandable coloumn?
1094 return; // we doe not have a view in this design..
1097 onBodyScroll: function()
1099 //Roo.log("body scrolled');" + this.mainBody.dom.scrollLeft);
1101 this.mainHead.setStyle({
1102 'position' : 'relative',
1103 'left': (-1* this.mainBody.dom.scrollLeft) + 'px'
1109 var scrollHeight = this.mainBody.dom.scrollHeight;
1111 var scrollTop = Math.ceil(this.mainBody.getScroll().top);
1113 var height = this.mainBody.getHeight();
1115 if(scrollHeight - height == scrollTop) {
1117 var total = this.ds.getTotalCount();
1119 if(this.footer.cursor + this.footer.pageSize < total){
1121 this.footer.ds.load({
1123 start : this.footer.cursor + this.footer.pageSize,
1124 limit : this.footer.pageSize
1134 onHeaderChange : function()
1136 var header = this.renderHeader();
1137 var table = this.el.select('table', true).first();
1139 this.mainHead.remove();
1140 this.mainHead = table.createChild(header, this.mainBody, false);
1143 onHiddenChange : function(colModel, colIndex, hidden)
1145 var thSelector = 'roo-bootstrap-thead-col-' + colIndex;
1146 var tdSelector = 'roo-bootstrap-tbody-col-' + colIndex;
1148 this.CSS.updateRule(thSelector, "display", "");
1149 this.CSS.updateRule(tdSelector, "display", "");
1152 this.CSS.updateRule(thSelector, "display", "none");
1153 this.CSS.updateRule(tdSelector, "display", "none");
1156 this.onHeaderChange();