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 row is rendered, so you can change add a style to it.
111 * @param {Roo.bootstrap.Table} this
112 * @param {Object} rowcfg contains record rowIndex colIndex and rowClass - set rowClass to add a style.
119 Roo.extend(Roo.bootstrap.Table, Roo.bootstrap.Component, {
143 RowSelection : false,
144 CellSelection : false,
148 getAutoCreate : function(){
149 var cfg = Roo.apply({}, Roo.bootstrap.Table.superclass.getAutoCreate.call(this));
158 cfg.cls += ' table-striped';
162 cfg.cls += ' table-hover';
165 cfg.cls += ' table-bordered';
167 if (this.condensed) {
168 cfg.cls += ' table-condensed';
170 if (this.responsive) {
171 cfg.cls += ' table-responsive';
175 cfg.cls+= ' ' +this.cls;
178 // this lot should be simplifed...
181 cfg.align=this.align;
184 cfg.bgcolor=this.bgcolor;
187 cfg.border=this.border;
189 if (this.cellpadding) {
190 cfg.cellpadding=this.cellpadding;
192 if (this.cellspacing) {
193 cfg.cellspacing=this.cellspacing;
196 cfg.frame=this.frame;
199 cfg.rules=this.rules;
202 cfg.sortable=this.sortable;
205 cfg.summary=this.summary;
208 cfg.width=this.width;
211 cfg.style = (typeof(cfg.style) == 'undefined') ? ('table-layout:' + this.layout + ';') : (cfg.style + ('table-layout:' + this.layout + ';'));
214 if(this.store || this.cm){
216 cfg.cn.push(this.renderHeader());
219 cfg.cn.push(this.renderBody());
222 cfg.cn.push(this.renderFooter());
225 cfg.cls+= ' TableGrid';
228 return { cn : [ cfg ] };
231 initEvents : function()
233 if(!this.store || !this.cm){
237 Roo.log('initEvents with ds!!!!');
241 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
242 e.on('click', _this.sort, _this);
245 this.el.on("click", this.onClick, this);
246 this.el.on("dblclick", this.onDblClick, this);
248 this.parent().el.setStyle('position', 'relative');
250 this.footer.parentId = this.id;
251 this.footer.onRender(this.el.select('tfoot tr td').first(), null);
254 this.maskEl = new Roo.LoadMask(this.el, { store : this.ds, msgCls: 'roo-el-mask-msg' });
256 this.store.on('load', this.onLoad, this);
257 this.store.on('beforeload', this.onBeforeLoad, this);
261 onClick : function(e, el)
263 var cell = Roo.get(el);
264 var row = cell.findParent('tr', false, true);
265 var cellIndex = cell.dom.cellIndex;
266 var rowIndex = row.dom.rowIndex;
268 if(this.CellSelection){
269 this.fireEvent('cellclick', this, cell, rowIndex, cellIndex, e);
272 if(this.RowSelection){
273 this.fireEvent('rowclick', this, row, rowIndex, e);
279 onDblClick : function(e,el)
281 var cell = Roo.get(el);;
282 var row = cell.findParent('tr', false, true);
283 var cellIndex = cell.dom.cellIndex;
284 var rowIndex = row.dom.rowIndex;
286 if(this.CellSelection){
287 this.fireEvent('celldblclick', this, cell, rowIndex, cellIndex, e);
290 if(this.RowSelection){
291 this.fireEvent('rowdblclick', this, row, rowIndex, e);
295 sort : function(e,el)
297 var col = Roo.get(el)
299 if(!col.hasClass('sortable')){
303 var sort = col.attr('sort');
306 if(col.hasClass('glyphicon-arrow-up')){
310 this.store.sortInfo = {field : sort, direction : dir};
313 Roo.log("calling footer first");
314 this.footer.onClick('first');
317 this.store.load({ params : { start : 0 } });
321 renderHeader : function()
330 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
332 var config = cm.config[i];
334 if(typeof(config.hidden) != 'undefined' && config.hidden){
341 html: cm.getColumnHeader(i)
344 if(typeof(config.dataIndex) != 'undefined'){
345 c.sort = config.dataIndex;
348 if(typeof(config.sortable) != 'undefined' && config.sortable){
352 // if(typeof(config.align) != 'undefined' && config.align.length){
353 // c.style += ' text-align:' + config.align + ';';
356 if(typeof(config.width) != 'undefined'){
357 c.style += ' width:' + config.width + 'px;';
366 renderBody : function()
376 colspan : this.cm.getColumnCount()
386 renderFooter : function()
396 colspan : this.cm.getColumnCount()
408 Roo.log('ds onload');
414 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
415 e.removeClass(['glyphicon', 'glyphicon-arrow-up', 'glyphicon-arrow-down']);
417 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
418 e.addClass(['glyphicon', 'glyphicon-arrow-up']);
421 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
422 e.addClass(['glyphicon', 'glyphicon-arrow-down']);
426 var tbody = this.el.select('tbody', true).first();
430 if(this.store.getCount() > 0){
431 this.store.data.each(function(d,rowIndex){
437 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
438 var config = cm.config[i];
440 if(typeof(config.hidden) != 'undefined' && config.hidden){
444 var renderer = cm.getRenderer(i);
448 if(typeof(renderer) !== 'undefined'){
449 value = renderer(d.data[cm.getDataIndex(i)], false, d);
452 if(typeof(value) === 'object'){
466 _this.fireEvent('rowclass', this, rowcfg);
471 cls : rowcfg.rowClass,
473 html: (typeof(value) === 'object') ? '' : value
476 if(typeof(config.align) != 'undefined' && config.align.length){
477 td.style += ' text-align:' + config.align + ';';
480 if(typeof(config.width) != 'undefined'){
481 td.style += ' width:' + config.width + 'px;';
489 tbody.createChild(row);
497 Roo.each(renders, function(r){
498 _this.renderColumn(r);
503 // this.maskEl.hide();
507 onBeforeLoad : function()
509 //Roo.log('ds onBeforeLoad');
514 // this.maskEl.show();
520 this.el.select('tbody', true).first().dom.innerHTML = '';
523 getSelectionModel : function(){
525 this.selModel = new Roo.bootstrap.Table.RowSelectionModel();
527 return this.selModel;
530 renderColumn : function(r)
534 var cn = Roo.apply(r.cfg);
536 var t = cn.render(r.container);
539 Roo.each(r.cfg.cn, function(c){
541 container: t.getChildContainer(),
544 _this.renderColumn(child);