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);
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];
592 cls : 'roo-bootstrap-thead-row-' + rowIndex,
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];
682 renderBody : function()
692 colspan : this.cm.getColumnCount()
702 renderFooter : function()
712 colspan : this.cm.getColumnCount()
726 // Roo.log('ds onload');
733 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
734 e.select('i', true).removeClass(['glyphicon-arrow-up', 'glyphicon-arrow-down']);
735 if (_this.store.sortInfo) {
737 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
738 e.select('i', true).addClass(['glyphicon-arrow-up']);
741 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
742 e.select('i', true).addClass(['glyphicon-arrow-down']);
747 var tbody = this.mainBody;
749 if(ds.getCount() > 0){
750 ds.data.each(function(d,rowIndex){
751 var row = this.renderRow(cm, ds, rowIndex);
753 tbody.createChild(row);
757 if(row.cellObjects.length){
758 Roo.each(row.cellObjects, function(r){
759 _this.renderCellObject(r);
766 Roo.each(this.el.select('tbody td', true).elements, function(e){
767 e.on('mouseover', _this.onMouseover, _this);
770 Roo.each(this.el.select('tbody td', true).elements, function(e){
771 e.on('mouseout', _this.onMouseout, _this);
773 this.fireEvent('rowsrendered', this);
775 // this.maskEl.hide();
782 onUpdate : function(ds,record)
784 this.refreshRow(record);
788 onRemove : function(ds, record, index, isUpdate){
789 if(isUpdate !== true){
790 this.fireEvent("beforerowremoved", this, index, record);
792 var bt = this.mainBody.dom;
794 var rows = this.el.select('tbody > tr', true).elements;
796 if(typeof(rows[index]) != 'undefined'){
797 bt.removeChild(rows[index].dom);
800 // if(bt.rows[index]){
801 // bt.removeChild(bt.rows[index]);
804 if(isUpdate !== true){
805 //this.stripeRows(index);
806 //this.syncRowHeights(index, index);
808 this.fireEvent("rowremoved", this, index, record);
812 onAdd : function(ds, records, rowIndex)
814 //Roo.log('on Add called');
815 // - note this does not handle multiple adding very well..
816 var bt = this.mainBody.dom;
817 for (var i =0 ; i < records.length;i++) {
818 //Roo.log('call insert row Add called on ' + rowIndex + ':' + i);
819 //Roo.log(records[i]);
820 //Roo.log(this.store.getAt(rowIndex+i));
821 this.insertRow(this.store, rowIndex + i, false);
828 refreshRow : function(record){
829 var ds = this.store, index;
830 if(typeof record == 'number'){
832 record = ds.getAt(index);
834 index = ds.indexOf(record);
836 this.insertRow(ds, index, true);
838 this.onRemove(ds, record, index+1, true);
840 //this.syncRowHeights(index, index);
842 this.fireEvent("rowupdated", this, index, record);
845 insertRow : function(dm, rowIndex, isUpdate){
848 this.fireEvent("beforerowsinserted", this, rowIndex);
850 //var s = this.getScrollState();
851 var row = this.renderRow(this.cm, this.store, rowIndex);
852 // insert before rowIndex..
853 var e = this.mainBody.createChild(row,this.getRowDom(rowIndex));
857 if(row.cellObjects.length){
858 Roo.each(row.cellObjects, function(r){
859 _this.renderCellObject(r);
864 this.fireEvent("rowsinserted", this, rowIndex);
865 //this.syncRowHeights(firstRow, lastRow);
866 //this.stripeRows(firstRow);
873 getRowDom : function(rowIndex)
875 var rows = this.el.select('tbody > tr', true).elements;
877 return (typeof(rows[rowIndex]) == 'undefined') ? false : rows[rowIndex];
880 // returns the object tree for a tr..
883 renderRow : function(cm, ds, rowIndex)
885 var d = ds.getAt(rowIndex);
889 cls : 'roo-bootstrap-tbody-row-' + rowIndex,
893 var cellObjects = [];
895 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
896 var config = cm.config[i];
898 var renderer = cm.getRenderer(i);
902 if(typeof(renderer) !== 'undefined'){
903 value = renderer(d.data[cm.getDataIndex(i)], false, d);
905 // if object are returned, then they are expected to be Roo.bootstrap.Component instances
906 // and are rendered into the cells after the row is rendered - using the id for the element.
908 if(typeof(value) === 'object'){
923 this.fireEvent('rowclass', this, rowcfg);
927 cls : rowcfg.rowClass + ' roo-bootstrap-tbody-col-' + i,
929 html: (typeof(value) === 'object') ? '' : value
936 if(typeof(config.colspan) != 'undefined'){
937 td.colspan = config.colspan;
940 if(typeof(config.hidden) != 'undefined' && config.hidden){
941 td.style += ' display:none;';
944 if(typeof(config.align) != 'undefined' && config.align.length){
945 td.style += ' text-align:' + config.align + ';';
948 if(typeof(config.width) != 'undefined'){
949 td.style += ' width:' + config.width + 'px;';
952 if(typeof(config.cursor) != 'undefined'){
953 td.style += ' cursor:' + config.cursor + ';';
956 if(typeof(config.cls) != 'undefined'){
957 td.cls = (typeof(td.cls) == 'undefined') ? config.cls : (td.cls + ' ' + config.cls);
960 ['xs','sm','md','lg'].map(function(size){
962 if(typeof(config[size]) == 'undefined'){
966 if (!config[size]) { // 0 = hidden
967 td.cls += ' hidden-' + size;
971 td.cls += ' col-' + size + '-' + config[size];
983 row.cellObjects = cellObjects;
991 onBeforeLoad : function()
993 //Roo.log('ds onBeforeLoad');
998 // this.maskEl.show();
1006 this.el.select('tbody', true).first().dom.innerHTML = '';
1009 * Show or hide a row.
1010 * @param {Number} rowIndex to show or hide
1011 * @param {Boolean} state hide
1013 setRowVisibility : function(rowIndex, state)
1015 var bt = this.mainBody.dom;
1017 var rows = this.el.select('tbody > tr', true).elements;
1019 if(typeof(rows[rowIndex]) == 'undefined'){
1022 rows[rowIndex].dom.style.display = state ? '' : 'none';
1026 getSelectionModel : function(){
1028 this.selModel = new Roo.bootstrap.Table.RowSelectionModel({grid: this});
1030 return this.selModel;
1033 * Render the Roo.bootstrap object from renderder
1035 renderCellObject : function(r)
1039 r.cfg.parentId = (typeof(r.container) == 'string') ? r.container : r.container.id;
1041 var t = r.cfg.render(r.container);
1044 Roo.each(r.cfg.cn, function(c){
1046 container: t.getChildContainer(),
1049 _this.renderCellObject(child);
1054 getRowIndex : function(row)
1058 Roo.each(this.el.select('tbody > tr', true).elements, function(el, index){
1069 * Returns the grid's underlying element = used by panel.Grid
1070 * @return {Element} The element
1072 getGridEl : function(){
1076 * Forces a resize - used by panel.Grid
1077 * @return {Element} The element
1079 autoSize : function()
1081 //var ctr = Roo.get(this.container.dom.parentElement);
1082 var ctr = Roo.get(this.el.dom);
1084 var thd = this.getGridEl().select('thead',true).first();
1085 var tbd = this.getGridEl().select('tbody', true).first();
1086 var tfd = this.getGridEl().select('tfoot', true).first();
1088 var cw = ctr.getWidth();
1092 tbd.setSize(ctr.getWidth(),
1093 ctr.getHeight() - ((thd ? thd.getHeight() : 0) + (tfd ? tfd.getHeight() : 0))
1095 var barsize = (tbd.dom.offsetWidth - tbd.dom.clientWidth);
1098 cw = Math.max(cw, this.totalWidth);
1099 this.getGridEl().select('tr',true).setWidth(cw);
1100 // resize 'expandable coloumn?
1102 return; // we doe not have a view in this design..
1105 onBodyScroll: function()
1107 //Roo.log("body scrolled');" + this.mainBody.dom.scrollLeft);
1109 this.mainHead.setStyle({
1110 'position' : 'relative',
1111 'left': (-1* this.mainBody.dom.scrollLeft) + 'px'
1117 var scrollHeight = this.mainBody.dom.scrollHeight;
1119 var scrollTop = Math.ceil(this.mainBody.getScroll().top);
1121 var height = this.mainBody.getHeight();
1123 if(scrollHeight - height == scrollTop) {
1125 var total = this.ds.getTotalCount();
1127 if(this.footer.cursor + this.footer.pageSize < total){
1129 this.footer.ds.load({
1131 start : this.footer.cursor + this.footer.pageSize,
1132 limit : this.footer.pageSize
1142 onHeaderChange : function()
1145 var header = this.renderHeader();
1146 var table = this.el.select('table', true).first();
1148 this.mainHead.remove();
1149 this.mainHead = table.createChild(header, this.mainBody, false);
1152 onHiddenChange : function()
1155 this.onHeaderChange();