2 * @class Roo.bootstrap.Table
4 * @extends Roo.bootstrap.Component
5 * @children Roo.bootstrap.TableBody
6 * Bootstrap Table class. This class represents the primary interface of a component based grid control.
7 * Similar to Roo.grid.Grid
9 var table = Roo.factory({
12 autoSizeColumns: true,
19 sortInfo : { direction : 'ASC', field: 'name' },
24 url : 'https://example.com/some.data.url.json'
29 fields : [ 'id', 'name', whatever' ],
36 xtype : 'ColumnModel',
40 dataIndex : 'is_in_group',
43 renderer : function(v, x , r) {
45 return String.format("{0}", v)
51 xtype : 'RowSelectionModel',
52 xns : Roo.bootstrap.Table
53 // you can add listeners to catch selection change here....
59 grid.render(Roo.get("some-div"));
62 Currently the Table uses multiple headers to try and handle XL / Medium etc... styling
67 * @cfg {Roo.grid.AbstractSelectionModel} sm The selection model to use (cell selection is not supported yet)
68 * @cfg {Roo.data.Store} store The data store to use
69 * @cfg {Roo.grid.ColumnModel} cm[] A column for the grid.
71 * @cfg {String} cls table class
74 * @cfg {string} empty_results Text to display for no results
75 * @cfg {boolean} striped Should the rows be alternative striped
76 * @cfg {boolean} bordered Add borders to the table
77 * @cfg {boolean} hover Add hover highlighting
78 * @cfg {boolean} condensed Format condensed
79 * @cfg {boolean} responsive default false - if this is on, columns are rendered with col-xs-4 etc. classes, otherwise columns will be sized by CSS,
80 * also adds table-responsive (see bootstrap docs for details)
81 * @cfg {Boolean} loadMask (true|false) default false
82 * @cfg {Boolean} footerShow (true|false) generate tfoot, default true
83 * @cfg {Boolean} summaryFooterShow (true|false) generate tfoot for summary, default false
84 * @cfg {Boolean} headerShow (true|false) generate thead, default true
85 * @cfg {Boolean} rowSelection (true|false) default false
86 * @cfg {Boolean} cellSelection (true|false) default false
87 * @cfg {Boolean} scrollBody (true|false) default false - body scrolled / fixed header (with resizable columns)
88 * @cfg {Roo.bootstrap.PagingToolbar} footer a paging toolbar
89 * @cfg {Boolean} lazyLoad auto load data while scrolling to the end (default false)
90 * @cfg {Boolean} auto_hide_footer auto hide footer if only one page (default false)
91 * @cfg {Boolean} enableColumnResize default true if columns can be resized = needs scrollBody to be set to work (drag/drop)
92 * @cfg {Boolean} disableAutoSize disable autoSize() and initCSS()
95 * @cfg {Number} minColumnWidth default 50 pixels minimum column width
99 * @param {Object} config The config object
102 Roo.bootstrap.Table = function(config)
104 Roo.bootstrap.Table.superclass.constructor.call(this, config);
107 this.rowSelection = (typeof(config.rowSelection) != 'undefined') ? config.rowSelection : this.rowSelection;
108 this.cellSelection = (typeof(config.cellSelection) != 'undefined') ? config.cellSelection : this.cellSelection;
109 this.headerShow = (typeof(config.thead) != 'undefined') ? config.thead : this.headerShow;
110 this.footerShow = (typeof(config.tfoot) != 'undefined') ? config.tfoot : this.footerShow;
112 this.view = this; // compat with grid.
114 this.sm = this.sm || {xtype: 'RowSelectionModel'};
117 this.selModel = Roo.factory(this.sm, Roo.grid);
118 this.sm = this.selModel;
119 this.sm.xmodule = this.xmodule || false;
122 if (this.cm && typeof(this.cm.config) == 'undefined') {
123 this.colModel = new Roo.grid.ColumnModel(this.cm);
124 this.cm = this.colModel;
125 this.cm.xmodule = this.xmodule || false;
128 this.store= Roo.factory(this.store, Roo.data);
129 this.ds = this.store;
130 this.ds.xmodule = this.xmodule || false;
133 if (this.footer && this.store) {
134 this.footer.dataSource = this.ds;
135 this.footer = Roo.factory(this.footer);
142 * Fires when a cell is clicked
143 * @param {Roo.bootstrap.Table} this
144 * @param {Roo.Element} el
145 * @param {Number} rowIndex
146 * @param {Number} columnIndex
147 * @param {Roo.EventObject} e
151 * @event celldblclick
152 * Fires when a cell is double clicked
153 * @param {Roo.bootstrap.Table} this
154 * @param {Roo.Element} el
155 * @param {Number} rowIndex
156 * @param {Number} columnIndex
157 * @param {Roo.EventObject} e
159 "celldblclick" : true,
162 * Fires when a row is clicked
163 * @param {Roo.bootstrap.Table} this
164 * @param {Roo.Element} el
165 * @param {Number} rowIndex
166 * @param {Roo.EventObject} e
171 * Fires when a row is double clicked
172 * @param {Roo.bootstrap.Table} this
173 * @param {Roo.Element} el
174 * @param {Number} rowIndex
175 * @param {Roo.EventObject} e
177 "rowdblclick" : true,
180 * Fires when a mouseover occur
181 * @param {Roo.bootstrap.Table} this
182 * @param {Roo.Element} el
183 * @param {Number} rowIndex
184 * @param {Number} columnIndex
185 * @param {Roo.EventObject} e
190 * Fires when a mouseout occur
191 * @param {Roo.bootstrap.Table} this
192 * @param {Roo.Element} el
193 * @param {Number} rowIndex
194 * @param {Number} columnIndex
195 * @param {Roo.EventObject} e
200 * Fires when a row is rendered, so you can change add a style to it.
201 * @param {Roo.bootstrap.Table} this
202 * @param {Object} rowcfg contains record rowIndex colIndex and rowClass - set rowClass to add a style.
206 * @event rowsrendered
207 * Fires when all the rows have been rendered
208 * @param {Roo.bootstrap.Table} this
210 'rowsrendered' : true,
213 * The raw contextmenu event for the entire grid.
214 * @param {Roo.EventObject} e
216 "contextmenu" : true,
218 * @event rowcontextmenu
219 * Fires when a row is right clicked
220 * @param {Roo.bootstrap.Table} this
221 * @param {Number} rowIndex
222 * @param {Roo.EventObject} e
224 "rowcontextmenu" : true,
226 * @event cellcontextmenu
227 * Fires when a cell is right clicked
228 * @param {Roo.bootstrap.Table} this
229 * @param {Number} rowIndex
230 * @param {Number} cellIndex
231 * @param {Roo.EventObject} e
233 "cellcontextmenu" : true,
235 * @event headercontextmenu
236 * Fires when a header is right clicked
237 * @param {Roo.bootstrap.Table} this
238 * @param {Number} columnIndex
239 * @param {Roo.EventObject} e
241 "headercontextmenu" : true,
244 * The raw mousedown event for the entire grid.
245 * @param {Roo.EventObject} e
252 Roo.extend(Roo.bootstrap.Table, Roo.bootstrap.Component, {
269 enableColumnResize: true,
270 disableAutoSize: false,
272 rowSelection : false,
273 cellSelection : false,
278 // Roo.Element - the tbody
279 bodyEl: false, // <tbody> Roo.Element - thead element
280 headEl: false, // <thead> Roo.Element - thead element
281 resizeProxy : false, // proxy element for dragging?
285 container: false, // used by gridpanel...
291 auto_hide_footer : false,
293 view: false, // actually points to this..
295 getAutoCreate : function()
297 var cfg = Roo.apply({}, Roo.bootstrap.Table.superclass.getAutoCreate.call(this));
304 // this get's auto added by panel.Grid
305 if (this.scrollBody) {
306 cfg.cls += ' table-body-fixed';
309 cfg.cls += ' table-striped';
313 cfg.cls += ' table-hover';
316 cfg.cls += ' table-bordered';
318 if (this.condensed) {
319 cfg.cls += ' table-condensed';
322 if (this.responsive) {
323 cfg.cls += ' table-responsive';
327 cfg.cls+= ' ' +this.cls;
333 cfg.style = (typeof(cfg.style) == 'undefined') ? ('table-layout:' + this.layout + ';') : (cfg.style + ('table-layout:' + this.layout + ';'));
336 if(this.store || this.cm){
338 cfg.cn.push(this.renderHeader());
341 cfg.cn.push(this.renderBody());
344 cfg.cn.push(this.renderFooter());
347 if(!this.footerShow && this.summaryFooterShow) {
348 cfg.cn.push(this.rendersummaryFooter());
350 // where does this come from?
351 //cfg.cls+= ' TableGrid';
354 return { cn : [ cfg ] };
357 initEvents : function()
359 if(!this.store || !this.cm){
363 this.selModel.initEvents();
367 //Roo.log('initEvents with ds!!!!');
369 this.bodyEl = this.el.select('tbody', true).first();
370 this.headEl = this.el.select('thead', true).first();
371 this.mainFoot = this.el.select('tfoot', true).first();
376 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
377 e.on('click', this.sort, this);
381 // why is this done????? = it breaks dialogs??
382 //this.parent().el.setStyle('position', 'relative');
386 this.footer.parentId = this.id;
387 this.footer.onRender(this.el.select('tfoot tr td').first(), null);
390 this.el.select('tfoot tr td').first().addClass('hide');
395 this.maskEl = new Roo.LoadMask(this.el, { store : this.ds, msgCls: 'roo-el-mask-msg' });
398 this.store.on('load', this.onLoad, this);
399 this.store.on('beforeload', this.onBeforeLoad, this);
400 this.store.on('update', this.onUpdate, this);
401 this.store.on('add', this.onAdd, this);
402 this.store.on("clear", this.clear, this);
404 this.el.on("contextmenu", this.onContextMenu, this);
407 this.cm.on("headerchange", this.onHeaderChange, this);
408 this.cm.on("hiddenchange", this.onHiddenChange, this, arguments);
410 //?? does bodyEl get replaced on render?
411 this.bodyEl.on("click", this.onClick, this);
412 this.bodyEl.on("dblclick", this.onDblClick, this);
413 this.bodyEl.on('scroll', this.onBodyScroll, this);
415 // guessing mainbody will work - this relays usually caught by selmodel at present.
416 this.relayEvents(this.bodyEl, ["mousedown","mouseup","mouseover","mouseout","keypress"]);
419 this.resizeProxy = Roo.get(document.body).createChild({ cls:"x-grid-resize-proxy", html: ' ' });
422 if(this.headEl && this.enableColumnResize !== false && Roo.grid.SplitDragZone){
423 new Roo.grid.SplitDragZone(this, this.headEl.dom, false); // not sure what 'lockedHd is for this implementation..)
428 // Compatibility with grid - we implement all the view features at present.
436 if(this.disableAutoSize) {
440 var cm = this.cm, styles = [];
441 this.CSS.removeStyleSheet(this.id + '-cssrules');
442 var headHeight = this.headEl ? this.headEl.dom.clientHeight : 0;
443 // we can honour xs/sm/md/xl as widths...
444 // we first have to decide what widht we are currently at...
445 var sz = Roo.getGridSize();
449 var cols = []; // visable cols.
451 for(var i = 0, len = cm.getColumnCount(); i < len; i++) {
452 var w = cm.getColumnWidth(i, false);
454 cols.push( { rel : false, abs : 0 });
458 cols.push( { rel : false, abs : w });
460 last = i; // not really..
463 var w = cm.getColumnWidth(i, sz);
468 cols.push( { rel : w, abs : false });
471 var avail = this.bodyEl.dom.clientWidth - total_abs;
473 var unitWidth = Math.floor(avail / total);
474 var rem = avail - (unitWidth * total);
476 var hidden, width, pos = 0 , splithide , left;
477 for(var i = 0, len = cm.getColumnCount(); i < len; i++) {
479 hidden = 'display:none;';
481 width = 'width:0px;';
487 // we can honour xs/sm/md/xl ?
488 var w = cols[i].rel == false ? cols[i].abs : (cols[i].rel * unitWidth);
490 hidden = 'display:none;';
492 // width should return a small number...
494 w+=rem; // add the remaining with..
497 left = "left:" + (pos -4) + "px;";
498 width = "width:" + w+ "px;";
501 if (this.responsive) {
504 hidden = cm.isHidden(i) ? 'display:none;' : '';
505 splithide = 'display: none;';
508 styles.push( '#' , this.id , ' .x-col-' , i, " {", cm.config[i].css, width, hidden, "}\n" );
511 splithide = 'display:none;';
514 styles.push('#' , this.id , ' .x-hcol-' , i, " { ", width, hidden," }\n",
515 '#' , this.id , ' .x-grid-split-' , i, " { ", left, splithide, 'height:', (headHeight - 4), "px;}\n",
516 // this is the popover version..
517 '.popover-inner #' , this.id , ' .x-grid-split-' , i, " { ", left, splithide, 'height:', 100, "%;}\n"
522 //Roo.log(styles.join(''));
523 this.CSS.createStyleSheet( styles.join(''), this.id + '-cssrules');
529 onContextMenu : function(e, t)
531 this.processEvent("contextmenu", e);
534 processEvent : function(name, e)
536 if (name != 'touchstart' ) {
537 this.fireEvent(name, e);
540 var t = e.getTarget();
542 var cell = Roo.get(t);
548 if(cell.findParent('tfoot', false, true)){
552 if(cell.findParent('thead', false, true)){
554 if(e.getTarget().nodeName.toLowerCase() != 'th'){
555 cell = Roo.get(t).findParent('th', false, true);
557 Roo.log("failed to find th in thead?");
558 Roo.log(e.getTarget());
563 var cellIndex = cell.dom.cellIndex;
565 var ename = name == 'touchstart' ? 'click' : name;
566 this.fireEvent("header" + ename, this, cellIndex, e);
571 if(e.getTarget().nodeName.toLowerCase() != 'td'){
572 cell = Roo.get(t).findParent('td', false, true);
574 Roo.log("failed to find th in tbody?");
575 Roo.log(e.getTarget());
580 var row = cell.findParent('tr', false, true);
581 var cellIndex = cell.dom.cellIndex;
582 var rowIndex = row.dom.rowIndex - 1;
586 this.fireEvent("row" + name, this, rowIndex, e);
590 this.fireEvent("cell" + name, this, rowIndex, cellIndex, e);
596 onMouseover : function(e, el)
598 var cell = Roo.get(el);
604 if(e.getTarget().nodeName.toLowerCase() != 'td'){
605 cell = cell.findParent('td', false, true);
608 var row = cell.findParent('tr', false, true);
609 var cellIndex = cell.dom.cellIndex;
610 var rowIndex = row.dom.rowIndex - 1; // start from 0
612 this.fireEvent('mouseover', this, cell, rowIndex, cellIndex, e);
616 onMouseout : function(e, el)
618 var cell = Roo.get(el);
624 if(e.getTarget().nodeName.toLowerCase() != 'td'){
625 cell = cell.findParent('td', false, true);
628 var row = cell.findParent('tr', false, true);
629 var cellIndex = cell.dom.cellIndex;
630 var rowIndex = row.dom.rowIndex - 1; // start from 0
632 this.fireEvent('mouseout', this, cell, rowIndex, cellIndex, e);
636 onClick : function(e, el)
638 var cell = Roo.get(el);
640 if(!cell || (!this.cellSelection && !this.rowSelection)){
644 if(e.getTarget().nodeName.toLowerCase() != 'td'){
645 cell = cell.findParent('td', false, true);
648 if(!cell || typeof(cell) == 'undefined'){
652 var row = cell.findParent('tr', false, true);
654 if(!row || typeof(row) == 'undefined'){
658 var cellIndex = cell.dom.cellIndex;
659 var rowIndex = this.getRowIndex(row);
661 // why??? - should these not be based on SelectionModel?
662 //if(this.cellSelection){
663 this.fireEvent('cellclick', this, cell, rowIndex, cellIndex, e);
666 //if(this.rowSelection){
667 this.fireEvent('rowclick', this, row, rowIndex, e);
672 onDblClick : function(e,el)
674 var cell = Roo.get(el);
676 if(!cell || (!this.cellSelection && !this.rowSelection)){
680 if(e.getTarget().nodeName.toLowerCase() != 'td'){
681 cell = cell.findParent('td', false, true);
684 if(!cell || typeof(cell) == 'undefined'){
688 var row = cell.findParent('tr', false, true);
690 if(!row || typeof(row) == 'undefined'){
694 var cellIndex = cell.dom.cellIndex;
695 var rowIndex = this.getRowIndex(row);
697 if(this.cellSelection){
698 this.fireEvent('celldblclick', this, cell, rowIndex, cellIndex, e);
701 if(this.rowSelection){
702 this.fireEvent('rowdblclick', this, row, rowIndex, e);
705 findRowIndex : function(el)
707 var cell = Roo.get(el);
711 var row = cell.findParent('tr', false, true);
713 if(!row || typeof(row) == 'undefined'){
716 return this.getRowIndex(row);
718 sort : function(e,el)
720 var col = Roo.get(el);
722 if(!col.hasClass('sortable')){
726 var sort = col.attr('sort');
729 if(col.select('i', true).first().hasClass('fa-arrow-up')){
733 this.store.sortInfo = {field : sort, direction : dir};
736 Roo.log("calling footer first");
737 this.footer.onClick('first');
740 this.store.load({ params : { start : 0 } });
744 renderHeader : function()
754 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
756 var config = cm.config[i];
763 html: cm.getColumnHeader(i)
766 var tooltip = cm.getColumnTooltip(i);
774 if(typeof(config.sortable) != 'undefined' && config.sortable){
775 c.cls += ' sortable';
776 c.html = '<i class="fa"></i>' + c.html;
779 // could use BS4 hidden-..-down
781 if(typeof(config.lgHeader) != 'undefined'){
782 hh += '<span class="hidden-xs hidden-sm hidden-md ">' + config.lgHeader + '</span>';
785 if(typeof(config.mdHeader) != 'undefined'){
786 hh += '<span class="hidden-xs hidden-sm hidden-lg">' + config.mdHeader + '</span>';
789 if(typeof(config.smHeader) != 'undefined'){
790 hh += '<span class="hidden-xs hidden-md hidden-lg">' + config.smHeader + '</span>';
793 if(typeof(config.xsHeader) != 'undefined'){
794 hh += '<span class="hidden-sm hidden-md hidden-lg">' + config.xsHeader + '</span>';
801 if(typeof(config.tooltip) != 'undefined'){
802 c.tooltip = config.tooltip;
805 if(typeof(config.colspan) != 'undefined'){
806 c.colspan = config.colspan;
809 // hidden is handled by CSS now
811 if(typeof(config.dataIndex) != 'undefined'){
812 c.sort = config.dataIndex;
817 if(typeof(config.align) != 'undefined' && config.align.length){
818 c.style += ' text-align:' + config.align + ';';
821 /* width is done in CSS
822 *if(typeof(config.width) != 'undefined'){
823 c.style += ' width:' + config.width + 'px;';
824 this.totalWidth += config.width;
826 this.totalWidth += 100; // assume minimum of 100 per column?
830 if(typeof(config.cls) != 'undefined'){
831 c.cls = (typeof(c.cls) == 'undefined') ? config.cls : (c.cls + ' ' + config.cls);
833 // this is the bit that doesnt reall work at all...
835 if (this.responsive) {
838 ['xs','sm','md','lg'].map(function(size){
840 if(typeof(config[size]) == 'undefined'){
844 if (!config[size]) { // 0 = hidden
845 // BS 4 '0' is treated as hide that column and below.
846 c.cls += ' hidden-' + size + ' hidden' + size + '-down';
850 c.cls += ' col-' + size + '-' + config[size] + (
851 size == 'xs' ? (' col-' + config[size] ) : '' // bs4 col-{num} replaces col-xs
859 c.html +=' <span class="x-grid-split x-grid-split-' + i + '"></span>';
870 renderBody : function()
880 colspan : this.cm.getColumnCount()
890 renderFooter : function()
900 colspan : this.cm.getColumnCount()
914 // Roo.log('ds onload');
921 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
922 e.select('i', true).removeClass(['fa-arrow-up', 'fa-arrow-down']);
923 if (_this.store.sortInfo) {
925 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
926 e.select('i', true).addClass(['fa-arrow-up']);
929 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
930 e.select('i', true).addClass(['fa-arrow-down']);
935 var tbody = this.bodyEl;
937 if(ds.getCount() > 0){
938 ds.data.each(function(d,rowIndex){
939 var row = this.renderRow(cm, ds, rowIndex);
941 tbody.createChild(row);
945 if(row.cellObjects.length){
946 Roo.each(row.cellObjects, function(r){
947 _this.renderCellObject(r);
952 } else if (this.empty_results.length) {
953 this.el.mask(this.empty_results, 'no-spinner');
956 var tfoot = this.el.select('tfoot', true).first();
958 if(this.footerShow && this.auto_hide_footer && this.mainFoot){
960 this.mainFoot.setVisibilityMode(Roo.Element.DISPLAY).hide();
962 var total = this.ds.getTotalCount();
964 if(this.footer.pageSize < total){
965 this.mainFoot.show();
969 Roo.each(this.el.select('tbody td', true).elements, function(e){
970 e.on('mouseover', _this.onMouseover, _this);
973 Roo.each(this.el.select('tbody td', true).elements, function(e){
974 e.on('mouseout', _this.onMouseout, _this);
976 this.fireEvent('rowsrendered', this);
980 this.initCSS(); /// resize cols
986 onUpdate : function(ds,record)
988 this.refreshRow(record);
992 onRemove : function(ds, record, index, isUpdate){
993 if(isUpdate !== true){
994 this.fireEvent("beforerowremoved", this, index, record);
996 var bt = this.bodyEl.dom;
998 var rows = this.el.select('tbody > tr', true).elements;
1000 if(typeof(rows[index]) != 'undefined'){
1001 bt.removeChild(rows[index].dom);
1004 // if(bt.rows[index]){
1005 // bt.removeChild(bt.rows[index]);
1008 if(isUpdate !== true){
1009 //this.stripeRows(index);
1010 //this.syncRowHeights(index, index);
1012 this.fireEvent("rowremoved", this, index, record);
1016 onAdd : function(ds, records, rowIndex)
1018 //Roo.log('on Add called');
1019 // - note this does not handle multiple adding very well..
1020 var bt = this.bodyEl.dom;
1021 for (var i =0 ; i < records.length;i++) {
1022 //Roo.log('call insert row Add called on ' + rowIndex + ':' + i);
1023 //Roo.log(records[i]);
1024 //Roo.log(this.store.getAt(rowIndex+i));
1025 this.insertRow(this.store, rowIndex + i, false);
1032 refreshRow : function(record){
1033 var ds = this.store, index;
1034 if(typeof record == 'number'){
1036 record = ds.getAt(index);
1038 index = ds.indexOf(record);
1040 return; // should not happen - but seems to
1043 this.insertRow(ds, index, true);
1045 this.onRemove(ds, record, index+1, true);
1047 //this.syncRowHeights(index, index);
1049 this.fireEvent("rowupdated", this, index, record);
1051 // private - called by RowSelection
1052 onRowSelect : function(rowIndex){
1053 var row = this.getRowDom(rowIndex);
1054 row.addClass(['bg-info','info']);
1056 // private - called by RowSelection
1057 onRowDeselect : function(rowIndex)
1062 var row = this.getRowDom(rowIndex);
1063 row.removeClass(['bg-info','info']);
1066 * Focuses the specified row.
1067 * @param {Number} row The row index
1069 focusRow : function(row)
1071 //Roo.log('GridView.focusRow');
1072 var x = this.bodyEl.dom.scrollLeft;
1073 this.focusCell(row, 0, false);
1074 this.bodyEl.dom.scrollLeft = x;
1078 * Focuses the specified cell.
1079 * @param {Number} row The row index
1080 * @param {Number} col The column index
1081 * @param {Boolean} hscroll false to disable horizontal scrolling
1083 focusCell : function(row, col, hscroll)
1085 //Roo.log('GridView.focusCell');
1086 var el = this.ensureVisible(row, col, hscroll);
1087 // not sure what focusEL achives = it's a <a> pos relative
1088 //this.focusEl.alignTo(el, "tl-tl");
1090 // this.focusEl.focus();
1092 // this.focusEl.focus.defer(1, this.focusEl);
1097 * Scrolls the specified cell into view
1098 * @param {Number} row The row index
1099 * @param {Number} col The column index
1100 * @param {Boolean} hscroll false to disable horizontal scrolling
1102 ensureVisible : function(row, col, hscroll)
1104 //Roo.log('GridView.ensureVisible,' + row + ',' + col);
1105 //return null; //disable for testing.
1106 if(typeof row != "number"){
1109 if(row < 0 && row >= this.ds.getCount()){
1112 col = (col !== undefined ? col : 0);
1114 while(cm.isHidden(col)){
1118 var el = this.getCellDom(row, col);
1122 var c = this.bodyEl.dom;
1124 var ctop = parseInt(el.offsetTop, 10);
1125 var cleft = parseInt(el.offsetLeft, 10);
1126 var cbot = ctop + el.offsetHeight;
1127 var cright = cleft + el.offsetWidth;
1129 //var ch = c.clientHeight - this.mainHd.dom.offsetHeight;
1130 var ch = 0; //?? header is not withing the area?
1131 var stop = parseInt(c.scrollTop, 10);
1132 var sleft = parseInt(c.scrollLeft, 10);
1133 var sbot = stop + ch;
1134 var sright = sleft + c.clientWidth;
1136 Roo.log('GridView.ensureVisible:' +
1138 ' c.clientHeight:' + c.clientHeight +
1139 ' this.mainHd.dom.offsetHeight:' + this.mainHd.dom.offsetHeight +
1148 //Roo.log("set scrolltop to ctop DISABLE?");
1149 }else if(cbot > sbot){
1150 //Roo.log("set scrolltop to cbot-ch");
1151 c.scrollTop = cbot-ch;
1154 if(hscroll !== false){
1156 c.scrollLeft = cleft;
1157 }else if(cright > sright){
1158 c.scrollLeft = cright-c.clientWidth;
1166 insertRow : function(dm, rowIndex, isUpdate){
1169 this.fireEvent("beforerowsinserted", this, rowIndex);
1171 //var s = this.getScrollState();
1172 var row = this.renderRow(this.cm, this.store, rowIndex);
1173 // insert before rowIndex..
1174 var e = this.bodyEl.createChild(row,this.getRowDom(rowIndex));
1178 if(row.cellObjects.length){
1179 Roo.each(row.cellObjects, function(r){
1180 _this.renderCellObject(r);
1185 this.fireEvent("rowsinserted", this, rowIndex);
1186 //this.syncRowHeights(firstRow, lastRow);
1187 //this.stripeRows(firstRow);
1194 getRowDom : function(rowIndex)
1196 var rows = this.el.select('tbody > tr', true).elements;
1198 return (typeof(rows[rowIndex]) == 'undefined') ? false : rows[rowIndex];
1201 getCellDom : function(rowIndex, colIndex)
1203 var row = this.getRowDom(rowIndex);
1204 if (row === false) {
1207 var cols = row.select('td', true).elements;
1208 return (typeof(cols[colIndex]) == 'undefined') ? false : cols[colIndex];
1212 // returns the object tree for a tr..
1215 renderRow : function(cm, ds, rowIndex)
1217 var d = ds.getAt(rowIndex);
1221 cls : 'x-row-' + rowIndex,
1225 var cellObjects = [];
1227 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
1228 var config = cm.config[i];
1230 var renderer = cm.getRenderer(i);
1234 if(typeof(renderer) !== 'undefined'){
1235 value = renderer(d.data[cm.getDataIndex(i)], false, d);
1237 // if object are returned, then they are expected to be Roo.bootstrap.Component instances
1238 // and are rendered into the cells after the row is rendered - using the id for the element.
1240 if(typeof(value) === 'object'){
1250 rowIndex : rowIndex,
1255 this.fireEvent('rowclass', this, rowcfg);
1259 // this might end up displaying HTML?
1260 // this is too messy... - better to only do it on columsn you know are going to be too long
1261 //tooltip : (typeof(value) === 'object') ? '' : value,
1262 cls : rowcfg.rowClass + ' x-col-' + i,
1264 html: (typeof(value) === 'object') ? '' : value
1271 if(typeof(config.colspan) != 'undefined'){
1272 td.colspan = config.colspan;
1277 if(typeof(config.align) != 'undefined' && config.align.length){
1278 td.style += ' text-align:' + config.align + ';';
1280 if(typeof(config.valign) != 'undefined' && config.valign.length){
1281 td.style += ' vertical-align:' + config.valign + ';';
1284 if(typeof(config.width) != 'undefined'){
1285 td.style += ' width:' + config.width + 'px;';
1289 if(typeof(config.cursor) != 'undefined'){
1290 td.style += ' cursor:' + config.cursor + ';';
1293 if(typeof(config.cls) != 'undefined'){
1294 td.cls = (typeof(td.cls) == 'undefined') ? config.cls : (td.cls + ' ' + config.cls);
1296 if (this.responsive) {
1297 ['xs','sm','md','lg'].map(function(size){
1299 if(typeof(config[size]) == 'undefined'){
1305 if (!config[size]) { // 0 = hidden
1306 // BS 4 '0' is treated as hide that column and below.
1307 td.cls += ' hidden-' + size + ' hidden' + size + '-down';
1311 td.cls += ' col-' + size + '-' + config[size] + (
1312 size == 'xs' ? (' col-' + config[size] ) : '' // bs4 col-{num} replaces col-xs
1322 row.cellObjects = cellObjects;
1330 onBeforeLoad : function()
1332 this.el.unmask(); // if needed.
1339 this.el.select('tbody', true).first().dom.innerHTML = '';
1342 * Show or hide a row.
1343 * @param {Number} rowIndex to show or hide
1344 * @param {Boolean} state hide
1346 setRowVisibility : function(rowIndex, state)
1348 var bt = this.bodyEl.dom;
1350 var rows = this.el.select('tbody > tr', true).elements;
1352 if(typeof(rows[rowIndex]) == 'undefined'){
1355 rows[rowIndex][ state ? 'removeClass' : 'addClass']('d-none');
1360 getSelectionModel : function(){
1362 this.selModel = new Roo.bootstrap.Table.RowSelectionModel({grid: this});
1364 return this.selModel;
1367 * Render the Roo.bootstrap object from renderder
1369 renderCellObject : function(r)
1373 r.cfg.parentId = (typeof(r.container) == 'string') ? r.container : r.container.id;
1375 var t = r.cfg.render(r.container);
1378 Roo.each(r.cfg.cn, function(c){
1380 container: t.getChildContainer(),
1383 _this.renderCellObject(child);
1388 * get the Row Index from a dom element.
1389 * @param {Roo.Element} row The row to look for
1390 * @returns {Number} the row
1392 getRowIndex : function(row)
1396 Roo.each(this.el.select('tbody > tr', true).elements, function(el, index){
1407 * get the header TH element for columnIndex
1408 * @param {Number} columnIndex
1409 * @returns {Roo.Element}
1411 getHeaderIndex: function(colIndex)
1413 var cols = this.headEl.select('th', true).elements;
1414 return cols[colIndex];
1417 * get the Column Index from a dom element. (using regex on x-hcol-{colid})
1418 * @param {domElement} cell to look for
1419 * @returns {Number} the column
1421 getCellIndex : function(cell)
1423 var id = String(cell.className).match(Roo.bootstrap.Table.cellRE);
1425 return parseInt(id[1], 10);
1430 * Returns the grid's underlying element = used by panel.Grid
1431 * @return {Element} The element
1433 getGridEl : function(){
1437 * Forces a resize - used by panel.Grid
1438 * @return {Element} The element
1440 autoSize : function()
1442 if(this.disableAutoSize) {
1445 //var ctr = Roo.get(this.container.dom.parentElement);
1446 var ctr = Roo.get(this.el.dom);
1448 var thd = this.getGridEl().select('thead',true).first();
1449 var tbd = this.getGridEl().select('tbody', true).first();
1450 var tfd = this.getGridEl().select('tfoot', true).first();
1452 var cw = ctr.getWidth();
1453 this.getGridEl().select('tfoot tr, tfoot td',true).setWidth(cw);
1457 tbd.setWidth(ctr.getWidth());
1458 // if the body has a max height - and then scrolls - we should perhaps set up the height here
1459 // this needs fixing for various usage - currently only hydra job advers I think..
1461 // ctr.getHeight() - ((thd ? thd.getHeight() : 0) + (tfd ? tfd.getHeight() : 0))
1463 var barsize = (tbd.dom.offsetWidth - tbd.dom.clientWidth);
1466 cw = Math.max(cw, this.totalWidth);
1467 this.getGridEl().select('tbody tr',true).setWidth(cw);
1470 // resize 'expandable coloumn?
1472 return; // we doe not have a view in this design..
1475 onBodyScroll: function()
1477 //Roo.log("body scrolled');" + this.bodyEl.dom.scrollLeft);
1479 this.headEl.setStyle({
1480 'position' : 'relative',
1481 'left': (-1* this.bodyEl.dom.scrollLeft) + 'px'
1487 var scrollHeight = this.bodyEl.dom.scrollHeight;
1489 var scrollTop = Math.ceil(this.bodyEl.getScroll().top);
1491 var height = this.bodyEl.getHeight();
1493 if(scrollHeight - height == scrollTop) {
1495 var total = this.ds.getTotalCount();
1497 if(this.footer.cursor + this.footer.pageSize < total){
1499 this.footer.ds.load({
1501 start : this.footer.cursor + this.footer.pageSize,
1502 limit : this.footer.pageSize
1511 onColumnSplitterMoved : function(i, diff)
1513 this.userResized = true;
1515 var cm = this.colModel;
1517 var w = this.getHeaderIndex(i).getWidth() + diff;
1520 cm.setColumnWidth(i, w, true);
1522 //var cid = cm.getColumnId(i); << not used in this version?
1523 /* Roo.log(['#' + this.id + ' .x-col-' + i, "width", w + "px"]);
1525 this.CSS.updateRule( '#' + this.id + ' .x-col-' + i, "width", w + "px");
1526 this.CSS.updateRule('#' + this.id + ' .x-hcol-' + i, "width", w + "px");
1527 this.CSS.updateRule('#' + this.id + ' .x-grid-split-' + i, "left", w + "px");
1529 //this.updateSplitters();
1530 //this.layout(); << ??
1531 this.fireEvent("columnresize", i, w);
1533 onHeaderChange : function()
1535 var header = this.renderHeader();
1536 var table = this.el.select('table', true).first();
1538 this.headEl.remove();
1539 this.headEl = table.createChild(header, this.bodyEl, false);
1541 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
1542 e.on('click', this.sort, this);
1545 if(this.enableColumnResize !== false && Roo.grid.SplitDragZone){
1546 new Roo.grid.SplitDragZone(this, this.headEl.dom, false); // not sure what 'lockedHd is for this implementation..)
1551 onHiddenChange : function(colModel, colIndex, hidden)
1555 var thSelector = '#' + this.id + ' .x-hcol-' + colIndex;
1556 var tdSelector = '#' + this.id + ' .x-col-' + colIndex;
1558 this.CSS.updateRule(thSelector, "display", "");
1559 this.CSS.updateRule(tdSelector, "display", "");
1562 this.CSS.updateRule(thSelector, "display", "none");
1563 this.CSS.updateRule(tdSelector, "display", "none");
1566 // onload calls initCSS()
1567 this.onHeaderChange();
1571 setColumnWidth: function(col_index, width)
1573 // width = "md-2 xs-2..."
1574 if(!this.colModel.config[col_index]) {
1578 var w = width.split(" ");
1580 var rows = this.el.dom.getElementsByClassName("x-col-"+col_index);
1582 var h_row = this.el.dom.getElementsByClassName("x-hcol-"+col_index);
1585 for(var j = 0; j < w.length; j++) {
1591 var size_cls = w[j].split("-");
1593 if(!Number.isInteger(size_cls[1] * 1)) {
1597 if(!this.colModel.config[col_index][size_cls[0]]) {
1601 if(!h_row[0].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1605 h_row[0].classList.replace(
1606 "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1607 "col-"+size_cls[0]+"-"+size_cls[1]
1610 for(var i = 0; i < rows.length; i++) {
1612 var size_cls = w[j].split("-");
1614 if(!Number.isInteger(size_cls[1] * 1)) {
1618 if(!this.colModel.config[col_index][size_cls[0]]) {
1622 if(!rows[i].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1626 rows[i].classList.replace(
1627 "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1628 "col-"+size_cls[0]+"-"+size_cls[1]
1632 this.colModel.config[col_index][size_cls[0]] = size_cls[1];
1637 // currently only used to find the split on drag..
1638 Roo.bootstrap.Table.cellRE = /(?:.*?)x-grid-(?:hd|cell|split)-([\d]+)(?:.*?)/;
1643 Roo.bootstrap.Table.AbstractSelectionModel = Roo.grid.AbstractSelectionModel;
1644 Roo.bootstrap.Table.RowSelectionModel = Roo.grid.RowSelectionModel;