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 {boolean} striped Should the rows be alternative striped
75 * @cfg {boolean} bordered Add borders to the table
76 * @cfg {boolean} hover Add hover highlighting
77 * @cfg {boolean} condensed Format condensed
78 * @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,
79 * also adds table-responsive (see bootstrap docs for details)
80 * @cfg {Boolean} loadMask (true|false) default false
81 * @cfg {Boolean} footerShow (true|false) generate tfoot, default true
82 * @cfg {Boolean} headerShow (true|false) generate thead, default true
83 * @cfg {Boolean} rowSelection (true|false) default false
84 * @cfg {Boolean} cellSelection (true|false) default false
85 * @cfg {Boolean} scrollBody (true|false) default false - body scrolled / fixed header
86 * @cfg {Roo.bootstrap.PagingToolbar} footer a paging toolbar
87 * @cfg {Boolean} lazyLoad auto load data while scrolling to the end (default false)
88 * @cfg {Boolean} auto_hide_footer auto hide footer if only one page (default false)
89 * @cfg {Boolean} enableColumnResize default true if columns can be resized (drag/drop)
90 * @cfg {Number} minColumnWidth default 50 pixels minimum column width
94 * @param {Object} config The config object
97 Roo.bootstrap.Table = function(config)
99 Roo.bootstrap.Table.superclass.constructor.call(this, config);
102 this.rowSelection = (typeof(config.rowSelection) != 'undefined') ? config.rowSelection : this.rowSelection;
103 this.cellSelection = (typeof(config.cellSelection) != 'undefined') ? config.cellSelection : this.cellSelection;
104 this.headerShow = (typeof(config.thead) != 'undefined') ? config.thead : this.headerShow;
105 this.footerShow = (typeof(config.tfoot) != 'undefined') ? config.tfoot : this.footerShow;
107 this.view = this; // compat with grid.
109 this.sm = this.sm || {xtype: 'RowSelectionModel'};
112 this.selModel = Roo.factory(this.sm, Roo.grid);
113 this.sm = this.selModel;
114 this.sm.xmodule = this.xmodule || false;
117 if (this.cm && typeof(this.cm.config) == 'undefined') {
118 this.colModel = new Roo.grid.ColumnModel(this.cm);
119 this.cm = this.colModel;
120 this.cm.xmodule = this.xmodule || false;
123 this.store= Roo.factory(this.store, Roo.data);
124 this.ds = this.store;
125 this.ds.xmodule = this.xmodule || false;
128 if (this.footer && this.store) {
129 this.footer.dataSource = this.ds;
130 this.footer = Roo.factory(this.footer);
137 * Fires when a cell is clicked
138 * @param {Roo.bootstrap.Table} this
139 * @param {Roo.Element} el
140 * @param {Number} rowIndex
141 * @param {Number} columnIndex
142 * @param {Roo.EventObject} e
146 * @event celldblclick
147 * Fires when a cell is double clicked
148 * @param {Roo.bootstrap.Table} this
149 * @param {Roo.Element} el
150 * @param {Number} rowIndex
151 * @param {Number} columnIndex
152 * @param {Roo.EventObject} e
154 "celldblclick" : true,
157 * Fires when a row is clicked
158 * @param {Roo.bootstrap.Table} this
159 * @param {Roo.Element} el
160 * @param {Number} rowIndex
161 * @param {Roo.EventObject} e
166 * Fires when a row is double clicked
167 * @param {Roo.bootstrap.Table} this
168 * @param {Roo.Element} el
169 * @param {Number} rowIndex
170 * @param {Roo.EventObject} e
172 "rowdblclick" : true,
175 * Fires when a mouseover occur
176 * @param {Roo.bootstrap.Table} this
177 * @param {Roo.Element} el
178 * @param {Number} rowIndex
179 * @param {Number} columnIndex
180 * @param {Roo.EventObject} e
185 * Fires when a mouseout occur
186 * @param {Roo.bootstrap.Table} this
187 * @param {Roo.Element} el
188 * @param {Number} rowIndex
189 * @param {Number} columnIndex
190 * @param {Roo.EventObject} e
195 * Fires when a row is rendered, so you can change add a style to it.
196 * @param {Roo.bootstrap.Table} this
197 * @param {Object} rowcfg contains record rowIndex colIndex and rowClass - set rowClass to add a style.
201 * @event rowsrendered
202 * Fires when all the rows have been rendered
203 * @param {Roo.bootstrap.Table} this
205 'rowsrendered' : true,
208 * The raw contextmenu event for the entire grid.
209 * @param {Roo.EventObject} e
211 "contextmenu" : true,
213 * @event rowcontextmenu
214 * Fires when a row is right clicked
215 * @param {Roo.bootstrap.Table} this
216 * @param {Number} rowIndex
217 * @param {Roo.EventObject} e
219 "rowcontextmenu" : true,
221 * @event cellcontextmenu
222 * Fires when a cell is right clicked
223 * @param {Roo.bootstrap.Table} this
224 * @param {Number} rowIndex
225 * @param {Number} cellIndex
226 * @param {Roo.EventObject} e
228 "cellcontextmenu" : true,
230 * @event headercontextmenu
231 * Fires when a header is right clicked
232 * @param {Roo.bootstrap.Table} this
233 * @param {Number} columnIndex
234 * @param {Roo.EventObject} e
236 "headercontextmenu" : true,
239 * The raw mousedown event for the entire grid.
240 * @param {Roo.EventObject} e
247 Roo.extend(Roo.bootstrap.Table, Roo.bootstrap.Component, {
263 enableColumnResize: true,
265 rowSelection : false,
266 cellSelection : false,
271 // Roo.Element - the tbody
272 bodyEl: false, // <tbody> Roo.Element - thead element
273 headEl: false, // <thead> Roo.Element - thead element
274 resizeProxy : false, // proxy element for dragging?
278 container: false, // used by gridpanel...
284 auto_hide_footer : false,
286 view: false, // actually points to this..
288 getAutoCreate : function()
290 var cfg = Roo.apply({}, Roo.bootstrap.Table.superclass.getAutoCreate.call(this));
297 // this get's auto added by panel.Grid
298 if (this.scrollBody) {
299 cfg.cls += ' table-body-fixed';
302 cfg.cls += ' table-striped';
306 cfg.cls += ' table-hover';
309 cfg.cls += ' table-bordered';
311 if (this.condensed) {
312 cfg.cls += ' table-condensed';
315 if (this.responsive) {
316 cfg.cls += ' table-responsive';
320 cfg.cls+= ' ' +this.cls;
326 cfg.style = (typeof(cfg.style) == 'undefined') ? ('table-layout:' + this.layout + ';') : (cfg.style + ('table-layout:' + this.layout + ';'));
329 if(this.store || this.cm){
331 cfg.cn.push(this.renderHeader());
334 cfg.cn.push(this.renderBody());
337 cfg.cn.push(this.renderFooter());
339 // where does this come from?
340 //cfg.cls+= ' TableGrid';
343 return { cn : [ cfg ] };
346 initEvents : function()
348 if(!this.store || !this.cm){
352 this.selModel.initEvents();
356 //Roo.log('initEvents with ds!!!!');
358 this.bodyEl = this.el.select('tbody', true).first();
359 this.headEl = this.el.select('thead', true).first();
360 this.mainFoot = this.el.select('tfoot', true).first();
365 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
366 e.on('click', this.sort, this);
370 // why is this done????? = it breaks dialogs??
371 //this.parent().el.setStyle('position', 'relative');
375 this.footer.parentId = this.id;
376 this.footer.onRender(this.el.select('tfoot tr td').first(), null);
379 this.el.select('tfoot tr td').first().addClass('hide');
384 this.maskEl = new Roo.LoadMask(this.el, { store : this.ds, msgCls: 'roo-el-mask-msg' });
387 this.store.on('load', this.onLoad, this);
388 this.store.on('beforeload', this.onBeforeLoad, this);
389 this.store.on('update', this.onUpdate, this);
390 this.store.on('add', this.onAdd, this);
391 this.store.on("clear", this.clear, this);
393 this.el.on("contextmenu", this.onContextMenu, this);
396 this.cm.on("headerchange", this.onHeaderChange, this);
397 this.cm.on("hiddenchange", this.onHiddenChange, this, arguments);
399 //?? does bodyEl get replaced on render?
400 this.bodyEl.on("click", this.onClick, this);
401 this.bodyEl.on("dblclick", this.onDblClick, this);
402 this.bodyEl.on('scroll', this.onBodyScroll, this);
404 // guessing mainbody will work - this relays usually caught by selmodel at present.
405 this.relayEvents(this.bodyEl, ["mousedown","mouseup","mouseover","mouseout","keypress"]);
408 this.resizeProxy = Roo.get(document.body).createChild({ cls:"x-grid-resize-proxy", html: ' ' });
411 if(this.headEl && this.enableColumnResize !== false && Roo.grid.SplitDragZone){
412 new Roo.grid.SplitDragZone(this, this.headEl.dom, false); // not sure what 'lockedHd is for this implementation..)
417 // Compatibility with grid - we implement all the view features at present.
427 var cm = this.cm, styles = [];
428 this.CSS.removeStyleSheet(this.id + '-cssrules');
429 var headHeight = this.headEl ? this.headEl.dom.clientHeight : 0;
430 // we can honour xs/sm/md/xl as widths...
431 // we first have to decide what widht we are currently at...
432 var sz = Roo.getGridSize();
436 var cols = []; // visable cols.
438 for(var i = 0, len = cm.getColumnCount(); i < len; i++) {
439 var w = cm.getColumnWidth(i, false);
441 cols.push( { rel : false, abs : 0 });
445 cols.push( { rel : false, abs : w });
447 last = i; // not really..
450 var w = cm.getColumnWidth(i, sz);
455 cols.push( { rel : w, abs : false });
458 var avail = this.bodyEl.dom.clientWidth - total_abs;
460 var unitWidth = Math.floor(avail / total);
461 var rem = avail - (unitWidth * total);
463 var hidden, width, pos = 0 , splithide , left;
464 for(var i = 0, len = cm.getColumnCount(); i < len; i++) {
466 hidden = 'display:none;';
468 width = 'width:0px;';
474 // we can honour xs/sm/md/xl ?
475 var w = cols[i].rel == false ? cols[i].abs : (cols[i].rel * unitWidth);
477 hidden = 'display:none;';
479 // width should return a small number...
481 w+=rem; // add the remaining with..
484 left = "left:" + (pos -4) + "px;";
485 width = "width:" + w+ "px;";
488 if (this.responsive) {
491 hidden = cm.isHidden(i) ? 'display:none;' : '';
492 splithide = 'display: none;';
495 styles.push( '#' , this.id , ' .x-col-' , i, " {", cm.config[i].css, width, hidden, "}\n" );
498 splithide = 'display:none;';
501 styles.push('#' , this.id , ' .x-hcol-' , i, " { ", width, hidden," }\n",
502 '#' , this.id , ' .x-grid-split-' , i, " { ", left, splithide,'height:', (headHeight - 4), "px;}\n"
507 //Roo.log(styles.join(''));
508 this.CSS.createStyleSheet( styles.join(''), this.id + '-cssrules');
514 onContextMenu : function(e, t)
516 this.processEvent("contextmenu", e);
519 processEvent : function(name, e)
521 if (name != 'touchstart' ) {
522 this.fireEvent(name, e);
525 var t = e.getTarget();
527 var cell = Roo.get(t);
533 if(cell.findParent('tfoot', false, true)){
537 if(cell.findParent('thead', false, true)){
539 if(e.getTarget().nodeName.toLowerCase() != 'th'){
540 cell = Roo.get(t).findParent('th', false, true);
542 Roo.log("failed to find th in thead?");
543 Roo.log(e.getTarget());
548 var cellIndex = cell.dom.cellIndex;
550 var ename = name == 'touchstart' ? 'click' : name;
551 this.fireEvent("header" + ename, this, cellIndex, e);
556 if(e.getTarget().nodeName.toLowerCase() != 'td'){
557 cell = Roo.get(t).findParent('td', false, true);
559 Roo.log("failed to find th in tbody?");
560 Roo.log(e.getTarget());
565 var row = cell.findParent('tr', false, true);
566 var cellIndex = cell.dom.cellIndex;
567 var rowIndex = row.dom.rowIndex - 1;
571 this.fireEvent("row" + name, this, rowIndex, e);
575 this.fireEvent("cell" + name, this, rowIndex, cellIndex, e);
581 onMouseover : function(e, el)
583 var cell = Roo.get(el);
589 if(e.getTarget().nodeName.toLowerCase() != 'td'){
590 cell = cell.findParent('td', false, true);
593 var row = cell.findParent('tr', false, true);
594 var cellIndex = cell.dom.cellIndex;
595 var rowIndex = row.dom.rowIndex - 1; // start from 0
597 this.fireEvent('mouseover', this, cell, rowIndex, cellIndex, e);
601 onMouseout : function(e, el)
603 var cell = Roo.get(el);
609 if(e.getTarget().nodeName.toLowerCase() != 'td'){
610 cell = cell.findParent('td', false, true);
613 var row = cell.findParent('tr', false, true);
614 var cellIndex = cell.dom.cellIndex;
615 var rowIndex = row.dom.rowIndex - 1; // start from 0
617 this.fireEvent('mouseout', this, cell, rowIndex, cellIndex, e);
621 onClick : function(e, el)
623 var cell = Roo.get(el);
625 if(!cell || (!this.cellSelection && !this.rowSelection)){
629 if(e.getTarget().nodeName.toLowerCase() != 'td'){
630 cell = cell.findParent('td', false, true);
633 if(!cell || typeof(cell) == 'undefined'){
637 var row = cell.findParent('tr', false, true);
639 if(!row || typeof(row) == 'undefined'){
643 var cellIndex = cell.dom.cellIndex;
644 var rowIndex = this.getRowIndex(row);
646 // why??? - should these not be based on SelectionModel?
647 //if(this.cellSelection){
648 this.fireEvent('cellclick', this, cell, rowIndex, cellIndex, e);
651 //if(this.rowSelection){
652 this.fireEvent('rowclick', this, row, rowIndex, e);
657 onDblClick : function(e,el)
659 var cell = Roo.get(el);
661 if(!cell || (!this.cellSelection && !this.rowSelection)){
665 if(e.getTarget().nodeName.toLowerCase() != 'td'){
666 cell = cell.findParent('td', false, true);
669 if(!cell || typeof(cell) == 'undefined'){
673 var row = cell.findParent('tr', false, true);
675 if(!row || typeof(row) == 'undefined'){
679 var cellIndex = cell.dom.cellIndex;
680 var rowIndex = this.getRowIndex(row);
682 if(this.cellSelection){
683 this.fireEvent('celldblclick', this, cell, rowIndex, cellIndex, e);
686 if(this.rowSelection){
687 this.fireEvent('rowdblclick', this, row, rowIndex, e);
690 findRowIndex : function(el)
692 var cell = Roo.get(el);
696 var row = cell.findParent('tr', false, true);
698 if(!row || typeof(row) == 'undefined'){
701 return this.getRowIndex(row);
703 sort : function(e,el)
705 var col = Roo.get(el);
707 if(!col.hasClass('sortable')){
711 var sort = col.attr('sort');
714 if(col.select('i', true).first().hasClass('fa-arrow-up')){
718 this.store.sortInfo = {field : sort, direction : dir};
721 Roo.log("calling footer first");
722 this.footer.onClick('first');
725 this.store.load({ params : { start : 0 } });
729 renderHeader : function()
739 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
741 var config = cm.config[i];
748 html: cm.getColumnHeader(i)
751 var tooltip = cm.getColumnTooltip(i);
759 if(typeof(config.sortable) != 'undefined' && config.sortable){
760 c.cls += ' sortable';
761 c.html = '<i class="fa"></i>' + c.html;
764 // could use BS4 hidden-..-down
766 if(typeof(config.lgHeader) != 'undefined'){
767 hh += '<span class="hidden-xs hidden-sm hidden-md ">' + config.lgHeader + '</span>';
770 if(typeof(config.mdHeader) != 'undefined'){
771 hh += '<span class="hidden-xs hidden-sm hidden-lg">' + config.mdHeader + '</span>';
774 if(typeof(config.smHeader) != 'undefined'){
775 hh += '<span class="hidden-xs hidden-md hidden-lg">' + config.smHeader + '</span>';
778 if(typeof(config.xsHeader) != 'undefined'){
779 hh += '<span class="hidden-sm hidden-md hidden-lg">' + config.xsHeader + '</span>';
786 if(typeof(config.tooltip) != 'undefined'){
787 c.tooltip = config.tooltip;
790 if(typeof(config.colspan) != 'undefined'){
791 c.colspan = config.colspan;
794 // hidden is handled by CSS now
796 if(typeof(config.dataIndex) != 'undefined'){
797 c.sort = config.dataIndex;
802 if(typeof(config.align) != 'undefined' && config.align.length){
803 c.style += ' text-align:' + config.align + ';';
806 /* width is done in CSS
807 *if(typeof(config.width) != 'undefined'){
808 c.style += ' width:' + config.width + 'px;';
809 this.totalWidth += config.width;
811 this.totalWidth += 100; // assume minimum of 100 per column?
815 if(typeof(config.cls) != 'undefined'){
816 c.cls = (typeof(c.cls) == 'undefined') ? config.cls : (c.cls + ' ' + config.cls);
818 // this is the bit that doesnt reall work at all...
820 if (this.responsive) {
823 ['xs','sm','md','lg'].map(function(size){
825 if(typeof(config[size]) == 'undefined'){
829 if (!config[size]) { // 0 = hidden
830 // BS 4 '0' is treated as hide that column and below.
831 c.cls += ' hidden-' + size + ' hidden' + size + '-down';
835 c.cls += ' col-' + size + '-' + config[size] + (
836 size == 'xs' ? (' col-' + config[size] ) : '' // bs4 col-{num} replaces col-xs
844 c.html +=' <span class="x-grid-split x-grid-split-' + i + '"></span>';
855 renderBody : function()
865 colspan : this.cm.getColumnCount()
875 renderFooter : function()
885 colspan : this.cm.getColumnCount()
899 // Roo.log('ds onload');
906 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
907 e.select('i', true).removeClass(['fa-arrow-up', 'fa-arrow-down']);
908 if (_this.store.sortInfo) {
910 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
911 e.select('i', true).addClass(['fa-arrow-up']);
914 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
915 e.select('i', true).addClass(['fa-arrow-down']);
920 var tbody = this.bodyEl;
922 if(ds.getCount() > 0){
923 ds.data.each(function(d,rowIndex){
924 var row = this.renderRow(cm, ds, rowIndex);
926 tbody.createChild(row);
930 if(row.cellObjects.length){
931 Roo.each(row.cellObjects, function(r){
932 _this.renderCellObject(r);
939 var tfoot = this.el.select('tfoot', true).first();
941 if(this.footerShow && this.auto_hide_footer && this.mainFoot){
943 this.mainFoot.setVisibilityMode(Roo.Element.DISPLAY).hide();
945 var total = this.ds.getTotalCount();
947 if(this.footer.pageSize < total){
948 this.mainFoot.show();
952 Roo.each(this.el.select('tbody td', true).elements, function(e){
953 e.on('mouseover', _this.onMouseover, _this);
956 Roo.each(this.el.select('tbody td', true).elements, function(e){
957 e.on('mouseout', _this.onMouseout, _this);
959 this.fireEvent('rowsrendered', this);
963 this.initCSS(); /// resize cols
969 onUpdate : function(ds,record)
971 this.refreshRow(record);
975 onRemove : function(ds, record, index, isUpdate){
976 if(isUpdate !== true){
977 this.fireEvent("beforerowremoved", this, index, record);
979 var bt = this.bodyEl.dom;
981 var rows = this.el.select('tbody > tr', true).elements;
983 if(typeof(rows[index]) != 'undefined'){
984 bt.removeChild(rows[index].dom);
987 // if(bt.rows[index]){
988 // bt.removeChild(bt.rows[index]);
991 if(isUpdate !== true){
992 //this.stripeRows(index);
993 //this.syncRowHeights(index, index);
995 this.fireEvent("rowremoved", this, index, record);
999 onAdd : function(ds, records, rowIndex)
1001 //Roo.log('on Add called');
1002 // - note this does not handle multiple adding very well..
1003 var bt = this.bodyEl.dom;
1004 for (var i =0 ; i < records.length;i++) {
1005 //Roo.log('call insert row Add called on ' + rowIndex + ':' + i);
1006 //Roo.log(records[i]);
1007 //Roo.log(this.store.getAt(rowIndex+i));
1008 this.insertRow(this.store, rowIndex + i, false);
1015 refreshRow : function(record){
1016 var ds = this.store, index;
1017 if(typeof record == 'number'){
1019 record = ds.getAt(index);
1021 index = ds.indexOf(record);
1023 return; // should not happen - but seems to
1026 this.insertRow(ds, index, true);
1028 this.onRemove(ds, record, index+1, true);
1030 //this.syncRowHeights(index, index);
1032 this.fireEvent("rowupdated", this, index, record);
1034 // private - called by RowSelection
1035 onRowSelect : function(rowIndex){
1036 var row = this.getRowDom(rowIndex);
1037 row.addClass(['bg-info','info']);
1039 // private - called by RowSelection
1040 onRowDeselect : function(rowIndex)
1045 var row = this.getRowDom(rowIndex);
1046 row.removeClass(['bg-info','info']);
1049 * Focuses the specified row.
1050 * @param {Number} row The row index
1052 focusRow : function(row)
1054 //Roo.log('GridView.focusRow');
1055 var x = this.bodyEl.dom.scrollLeft;
1056 this.focusCell(row, 0, false);
1057 this.bodyEl.dom.scrollLeft = x;
1061 * Focuses the specified cell.
1062 * @param {Number} row The row index
1063 * @param {Number} col The column index
1064 * @param {Boolean} hscroll false to disable horizontal scrolling
1066 focusCell : function(row, col, hscroll)
1068 //Roo.log('GridView.focusCell');
1069 var el = this.ensureVisible(row, col, hscroll);
1070 // not sure what focusEL achives = it's a <a> pos relative
1071 //this.focusEl.alignTo(el, "tl-tl");
1073 // this.focusEl.focus();
1075 // this.focusEl.focus.defer(1, this.focusEl);
1080 * Scrolls the specified cell into view
1081 * @param {Number} row The row index
1082 * @param {Number} col The column index
1083 * @param {Boolean} hscroll false to disable horizontal scrolling
1085 ensureVisible : function(row, col, hscroll)
1087 //Roo.log('GridView.ensureVisible,' + row + ',' + col);
1088 //return null; //disable for testing.
1089 if(typeof row != "number"){
1092 if(row < 0 && row >= this.ds.getCount()){
1095 col = (col !== undefined ? col : 0);
1097 while(cm.isHidden(col)){
1101 var el = this.getCellDom(row, col);
1105 var c = this.bodyEl.dom;
1107 var ctop = parseInt(el.offsetTop, 10);
1108 var cleft = parseInt(el.offsetLeft, 10);
1109 var cbot = ctop + el.offsetHeight;
1110 var cright = cleft + el.offsetWidth;
1112 //var ch = c.clientHeight - this.mainHd.dom.offsetHeight;
1113 var ch = 0; //?? header is not withing the area?
1114 var stop = parseInt(c.scrollTop, 10);
1115 var sleft = parseInt(c.scrollLeft, 10);
1116 var sbot = stop + ch;
1117 var sright = sleft + c.clientWidth;
1119 Roo.log('GridView.ensureVisible:' +
1121 ' c.clientHeight:' + c.clientHeight +
1122 ' this.mainHd.dom.offsetHeight:' + this.mainHd.dom.offsetHeight +
1131 //Roo.log("set scrolltop to ctop DISABLE?");
1132 }else if(cbot > sbot){
1133 //Roo.log("set scrolltop to cbot-ch");
1134 c.scrollTop = cbot-ch;
1137 if(hscroll !== false){
1139 c.scrollLeft = cleft;
1140 }else if(cright > sright){
1141 c.scrollLeft = cright-c.clientWidth;
1149 insertRow : function(dm, rowIndex, isUpdate){
1152 this.fireEvent("beforerowsinserted", this, rowIndex);
1154 //var s = this.getScrollState();
1155 var row = this.renderRow(this.cm, this.store, rowIndex);
1156 // insert before rowIndex..
1157 var e = this.bodyEl.createChild(row,this.getRowDom(rowIndex));
1161 if(row.cellObjects.length){
1162 Roo.each(row.cellObjects, function(r){
1163 _this.renderCellObject(r);
1168 this.fireEvent("rowsinserted", this, rowIndex);
1169 //this.syncRowHeights(firstRow, lastRow);
1170 //this.stripeRows(firstRow);
1177 getRowDom : function(rowIndex)
1179 var rows = this.el.select('tbody > tr', true).elements;
1181 return (typeof(rows[rowIndex]) == 'undefined') ? false : rows[rowIndex];
1184 getCellDom : function(rowIndex, colIndex)
1186 var row = this.getRowDom(rowIndex);
1187 if (row === false) {
1190 var cols = row.select('td', true).elements;
1191 return (typeof(cols[colIndex]) == 'undefined') ? false : cols[colIndex];
1195 // returns the object tree for a tr..
1198 renderRow : function(cm, ds, rowIndex)
1200 var d = ds.getAt(rowIndex);
1204 cls : 'x-row-' + rowIndex,
1208 var cellObjects = [];
1210 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
1211 var config = cm.config[i];
1213 var renderer = cm.getRenderer(i);
1217 if(typeof(renderer) !== 'undefined'){
1218 value = renderer(d.data[cm.getDataIndex(i)], false, d);
1220 // if object are returned, then they are expected to be Roo.bootstrap.Component instances
1221 // and are rendered into the cells after the row is rendered - using the id for the element.
1223 if(typeof(value) === 'object'){
1233 rowIndex : rowIndex,
1238 this.fireEvent('rowclass', this, rowcfg);
1242 // this might end up displaying HTML?
1243 // this is too messy... - better to only do it on columsn you know are going to be too long
1244 //tooltip : (typeof(value) === 'object') ? '' : value,
1245 cls : rowcfg.rowClass + ' x-col-' + i,
1247 html: (typeof(value) === 'object') ? '' : value
1254 if(typeof(config.colspan) != 'undefined'){
1255 td.colspan = config.colspan;
1260 if(typeof(config.align) != 'undefined' && config.align.length){
1261 td.style += ' text-align:' + config.align + ';';
1263 if(typeof(config.valign) != 'undefined' && config.valign.length){
1264 td.style += ' vertical-align:' + config.valign + ';';
1267 if(typeof(config.width) != 'undefined'){
1268 td.style += ' width:' + config.width + 'px;';
1272 if(typeof(config.cursor) != 'undefined'){
1273 td.style += ' cursor:' + config.cursor + ';';
1276 if(typeof(config.cls) != 'undefined'){
1277 td.cls = (typeof(td.cls) == 'undefined') ? config.cls : (td.cls + ' ' + config.cls);
1279 if (this.responsive) {
1280 ['xs','sm','md','lg'].map(function(size){
1282 if(typeof(config[size]) == 'undefined'){
1288 if (!config[size]) { // 0 = hidden
1289 // BS 4 '0' is treated as hide that column and below.
1290 td.cls += ' hidden-' + size + ' hidden' + size + '-down';
1294 td.cls += ' col-' + size + '-' + config[size] + (
1295 size == 'xs' ? (' col-' + config[size] ) : '' // bs4 col-{num} replaces col-xs
1305 row.cellObjects = cellObjects;
1313 onBeforeLoad : function()
1322 this.el.select('tbody', true).first().dom.innerHTML = '';
1325 * Show or hide a row.
1326 * @param {Number} rowIndex to show or hide
1327 * @param {Boolean} state hide
1329 setRowVisibility : function(rowIndex, state)
1331 var bt = this.bodyEl.dom;
1333 var rows = this.el.select('tbody > tr', true).elements;
1335 if(typeof(rows[rowIndex]) == 'undefined'){
1338 rows[rowIndex][ state ? 'removeClass' : 'addClass']('d-none');
1343 getSelectionModel : function(){
1345 this.selModel = new Roo.bootstrap.Table.RowSelectionModel({grid: this});
1347 return this.selModel;
1350 * Render the Roo.bootstrap object from renderder
1352 renderCellObject : function(r)
1356 r.cfg.parentId = (typeof(r.container) == 'string') ? r.container : r.container.id;
1358 var t = r.cfg.render(r.container);
1361 Roo.each(r.cfg.cn, function(c){
1363 container: t.getChildContainer(),
1366 _this.renderCellObject(child);
1371 * get the Row Index from a dom element.
1372 * @param {Roo.Element} row The row to look for
1373 * @returns {Number} the row
1375 getRowIndex : function(row)
1379 Roo.each(this.el.select('tbody > tr', true).elements, function(el, index){
1390 * get the header TH element for columnIndex
1391 * @param {Number} columnIndex
1392 * @returns {Roo.Element}
1394 getHeaderIndex: function(colIndex)
1396 var cols = this.headEl.select('th', true).elements;
1397 return cols[colIndex];
1400 * get the Column Index from a dom element. (using regex on x-hcol-{colid})
1401 * @param {domElement} cell to look for
1402 * @returns {Number} the column
1404 getCellIndex : function(cell)
1406 var id = String(cell.className).match(Roo.bootstrap.Table.cellRE);
1408 return parseInt(id[1], 10);
1413 * Returns the grid's underlying element = used by panel.Grid
1414 * @return {Element} The element
1416 getGridEl : function(){
1420 * Forces a resize - used by panel.Grid
1421 * @return {Element} The element
1423 autoSize : function()
1425 //var ctr = Roo.get(this.container.dom.parentElement);
1426 var ctr = Roo.get(this.el.dom);
1428 var thd = this.getGridEl().select('thead',true).first();
1429 var tbd = this.getGridEl().select('tbody', true).first();
1430 var tfd = this.getGridEl().select('tfoot', true).first();
1432 var cw = ctr.getWidth();
1433 this.getGridEl().select('tfoot tr, tfoot td',true).setWidth(cw);
1437 tbd.setWidth(ctr.getWidth());
1438 // if the body has a max height - and then scrolls - we should perhaps set up the height here
1439 // this needs fixing for various usage - currently only hydra job advers I think..
1441 // ctr.getHeight() - ((thd ? thd.getHeight() : 0) + (tfd ? tfd.getHeight() : 0))
1443 var barsize = (tbd.dom.offsetWidth - tbd.dom.clientWidth);
1446 cw = Math.max(cw, this.totalWidth);
1447 this.getGridEl().select('tbody tr',true).setWidth(cw);
1450 // resize 'expandable coloumn?
1452 return; // we doe not have a view in this design..
1455 onBodyScroll: function()
1457 //Roo.log("body scrolled');" + this.bodyEl.dom.scrollLeft);
1459 this.headEl.setStyle({
1460 'position' : 'relative',
1461 'left': (-1* this.bodyEl.dom.scrollLeft) + 'px'
1467 var scrollHeight = this.bodyEl.dom.scrollHeight;
1469 var scrollTop = Math.ceil(this.bodyEl.getScroll().top);
1471 var height = this.bodyEl.getHeight();
1473 if(scrollHeight - height == scrollTop) {
1475 var total = this.ds.getTotalCount();
1477 if(this.footer.cursor + this.footer.pageSize < total){
1479 this.footer.ds.load({
1481 start : this.footer.cursor + this.footer.pageSize,
1482 limit : this.footer.pageSize
1491 onColumnSplitterMoved : function(i, diff)
1493 this.userResized = true;
1495 var cm = this.colModel;
1497 var w = this.getHeaderIndex(i).getWidth() + diff;
1500 cm.setColumnWidth(i, w, true);
1502 //var cid = cm.getColumnId(i); << not used in this version?
1503 /* Roo.log(['#' + this.id + ' .x-col-' + i, "width", w + "px"]);
1505 this.CSS.updateRule( '#' + this.id + ' .x-col-' + i, "width", w + "px");
1506 this.CSS.updateRule('#' + this.id + ' .x-hcol-' + i, "width", w + "px");
1507 this.CSS.updateRule('#' + this.id + ' .x-grid-split-' + i, "left", w + "px");
1509 //this.updateSplitters();
1510 //this.layout(); << ??
1511 this.fireEvent("columnresize", i, w);
1513 onHeaderChange : function()
1515 var header = this.renderHeader();
1516 var table = this.el.select('table', true).first();
1518 this.headEl.remove();
1519 this.headEl = table.createChild(header, this.bodyEl, false);
1521 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
1522 e.on('click', this.sort, this);
1525 if(this.enableColumnResize !== false && Roo.grid.SplitDragZone){
1526 new Roo.grid.SplitDragZone(this, this.headEl.dom, false); // not sure what 'lockedHd is for this implementation..)
1531 onHiddenChange : function(colModel, colIndex, hidden)
1535 var thSelector = '#' + this.id + ' .x-hcol-' + colIndex;
1536 var tdSelector = '#' + this.id + ' .x-col-' + colIndex;
1538 this.CSS.updateRule(thSelector, "display", "");
1539 this.CSS.updateRule(tdSelector, "display", "");
1542 this.CSS.updateRule(thSelector, "display", "none");
1543 this.CSS.updateRule(tdSelector, "display", "none");
1546 // onload calls initCSS()
1547 this.onHeaderChange();
1551 setColumnWidth: function(col_index, width)
1553 // width = "md-2 xs-2..."
1554 if(!this.colModel.config[col_index]) {
1558 var w = width.split(" ");
1560 var rows = this.el.dom.getElementsByClassName("x-col-"+col_index);
1562 var h_row = this.el.dom.getElementsByClassName("x-hcol-"+col_index);
1565 for(var j = 0; j < w.length; j++) {
1571 var size_cls = w[j].split("-");
1573 if(!Number.isInteger(size_cls[1] * 1)) {
1577 if(!this.colModel.config[col_index][size_cls[0]]) {
1581 if(!h_row[0].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1585 h_row[0].classList.replace(
1586 "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1587 "col-"+size_cls[0]+"-"+size_cls[1]
1590 for(var i = 0; i < rows.length; i++) {
1592 var size_cls = w[j].split("-");
1594 if(!Number.isInteger(size_cls[1] * 1)) {
1598 if(!this.colModel.config[col_index][size_cls[0]]) {
1602 if(!rows[i].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1606 rows[i].classList.replace(
1607 "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1608 "col-"+size_cls[0]+"-"+size_cls[1]
1612 this.colModel.config[col_index][size_cls[0]] = size_cls[1];
1617 // currently only used to find the split on drag..
1618 Roo.bootstrap.Table.cellRE = /(?:.*?)x-grid-(?:hd|cell|split)-([\d]+)(?:.*?)/;
1623 Roo.bootstrap.Table.AbstractSelectionModel = Roo.grid.AbstractSelectionModel;
1624 Roo.bootstrap.Table.RowSelectionModel = Roo.grid.RowSelectionModel;