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} tfoot (true|false) generate tfoot, default true
32 * @cfg {Boolean} thead (true|false) generate thead, default true
33 * @cfg {Boolean} RowSelection (true|false) default false
34 * @cfg {Boolean} CellSelection (true|false) default false
36 * @cfg {Roo.bootstrap.PagingToolbar} footer a paging toolbar
41 * @param {Object} config The config object
44 Roo.bootstrap.Table = function(config){
45 Roo.bootstrap.Table.superclass.constructor.call(this, config);
48 this.selModel = Roo.factory(this.sm, Roo.bootstrap.Table);
49 this.sm = this.selModel;
50 this.sm.xmodule = this.xmodule || false;
52 if (this.cm && typeof(this.cm.config) == 'undefined') {
53 this.colModel = new Roo.grid.ColumnModel(this.cm);
54 this.cm = this.colModel;
55 this.cm.xmodule = this.xmodule || false;
58 this.store= Roo.factory(this.store, Roo.data);
60 this.ds.xmodule = this.xmodule || false;
63 if (this.footer && this.store) {
64 this.footer.dataSource = this.ds;
65 this.footer = Roo.factory(this.footer);
72 * Fires when a cell is clicked
73 * @param {Roo.bootstrap.Table} this
74 * @param {Roo.Element} el
75 * @param {Number} rowIndex
76 * @param {Number} columnIndex
77 * @param {Roo.EventObject} e
82 * Fires when a cell is double clicked
83 * @param {Roo.bootstrap.Table} this
84 * @param {Roo.Element} el
85 * @param {Number} rowIndex
86 * @param {Number} columnIndex
87 * @param {Roo.EventObject} e
89 "celldblclick" : true,
92 * Fires when a row is clicked
93 * @param {Roo.bootstrap.Table} this
94 * @param {Roo.Element} el
95 * @param {Number} rowIndex
96 * @param {Roo.EventObject} e
101 * Fires when a row is double clicked
102 * @param {Roo.bootstrap.Table} this
103 * @param {Roo.Element} el
104 * @param {Number} rowIndex
105 * @param {Roo.EventObject} e
107 "rowdblclick" : true,
110 * Fires when a mouseover occur
111 * @param {Roo.bootstrap.Table} this
112 * @param {Roo.Element} el
113 * @param {Number} rowIndex
114 * @param {Number} columnIndex
115 * @param {Roo.EventObject} e
120 * Fires when a mouseout occur
121 * @param {Roo.bootstrap.Table} this
122 * @param {Roo.Element} el
123 * @param {Number} rowIndex
124 * @param {Number} columnIndex
125 * @param {Roo.EventObject} e
130 * Fires when a row is rendered, so you can change add a style to it.
131 * @param {Roo.bootstrap.Table} this
132 * @param {Object} rowcfg contains record rowIndex colIndex and rowClass - set rowClass to add a style.
139 Roo.extend(Roo.bootstrap.Table, Roo.bootstrap.Component, {
163 RowSelection : false,
164 CellSelection : false,
167 // Roo.Element - the tbody
170 getAutoCreate : function(){
171 var cfg = Roo.apply({}, Roo.bootstrap.Table.superclass.getAutoCreate.call(this));
180 cfg.cls += ' table-striped';
184 cfg.cls += ' table-hover';
187 cfg.cls += ' table-bordered';
189 if (this.condensed) {
190 cfg.cls += ' table-condensed';
192 if (this.responsive) {
193 cfg.cls += ' table-responsive';
197 cfg.cls+= ' ' +this.cls;
200 // this lot should be simplifed...
203 cfg.align=this.align;
206 cfg.bgcolor=this.bgcolor;
209 cfg.border=this.border;
211 if (this.cellpadding) {
212 cfg.cellpadding=this.cellpadding;
214 if (this.cellspacing) {
215 cfg.cellspacing=this.cellspacing;
218 cfg.frame=this.frame;
221 cfg.rules=this.rules;
224 cfg.sortable=this.sortable;
227 cfg.summary=this.summary;
230 cfg.width=this.width;
233 cfg.style = (typeof(cfg.style) == 'undefined') ? ('table-layout:' + this.layout + ';') : (cfg.style + ('table-layout:' + this.layout + ';'));
236 if(this.store || this.cm){
238 cfg.cn.push(this.renderHeader());
241 cfg.cn.push(this.renderBody());
244 cfg.cn.push(this.renderFooter());
247 cfg.cls+= ' TableGrid';
250 return { cn : [ cfg ] };
253 initEvents : function()
255 if(!this.store || !this.cm){
259 //Roo.log('initEvents with ds!!!!');
261 this.mainBody = this.el.select('tbody', true).first();
266 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
267 e.on('click', _this.sort, _this);
270 this.el.on("click", this.onClick, this);
271 this.el.on("dblclick", this.onDblClick, this);
273 this.parent().el.setStyle('position', 'relative');
275 this.footer.parentId = this.id;
276 this.footer.onRender(this.el.select('tfoot tr td').first(), null);
279 this.maskEl = new Roo.LoadMask(this.el, { store : this.ds, msgCls: 'roo-el-mask-msg' });
281 this.store.on('load', this.onLoad, this);
282 this.store.on('beforeload', this.onBeforeLoad, this);
283 this.store.on('update', this.onUpdate, this);
287 onMouseover : function(e, el)
289 var cell = Roo.get(el);
295 if(e.getTarget().nodeName.toLowerCase() != 'td'){
296 cell = cell.findParent('td', false, true);
299 var row = cell.findParent('tr', false, true);
300 var cellIndex = cell.dom.cellIndex;
301 var rowIndex = row.dom.rowIndex - 1; // start from 0
303 this.fireEvent('mouseover', this, cell, rowIndex, cellIndex, e);
307 onMouseout : function(e, el)
309 var cell = Roo.get(el);
315 if(e.getTarget().nodeName.toLowerCase() != 'td'){
316 cell = cell.findParent('td', false, true);
319 var row = cell.findParent('tr', false, true);
320 var cellIndex = cell.dom.cellIndex;
321 var rowIndex = row.dom.rowIndex - 1; // start from 0
323 this.fireEvent('mouseout', this, cell, rowIndex, cellIndex, e);
327 onClick : function(e, el)
329 var cell = Roo.get(el);
331 if(!cell || (!this.CellSelection && !this.RowSelection)){
336 if(e.getTarget().nodeName.toLowerCase() != 'td'){
337 cell = cell.findParent('td', false, true);
340 var row = cell.findParent('tr', false, true);
341 var cellIndex = cell.dom.cellIndex;
342 var rowIndex = row.dom.rowIndex - 1;
344 if(this.CellSelection){
345 this.fireEvent('cellclick', this, cell, rowIndex, cellIndex, e);
348 if(this.RowSelection){
349 this.fireEvent('rowclick', this, row, rowIndex, e);
355 onDblClick : function(e,el)
357 var cell = Roo.get(el);
359 if(!cell || (!this.CellSelection && !this.RowSelection)){
363 if(e.getTarget().nodeName.toLowerCase() != 'td'){
364 cell = cell.findParent('td', false, true);
367 var row = cell.findParent('tr', false, true);
368 var cellIndex = cell.dom.cellIndex;
369 var rowIndex = row.dom.rowIndex - 1;
371 if(this.CellSelection){
372 this.fireEvent('celldblclick', this, cell, rowIndex, cellIndex, e);
375 if(this.RowSelection){
376 this.fireEvent('rowdblclick', this, row, rowIndex, e);
380 sort : function(e,el)
382 var col = Roo.get(el)
384 if(!col.hasClass('sortable')){
388 var sort = col.attr('sort');
391 if(col.hasClass('glyphicon-arrow-up')){
395 this.store.sortInfo = {field : sort, direction : dir};
398 Roo.log("calling footer first");
399 this.footer.onClick('first');
402 this.store.load({ params : { start : 0 } });
406 renderHeader : function()
415 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
417 var config = cm.config[i];
422 html: cm.getColumnHeader(i)
425 if(typeof(config.hidden) != 'undefined' && config.hidden){
426 c.style += ' display:none;';
429 if(typeof(config.dataIndex) != 'undefined'){
430 c.sort = config.dataIndex;
433 if(typeof(config.sortable) != 'undefined' && config.sortable){
437 // if(typeof(config.align) != 'undefined' && config.align.length){
438 // c.style += ' text-align:' + config.align + ';';
441 if(typeof(config.width) != 'undefined'){
442 c.style += ' width:' + config.width + 'px;';
451 renderBody : function()
461 colspan : this.cm.getColumnCount()
471 renderFooter : function()
481 colspan : this.cm.getColumnCount()
495 Roo.log('ds onload');
502 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
503 e.removeClass(['glyphicon', 'glyphicon-arrow-up', 'glyphicon-arrow-down']);
505 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
506 e.addClass(['glyphicon', 'glyphicon-arrow-up']);
509 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
510 e.addClass(['glyphicon', 'glyphicon-arrow-down']);
514 var tbody = this.mainBody;
518 if(ds.getCount() > 0){
519 ds.data.each(function(d,rowIndex){
520 var row = this.renderRow(cm, ds, rowIndex);
522 tbody.createChild(row);
530 Roo.each(renders, function(r){
531 _this.renderColumn(r);
535 Roo.each(this.el.select('tbody td', true).elements, function(e){
536 e.on('mouseover', _this.onMouseover, _this);
539 Roo.each(this.el.select('tbody td', true).elements, function(e){
540 e.on('mouseout', _this.onMouseout, _this);
544 // this.maskEl.hide();
549 onUpdate : function(ds,record)
551 this.refreshRow(record);
553 onRemove : function(ds, record, index, isUpdate){
554 if(isUpdate !== true){
555 this.fireEvent("beforerowremoved", this, index, record);
557 var bt = this.mainBody.dom;
559 bt.removeChild(bt.rows[index]);
562 if(isUpdate !== true){
563 //this.stripeRows(index);
564 //this.syncRowHeights(index, index);
566 this.fireEvent("rowremoved", this, index, record);
571 refreshRow : function(record){
572 var ds = this.store, index;
573 if(typeof record == 'number'){
575 record = ds.getAt(index);
577 index = ds.indexOf(record);
579 this.insertRow(ds, index, true);
580 this.onRemove(ds, record, index+1, true);
581 //this.syncRowHeights(index, index);
583 this.fireEvent("rowupdated", this, index, record);
586 insertRow : function(dm, rowIndex, isUpdate){
589 this.fireEvent("beforerowsinserted", this, firstRow, lastRow);
591 //var s = this.getScrollState();
592 var row = this.renderRow(this.cm, this.store, rowIndex);
593 // insert before rowIndex..
594 var e = this.mainBody.createChild(row,this.getRowDom(rowIndex));
598 this.fireEvent("rowsinserted", this, firstRow, lastRow);
599 //this.syncRowHeights(firstRow, lastRow);
600 //this.stripeRows(firstRow);
607 getRowDom : function(rowIndex)
609 // not sure if I need to check this.. but let's do it anyway..
610 return (this.mainBody.dom.rows && (rowIndex-1) < this.mainBody.dom.rows.length ) ?
611 this.mainBody.dom.rows[rowIndex] : false
613 // returns the object tree for a tr..
616 renderRow : function(cm, ds, rowIndex) {
618 var d = ds.getAt(rowIndex);
627 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
628 var config = cm.config[i];
630 var renderer = cm.getRenderer(i);
634 if(typeof(renderer) !== 'undefined'){
635 value = renderer(d.data[cm.getDataIndex(i)], false, d);
638 if(typeof(value) === 'object'){
652 this.fireEvent('rowclass', this, rowcfg);
657 cls : rowcfg.rowClass,
659 html: (typeof(value) === 'object') ? '' : value
662 if(typeof(config.hidden) != 'undefined' && config.hidden){
663 td.style += ' display:none;';
666 if(typeof(config.align) != 'undefined' && config.align.length){
667 td.style += ' text-align:' + config.align + ';';
670 if(typeof(config.width) != 'undefined'){
671 td.style += ' width:' + config.width + 'px;';
683 onBeforeLoad : function()
685 //Roo.log('ds onBeforeLoad');
690 // this.maskEl.show();
696 this.el.select('tbody', true).first().dom.innerHTML = '';
699 getSelectionModel : function(){
701 this.selModel = new Roo.bootstrap.Table.RowSelectionModel();
703 return this.selModel;
706 renderColumn : function(r)
710 var t = r.cfg.render(r.container);
713 Roo.each(r.cfg.cn, function(c){
715 container: t.getChildContainer(),
718 _this.renderColumn(child);