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 {Roo.bootstrap.PagingToolbar} footer a paging toolbar
40 * @param {Object} config The config object
43 Roo.bootstrap.Table = function(config){
44 Roo.bootstrap.Table.superclass.constructor.call(this, config);
47 this.rowSelection = (typeof(config.RowSelection) != 'undefined') ? config.RowSelection : this.rowSelection;
48 this.cellSelection = (typeof(config.CellSelection) != 'undefined') ? config.CellSelection : this.cellSelection;
49 this.headerShow = (typeof(config.thead) != 'undefined') ? config.thead : this.headerShow;
50 this.footerShow = (typeof(config.tfoot) != 'undefined') ? config.tfoot : this.footerShow;
54 this.selModel = Roo.factory(this.sm, Roo.bootstrap.Table);
55 this.sm = this.selModel;
56 this.sm.xmodule = this.xmodule || false;
58 if (this.cm && typeof(this.cm.config) == 'undefined') {
59 this.colModel = new Roo.grid.ColumnModel(this.cm);
60 this.cm = this.colModel;
61 this.cm.xmodule = this.xmodule || false;
64 this.store= Roo.factory(this.store, Roo.data);
66 this.ds.xmodule = this.xmodule || false;
69 if (this.footer && this.store) {
70 this.footer.dataSource = this.ds;
71 this.footer = Roo.factory(this.footer);
78 * Fires when a cell is clicked
79 * @param {Roo.bootstrap.Table} this
80 * @param {Roo.Element} el
81 * @param {Number} rowIndex
82 * @param {Number} columnIndex
83 * @param {Roo.EventObject} e
88 * Fires when a cell is double clicked
89 * @param {Roo.bootstrap.Table} this
90 * @param {Roo.Element} el
91 * @param {Number} rowIndex
92 * @param {Number} columnIndex
93 * @param {Roo.EventObject} e
95 "celldblclick" : true,
98 * Fires when a row is clicked
99 * @param {Roo.bootstrap.Table} this
100 * @param {Roo.Element} el
101 * @param {Number} rowIndex
102 * @param {Roo.EventObject} e
107 * Fires when a row is double clicked
108 * @param {Roo.bootstrap.Table} this
109 * @param {Roo.Element} el
110 * @param {Number} rowIndex
111 * @param {Roo.EventObject} e
113 "rowdblclick" : true,
116 * Fires when a mouseover occur
117 * @param {Roo.bootstrap.Table} this
118 * @param {Roo.Element} el
119 * @param {Number} rowIndex
120 * @param {Number} columnIndex
121 * @param {Roo.EventObject} e
126 * Fires when a mouseout occur
127 * @param {Roo.bootstrap.Table} this
128 * @param {Roo.Element} el
129 * @param {Number} rowIndex
130 * @param {Number} columnIndex
131 * @param {Roo.EventObject} e
136 * Fires when a row is rendered, so you can change add a style to it.
137 * @param {Roo.bootstrap.Table} this
138 * @param {Object} rowcfg contains record rowIndex colIndex and rowClass - set rowClass to add a style.
142 * @event rowsrendered
143 * Fires when all the rows have been rendered
144 * @param {Roo.bootstrap.Table} this
146 'rowsrendered' : true,
149 * The raw contextmenu event for the entire grid.
150 * @param {Roo.EventObject} e
152 "contextmenu" : true,
154 * @event rowcontextmenu
155 * Fires when a row is right clicked
156 * @param {Roo.bootstrap.Table} this
157 * @param {Number} rowIndex
158 * @param {Roo.EventObject} e
160 "rowcontextmenu" : true,
162 * @event cellcontextmenu
163 * Fires when a cell is right clicked
164 * @param {Roo.bootstrap.Table} this
165 * @param {Number} rowIndex
166 * @param {Number} cellIndex
167 * @param {Roo.EventObject} e
169 "cellcontextmenu" : true,
171 * @event headercontextmenu
172 * Fires when a header is right clicked
173 * @param {Roo.bootstrap.Table} this
174 * @param {Number} columnIndex
175 * @param {Roo.EventObject} e
177 "headercontextmenu" : true,
181 Roo.extend(Roo.bootstrap.Table, Roo.bootstrap.Component, {
206 rowSelection : false,
207 cellSelection : false,
210 // Roo.Element - the tbody
213 getAutoCreate : function(){
214 var cfg = Roo.apply({}, Roo.bootstrap.Table.superclass.getAutoCreate.call(this));
223 cfg.cls += ' table-striped';
227 cfg.cls += ' table-hover';
230 cfg.cls += ' table-bordered';
232 if (this.condensed) {
233 cfg.cls += ' table-condensed';
235 if (this.responsive) {
236 cfg.cls += ' table-responsive';
240 cfg.cls+= ' ' +this.cls;
243 // this lot should be simplifed...
246 cfg.align=this.align;
249 cfg.bgcolor=this.bgcolor;
252 cfg.border=this.border;
254 if (this.cellpadding) {
255 cfg.cellpadding=this.cellpadding;
257 if (this.cellspacing) {
258 cfg.cellspacing=this.cellspacing;
261 cfg.frame=this.frame;
264 cfg.rules=this.rules;
267 cfg.sortable=this.sortable;
270 cfg.summary=this.summary;
273 cfg.width=this.width;
276 cfg.style = (typeof(cfg.style) == 'undefined') ? ('table-layout:' + this.layout + ';') : (cfg.style + ('table-layout:' + this.layout + ';'));
279 if(this.store || this.cm){
281 cfg.cn.push(this.renderHeader());
284 cfg.cn.push(this.renderBody());
287 cfg.cn.push(this.renderFooter());
290 cfg.cls+= ' TableGrid';
293 return { cn : [ cfg ] };
296 initEvents : function()
298 if(!this.store || !this.cm){
302 //Roo.log('initEvents with ds!!!!');
304 this.mainBody = this.el.select('tbody', true).first();
309 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
310 e.on('click', _this.sort, _this);
313 this.el.on("click", this.onClick, this);
314 this.el.on("dblclick", this.onDblClick, this);
316 // why is this done????? = it breaks dialogs??
317 //this.parent().el.setStyle('position', 'relative');
321 this.footer.parentId = this.id;
322 this.footer.onRender(this.el.select('tfoot tr td').first(), null);
325 this.maskEl = new Roo.LoadMask(this.el, { store : this.ds, msgCls: 'roo-el-mask-msg' });
327 this.store.on('load', this.onLoad, this);
328 this.store.on('beforeload', this.onBeforeLoad, this);
329 this.store.on('update', this.onUpdate, this);
330 this.store.on('add', this.onAdd, this);
332 this.el.on("contextmenu", this.onContextMenu, this);
336 onContextMenu : function(e, t)
338 this.processEvent("contextmenu", e);
341 processEvent : function(name, e)
343 if (name != 'touchstart' ) {
344 this.fireEvent(name, e);
347 var t = e.getTarget();
349 var cell = Roo.get(t);
355 if(cell.findParent('tfoot', false, true)){
359 if(cell.findParent('thead', false, true)){
361 if(e.getTarget().nodeName.toLowerCase() != 'th'){
362 cell = Roo.get(t).findParent('th', false, true);
365 var cellIndex = cell.dom.cellIndex;
367 var ename = name == 'touchstart' ? 'click' : name;
368 this.fireEvent("header" + ename, this, cellIndex, e);
373 if(e.getTarget().nodeName.toLowerCase() != 'td'){
374 cell = Roo.get(t).findParent('td', false, true);
377 var row = cell.findParent('tr', false, true);
378 var cellIndex = cell.dom.cellIndex;
379 var rowIndex = row.dom.rowIndex - 1;
383 this.fireEvent("row" + name, this, rowIndex, e);
387 this.fireEvent("cell" + name, this, rowIndex, cellIndex, e);
393 onMouseover : function(e, el)
395 var cell = Roo.get(el);
401 if(e.getTarget().nodeName.toLowerCase() != 'td'){
402 cell = cell.findParent('td', false, true);
405 var row = cell.findParent('tr', false, true);
406 var cellIndex = cell.dom.cellIndex;
407 var rowIndex = row.dom.rowIndex - 1; // start from 0
409 this.fireEvent('mouseover', this, cell, rowIndex, cellIndex, e);
413 onMouseout : function(e, el)
415 var cell = Roo.get(el);
421 if(e.getTarget().nodeName.toLowerCase() != 'td'){
422 cell = cell.findParent('td', false, true);
425 var row = cell.findParent('tr', false, true);
426 var cellIndex = cell.dom.cellIndex;
427 var rowIndex = row.dom.rowIndex - 1; // start from 0
429 this.fireEvent('mouseout', this, cell, rowIndex, cellIndex, e);
433 onClick : function(e, el)
435 var cell = Roo.get(el);
437 if(!cell || (!this.cellSelection && !this.rowSelection)){
441 if(e.getTarget().nodeName.toLowerCase() != 'td'){
442 cell = cell.findParent('td', false, true);
445 if(!cell || typeof(cell) == 'undefined'){
449 var row = cell.findParent('tr', false, true);
451 if(!row || typeof(row) == 'undefined'){
455 var cellIndex = cell.dom.cellIndex;
456 var rowIndex = this.getRowIndex(row);
458 // why??? - should these not be based on SelectionModel?
459 if(this.cellSelection){
460 this.fireEvent('cellclick', this, cell, rowIndex, cellIndex, e);
463 if(this.rowSelection){
464 this.fireEvent('rowclick', this, row, rowIndex, e);
470 onDblClick : function(e,el)
472 var cell = Roo.get(el);
474 if(!cell || (!this.CellSelection && !this.RowSelection)){
478 if(e.getTarget().nodeName.toLowerCase() != 'td'){
479 cell = cell.findParent('td', false, true);
482 if(!cell || typeof(cell) == 'undefined'){
486 var row = cell.findParent('tr', false, true);
488 if(!row || typeof(row) == 'undefined'){
492 var cellIndex = cell.dom.cellIndex;
493 var rowIndex = this.getRowIndex(row);
495 if(this.CellSelection){
496 this.fireEvent('celldblclick', this, cell, rowIndex, cellIndex, e);
499 if(this.RowSelection){
500 this.fireEvent('rowdblclick', this, row, rowIndex, e);
504 sort : function(e,el)
506 var col = Roo.get(el);
508 if(!col.hasClass('sortable')){
512 var sort = col.attr('sort');
515 if(col.hasClass('glyphicon-arrow-up')){
519 this.store.sortInfo = {field : sort, direction : dir};
522 Roo.log("calling footer first");
523 this.footer.onClick('first');
526 this.store.load({ params : { start : 0 } });
530 renderHeader : function()
539 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
541 var config = cm.config[i];
546 html: cm.getColumnHeader(i)
551 if(typeof(config.lgHeader) != 'undefined'){
552 hh += '<span class="hidden-xs hidden-sm hidden-md">' + config.lgHeader + '</span>';
555 if(typeof(config.mdHeader) != 'undefined'){
556 hh += '<span class="hidden-xs hidden-sm hidden-lg">' + config.mdHeader + '</span>';
559 if(typeof(config.smHeader) != 'undefined'){
560 hh += '<span class="hidden-xs hidden-md hidden-lg">' + config.smHeader + '</span>';
563 if(typeof(config.xsHeader) != 'undefined'){
564 hh += '<span class="hidden-sm hidden-md hidden-lg">' + config.xsHeader + '</span>';
571 if(typeof(config.tooltip) != 'undefined'){
572 c.tooltip = config.tooltip;
575 if(typeof(config.colspan) != 'undefined'){
576 c.colspan = config.colspan;
579 if(typeof(config.hidden) != 'undefined' && config.hidden){
580 c.style += ' display:none;';
583 if(typeof(config.dataIndex) != 'undefined'){
584 c.sort = config.dataIndex;
587 if(typeof(config.sortable) != 'undefined' && config.sortable){
591 if(typeof(config.align) != 'undefined' && config.align.length){
592 c.style += ' text-align:' + config.align + ';';
595 if(typeof(config.width) != 'undefined'){
596 c.style += ' width:' + config.width + 'px;';
599 if(typeof(config.cls) != 'undefined'){
600 c.cls = (typeof(c.cls) == 'undefined') ? config.cls : (c.cls + ' ' + config.cls);
603 ['xs','sm','md','lg'].map(function(size){
605 if(typeof(config[size]) == 'undefined'){
609 if (!config[size]) { // 0 = hidden
610 c.cls += ' hidden-' + size;
614 c.cls += ' col-' + size + '-' + config[size];
624 renderBody : function()
634 colspan : this.cm.getColumnCount()
644 renderFooter : function()
654 colspan : this.cm.getColumnCount()
668 // Roo.log('ds onload');
675 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
676 e.removeClass(['glyphicon', 'glyphicon-arrow-up', 'glyphicon-arrow-down']);
678 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
679 e.addClass(['glyphicon', 'glyphicon-arrow-up']);
682 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
683 e.addClass(['glyphicon', 'glyphicon-arrow-down']);
687 var tbody = this.mainBody;
689 if(ds.getCount() > 0){
690 ds.data.each(function(d,rowIndex){
691 var row = this.renderRow(cm, ds, rowIndex);
693 tbody.createChild(row);
697 if(row.cellObjects.length){
698 Roo.each(row.cellObjects, function(r){
699 _this.renderCellObject(r);
706 Roo.each(this.el.select('tbody td', true).elements, function(e){
707 e.on('mouseover', _this.onMouseover, _this);
710 Roo.each(this.el.select('tbody td', true).elements, function(e){
711 e.on('mouseout', _this.onMouseout, _this);
713 this.fireEvent('rowsrendered', this);
715 // this.maskEl.hide();
720 onUpdate : function(ds,record)
722 this.refreshRow(record);
725 onRemove : function(ds, record, index, isUpdate){
726 if(isUpdate !== true){
727 this.fireEvent("beforerowremoved", this, index, record);
729 var bt = this.mainBody.dom;
731 var rows = this.el.select('tbody > tr', true).elements;
733 if(typeof(rows[index]) != 'undefined'){
734 bt.removeChild(rows[index].dom);
737 // if(bt.rows[index]){
738 // bt.removeChild(bt.rows[index]);
741 if(isUpdate !== true){
742 //this.stripeRows(index);
743 //this.syncRowHeights(index, index);
745 this.fireEvent("rowremoved", this, index, record);
749 onAdd : function(ds, records, rowIndex)
751 //Roo.log('on Add called');
752 // - note this does not handle multiple adding very well..
753 var bt = this.mainBody.dom;
754 for (var i =0 ; i < records.length;i++) {
755 //Roo.log('call insert row Add called on ' + rowIndex + ':' + i);
756 //Roo.log(records[i]);
757 //Roo.log(this.store.getAt(rowIndex+i));
758 this.insertRow(this.store, rowIndex + i, false);
765 refreshRow : function(record){
766 var ds = this.store, index;
767 if(typeof record == 'number'){
769 record = ds.getAt(index);
771 index = ds.indexOf(record);
773 this.insertRow(ds, index, true);
774 this.onRemove(ds, record, index+1, true);
775 //this.syncRowHeights(index, index);
777 this.fireEvent("rowupdated", this, index, record);
780 insertRow : function(dm, rowIndex, isUpdate){
783 this.fireEvent("beforerowsinserted", this, rowIndex);
785 //var s = this.getScrollState();
786 var row = this.renderRow(this.cm, this.store, rowIndex);
787 // insert before rowIndex..
788 var e = this.mainBody.createChild(row,this.getRowDom(rowIndex));
792 if(row.cellObjects.length){
793 Roo.each(row.cellObjects, function(r){
794 _this.renderCellObject(r);
799 this.fireEvent("rowsinserted", this, rowIndex);
800 //this.syncRowHeights(firstRow, lastRow);
801 //this.stripeRows(firstRow);
808 getRowDom : function(rowIndex)
810 var rows = this.el.select('tbody > tr', true).elements;
812 return (typeof(rows[rowIndex]) == 'undefined') ? false : rows[rowIndex];
815 // returns the object tree for a tr..
818 renderRow : function(cm, ds, rowIndex)
821 var d = ds.getAt(rowIndex);
828 var cellObjects = [];
830 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
831 var config = cm.config[i];
833 var renderer = cm.getRenderer(i);
837 if(typeof(renderer) !== 'undefined'){
838 value = renderer(d.data[cm.getDataIndex(i)], false, d);
840 // if object are returned, then they are expected to be Roo.bootstrap.Component instances
841 // and are rendered into the cells after the row is rendered - using the id for the element.
843 if(typeof(value) === 'object'){
858 this.fireEvent('rowclass', this, rowcfg);
862 cls : rowcfg.rowClass,
864 html: (typeof(value) === 'object') ? '' : value
871 if(typeof(config.colspan) != 'undefined'){
872 td.colspan = config.colspan;
875 if(typeof(config.hidden) != 'undefined' && config.hidden){
876 td.style += ' display:none;';
879 if(typeof(config.align) != 'undefined' && config.align.length){
880 td.style += ' text-align:' + config.align + ';';
883 if(typeof(config.width) != 'undefined'){
884 td.style += ' width:' + config.width + 'px;';
887 if(typeof(config.cursor) != 'undefined'){
888 td.style += ' cursor:' + config.cursor + ';';
891 if(typeof(config.cls) != 'undefined'){
892 td.cls = (typeof(td.cls) == 'undefined') ? config.cls : (td.cls + ' ' + config.cls);
895 ['xs','sm','md','lg'].map(function(size){
897 if(typeof(config[size]) == 'undefined'){
901 if (!config[size]) { // 0 = hidden
902 td.cls += ' hidden-' + size;
906 td.cls += ' col-' + size + '-' + config[size];
914 row.cellObjects = cellObjects;
922 onBeforeLoad : function()
924 //Roo.log('ds onBeforeLoad');
929 // this.maskEl.show();
937 this.el.select('tbody', true).first().dom.innerHTML = '';
940 * Show or hide a row.
941 * @param {Number} rowIndex to show or hide
942 * @param {Boolean} state hide
944 setRowVisibility : function(rowIndex, state)
946 var bt = this.mainBody.dom;
948 var rows = this.el.select('tbody > tr', true).elements;
950 if(typeof(rows[rowIndex]) == 'undefined'){
953 rows[rowIndex].dom.style.display = state ? '' : 'none';
957 getSelectionModel : function(){
959 this.selModel = new Roo.bootstrap.Table.RowSelectionModel();
961 return this.selModel;
964 * Render the Roo.bootstrap object from renderder
966 renderCellObject : function(r)
970 var t = r.cfg.render(r.container);
973 Roo.each(r.cfg.cn, function(c){
975 container: t.getChildContainer(),
978 _this.renderCellObject(child);
983 getRowIndex : function(row)
987 Roo.each(this.el.select('tbody > tr', true).elements, function(el, index){