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 (with resizable columns)
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 = needs scrollBody to be set to work (drag/drop)
91 * @cfg {Number} minColumnWidth default 50 pixels minimum column width
95 * @param {Object} config The config object
98 Roo.bootstrap.Table = function(config)
100 Roo.bootstrap.Table.superclass.constructor.call(this, config);
103 this.rowSelection = (typeof(config.rowSelection) != 'undefined') ? config.rowSelection : this.rowSelection;
104 this.cellSelection = (typeof(config.cellSelection) != 'undefined') ? config.cellSelection : this.cellSelection;
105 this.headerShow = (typeof(config.thead) != 'undefined') ? config.thead : this.headerShow;
106 this.footerShow = (typeof(config.tfoot) != 'undefined') ? config.tfoot : this.footerShow;
108 this.view = this; // compat with grid.
110 this.sm = this.sm || {xtype: 'RowSelectionModel'};
113 this.selModel = Roo.factory(this.sm, Roo.grid);
114 this.sm = this.selModel;
115 this.sm.xmodule = this.xmodule || false;
118 if (this.cm && typeof(this.cm.config) == 'undefined') {
119 this.colModel = new Roo.grid.ColumnModel(this.cm);
120 this.cm = this.colModel;
121 this.cm.xmodule = this.xmodule || false;
124 this.store= Roo.factory(this.store, Roo.data);
125 this.ds = this.store;
126 this.ds.xmodule = this.xmodule || false;
129 if (this.footer && this.store) {
130 this.footer.dataSource = this.ds;
131 this.footer = Roo.factory(this.footer);
138 * Fires when a cell is clicked
139 * @param {Roo.bootstrap.Table} this
140 * @param {Roo.Element} el
141 * @param {Number} rowIndex
142 * @param {Number} columnIndex
143 * @param {Roo.EventObject} e
147 * @event celldblclick
148 * Fires when a cell is double clicked
149 * @param {Roo.bootstrap.Table} this
150 * @param {Roo.Element} el
151 * @param {Number} rowIndex
152 * @param {Number} columnIndex
153 * @param {Roo.EventObject} e
155 "celldblclick" : true,
158 * Fires when a row is clicked
159 * @param {Roo.bootstrap.Table} this
160 * @param {Roo.Element} el
161 * @param {Number} rowIndex
162 * @param {Roo.EventObject} e
167 * Fires when a row is double clicked
168 * @param {Roo.bootstrap.Table} this
169 * @param {Roo.Element} el
170 * @param {Number} rowIndex
171 * @param {Roo.EventObject} e
173 "rowdblclick" : true,
176 * Fires when a mouseover occur
177 * @param {Roo.bootstrap.Table} this
178 * @param {Roo.Element} el
179 * @param {Number} rowIndex
180 * @param {Number} columnIndex
181 * @param {Roo.EventObject} e
186 * Fires when a mouseout occur
187 * @param {Roo.bootstrap.Table} this
188 * @param {Roo.Element} el
189 * @param {Number} rowIndex
190 * @param {Number} columnIndex
191 * @param {Roo.EventObject} e
196 * Fires when a row is rendered, so you can change add a style to it.
197 * @param {Roo.bootstrap.Table} this
198 * @param {Object} rowcfg contains record rowIndex colIndex and rowClass - set rowClass to add a style.
202 * @event rowsrendered
203 * Fires when all the rows have been rendered
204 * @param {Roo.bootstrap.Table} this
206 'rowsrendered' : true,
209 * The raw contextmenu event for the entire grid.
210 * @param {Roo.EventObject} e
212 "contextmenu" : true,
214 * @event rowcontextmenu
215 * Fires when a row is right clicked
216 * @param {Roo.bootstrap.Table} this
217 * @param {Number} rowIndex
218 * @param {Roo.EventObject} e
220 "rowcontextmenu" : true,
222 * @event cellcontextmenu
223 * Fires when a cell is right clicked
224 * @param {Roo.bootstrap.Table} this
225 * @param {Number} rowIndex
226 * @param {Number} cellIndex
227 * @param {Roo.EventObject} e
229 "cellcontextmenu" : true,
231 * @event headercontextmenu
232 * Fires when a header is right clicked
233 * @param {Roo.bootstrap.Table} this
234 * @param {Number} columnIndex
235 * @param {Roo.EventObject} e
237 "headercontextmenu" : true,
240 * The raw mousedown event for the entire grid.
241 * @param {Roo.EventObject} e
248 Roo.extend(Roo.bootstrap.Table, Roo.bootstrap.Component, {
264 enableColumnResize: true,
266 rowSelection : false,
267 cellSelection : false,
272 // Roo.Element - the tbody
273 bodyEl: false, // <tbody> Roo.Element - thead element
274 headEl: false, // <thead> Roo.Element - thead element
275 resizeProxy : false, // proxy element for dragging?
279 container: false, // used by gridpanel...
285 auto_hide_footer : false,
287 view: false, // actually points to this..
289 getAutoCreate : function()
291 var cfg = Roo.apply({}, Roo.bootstrap.Table.superclass.getAutoCreate.call(this));
298 // this get's auto added by panel.Grid
299 if (this.scrollBody) {
300 cfg.cls += ' table-body-fixed';
303 cfg.cls += ' table-striped';
307 cfg.cls += ' table-hover';
310 cfg.cls += ' table-bordered';
312 if (this.condensed) {
313 cfg.cls += ' table-condensed';
316 if (this.responsive) {
317 cfg.cls += ' table-responsive';
321 cfg.cls+= ' ' +this.cls;
327 cfg.style = (typeof(cfg.style) == 'undefined') ? ('table-layout:' + this.layout + ';') : (cfg.style + ('table-layout:' + this.layout + ';'));
330 if(this.store || this.cm){
332 cfg.cn.push(this.renderHeader());
335 cfg.cn.push(this.renderBody());
338 cfg.cn.push(this.renderFooter());
340 // where does this come from?
341 //cfg.cls+= ' TableGrid';
344 return { cn : [ cfg ] };
347 initEvents : function()
349 if(!this.store || !this.cm){
353 this.selModel.initEvents();
357 //Roo.log('initEvents with ds!!!!');
359 this.bodyEl = this.el.select('tbody', true).first();
360 this.headEl = this.el.select('thead', true).first();
361 this.mainFoot = this.el.select('tfoot', true).first();
366 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
367 e.on('click', this.sort, this);
371 // why is this done????? = it breaks dialogs??
372 //this.parent().el.setStyle('position', 'relative');
376 this.footer.parentId = this.id;
377 this.footer.onRender(this.el.select('tfoot tr td').first(), null);
380 this.el.select('tfoot tr td').first().addClass('hide');
385 this.maskEl = new Roo.LoadMask(this.el, { store : this.ds, msgCls: 'roo-el-mask-msg' });
388 this.store.on('load', this.onLoad, this);
389 this.store.on('beforeload', this.onBeforeLoad, this);
390 this.store.on('update', this.onUpdate, this);
391 this.store.on('add', this.onAdd, this);
392 this.store.on("clear", this.clear, this);
394 this.el.on("contextmenu", this.onContextMenu, this);
397 this.cm.on("headerchange", this.onHeaderChange, this);
398 this.cm.on("hiddenchange", this.onHiddenChange, this, arguments);
400 //?? does bodyEl get replaced on render?
401 this.bodyEl.on("click", this.onClick, this);
402 this.bodyEl.on("dblclick", this.onDblClick, this);
403 this.bodyEl.on('scroll', this.onBodyScroll, this);
405 // guessing mainbody will work - this relays usually caught by selmodel at present.
406 this.relayEvents(this.bodyEl, ["mousedown","mouseup","mouseover","mouseout","keypress"]);
409 this.resizeProxy = Roo.get(document.body).createChild({ cls:"x-grid-resize-proxy", html: ' ' });
412 if(this.headEl && this.enableColumnResize !== false && Roo.grid.SplitDragZone){
413 new Roo.grid.SplitDragZone(this, this.headEl.dom, false); // not sure what 'lockedHd is for this implementation..)
418 // Compatibility with grid - we implement all the view features at present.
428 var cm = this.cm, styles = [];
429 this.CSS.removeStyleSheet(this.id + '-cssrules');
430 var headHeight = this.headEl ? this.headEl.dom.clientHeight : 0;
431 // we can honour xs/sm/md/xl as widths...
432 // we first have to decide what widht we are currently at...
433 var sz = Roo.getGridSize();
437 var cols = []; // visable cols.
439 for(var i = 0, len = cm.getColumnCount(); i < len; i++) {
440 var w = cm.getColumnWidth(i, false);
442 cols.push( { rel : false, abs : 0 });
446 cols.push( { rel : false, abs : w });
448 last = i; // not really..
451 var w = cm.getColumnWidth(i, sz);
456 cols.push( { rel : w, abs : false });
459 var avail = this.bodyEl.dom.clientWidth - total_abs;
461 var unitWidth = Math.floor(avail / total);
462 var rem = avail - (unitWidth * total);
464 var hidden, width, pos = 0 , splithide , left;
465 for(var i = 0, len = cm.getColumnCount(); i < len; i++) {
467 hidden = 'display:none;';
469 width = 'width:0px;';
475 // we can honour xs/sm/md/xl ?
476 var w = cols[i].rel == false ? cols[i].abs : (cols[i].rel * unitWidth);
478 hidden = 'display:none;';
480 // width should return a small number...
482 w+=rem; // add the remaining with..
485 left = "left:" + (pos -4) + "px;";
486 width = "width:" + w+ "px;";
489 if (this.responsive) {
492 hidden = cm.isHidden(i) ? 'display:none;' : '';
493 splithide = 'display: none;';
496 styles.push( '#' , this.id , ' .x-col-' , i, " {", cm.config[i].css, width, hidden, "}\n" );
499 splithide = 'display:none;';
502 styles.push('#' , this.id , ' .x-hcol-' , i, " { ", width, hidden," }\n",
503 '#' , this.id , ' .x-grid-split-' , i, " { ", left, splithide, 'height:', (headHeight - 4), "px;}\n",
504 // this is the popover version..
505 '.popover-inner #' , this.id , ' .x-grid-split-' , i, " { ", left, splithide, 'height:', 100, "%;}\n"
510 //Roo.log(styles.join(''));
511 this.CSS.createStyleSheet( styles.join(''), this.id + '-cssrules');
517 onContextMenu : function(e, t)
519 this.processEvent("contextmenu", e);
522 processEvent : function(name, e)
524 if (name != 'touchstart' ) {
525 this.fireEvent(name, e);
528 var t = e.getTarget();
530 var cell = Roo.get(t);
536 if(cell.findParent('tfoot', false, true)){
540 if(cell.findParent('thead', false, true)){
542 if(e.getTarget().nodeName.toLowerCase() != 'th'){
543 cell = Roo.get(t).findParent('th', false, true);
545 Roo.log("failed to find th in thead?");
546 Roo.log(e.getTarget());
551 var cellIndex = cell.dom.cellIndex;
553 var ename = name == 'touchstart' ? 'click' : name;
554 this.fireEvent("header" + ename, this, cellIndex, e);
559 if(e.getTarget().nodeName.toLowerCase() != 'td'){
560 cell = Roo.get(t).findParent('td', false, true);
562 Roo.log("failed to find th in tbody?");
563 Roo.log(e.getTarget());
568 var row = cell.findParent('tr', false, true);
569 var cellIndex = cell.dom.cellIndex;
570 var rowIndex = row.dom.rowIndex - 1;
574 this.fireEvent("row" + name, this, rowIndex, e);
578 this.fireEvent("cell" + name, this, rowIndex, cellIndex, e);
584 onMouseover : function(e, el)
586 var cell = Roo.get(el);
592 if(e.getTarget().nodeName.toLowerCase() != 'td'){
593 cell = cell.findParent('td', false, true);
596 var row = cell.findParent('tr', false, true);
597 var cellIndex = cell.dom.cellIndex;
598 var rowIndex = row.dom.rowIndex - 1; // start from 0
600 this.fireEvent('mouseover', this, cell, rowIndex, cellIndex, e);
604 onMouseout : function(e, el)
606 var cell = Roo.get(el);
612 if(e.getTarget().nodeName.toLowerCase() != 'td'){
613 cell = cell.findParent('td', false, true);
616 var row = cell.findParent('tr', false, true);
617 var cellIndex = cell.dom.cellIndex;
618 var rowIndex = row.dom.rowIndex - 1; // start from 0
620 this.fireEvent('mouseout', this, cell, rowIndex, cellIndex, e);
624 onClick : function(e, el)
626 var cell = Roo.get(el);
628 if(!cell || (!this.cellSelection && !this.rowSelection)){
632 if(e.getTarget().nodeName.toLowerCase() != 'td'){
633 cell = cell.findParent('td', false, true);
636 if(!cell || typeof(cell) == 'undefined'){
640 var row = cell.findParent('tr', false, true);
642 if(!row || typeof(row) == 'undefined'){
646 var cellIndex = cell.dom.cellIndex;
647 var rowIndex = this.getRowIndex(row);
649 // why??? - should these not be based on SelectionModel?
650 //if(this.cellSelection){
651 this.fireEvent('cellclick', this, cell, rowIndex, cellIndex, e);
654 //if(this.rowSelection){
655 this.fireEvent('rowclick', this, row, rowIndex, e);
660 onDblClick : function(e,el)
662 var cell = Roo.get(el);
664 if(!cell || (!this.cellSelection && !this.rowSelection)){
668 if(e.getTarget().nodeName.toLowerCase() != 'td'){
669 cell = cell.findParent('td', false, true);
672 if(!cell || typeof(cell) == 'undefined'){
676 var row = cell.findParent('tr', false, true);
678 if(!row || typeof(row) == 'undefined'){
682 var cellIndex = cell.dom.cellIndex;
683 var rowIndex = this.getRowIndex(row);
685 if(this.cellSelection){
686 this.fireEvent('celldblclick', this, cell, rowIndex, cellIndex, e);
689 if(this.rowSelection){
690 this.fireEvent('rowdblclick', this, row, rowIndex, e);
693 findRowIndex : function(el)
695 var cell = Roo.get(el);
699 var row = cell.findParent('tr', false, true);
701 if(!row || typeof(row) == 'undefined'){
704 return this.getRowIndex(row);
706 sort : function(e,el)
708 var col = Roo.get(el);
710 if(!col.hasClass('sortable')){
714 var sort = col.attr('sort');
717 if(col.select('i', true).first().hasClass('fa-arrow-up')){
721 this.store.sortInfo = {field : sort, direction : dir};
724 Roo.log("calling footer first");
725 this.footer.onClick('first');
728 this.store.load({ params : { start : 0 } });
732 renderHeader : function()
742 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
744 var config = cm.config[i];
751 html: cm.getColumnHeader(i)
754 var tooltip = cm.getColumnTooltip(i);
762 if(typeof(config.sortable) != 'undefined' && config.sortable){
763 c.cls += ' sortable';
764 c.html = '<i class="fa"></i>' + c.html;
767 // could use BS4 hidden-..-down
769 if(typeof(config.lgHeader) != 'undefined'){
770 hh += '<span class="hidden-xs hidden-sm hidden-md ">' + config.lgHeader + '</span>';
773 if(typeof(config.mdHeader) != 'undefined'){
774 hh += '<span class="hidden-xs hidden-sm hidden-lg">' + config.mdHeader + '</span>';
777 if(typeof(config.smHeader) != 'undefined'){
778 hh += '<span class="hidden-xs hidden-md hidden-lg">' + config.smHeader + '</span>';
781 if(typeof(config.xsHeader) != 'undefined'){
782 hh += '<span class="hidden-sm hidden-md hidden-lg">' + config.xsHeader + '</span>';
789 if(typeof(config.tooltip) != 'undefined'){
790 c.tooltip = config.tooltip;
793 if(typeof(config.colspan) != 'undefined'){
794 c.colspan = config.colspan;
797 // hidden is handled by CSS now
799 if(typeof(config.dataIndex) != 'undefined'){
800 c.sort = config.dataIndex;
805 if(typeof(config.align) != 'undefined' && config.align.length){
806 c.style += ' text-align:' + config.align + ';';
809 /* width is done in CSS
810 *if(typeof(config.width) != 'undefined'){
811 c.style += ' width:' + config.width + 'px;';
812 this.totalWidth += config.width;
814 this.totalWidth += 100; // assume minimum of 100 per column?
818 if(typeof(config.cls) != 'undefined'){
819 c.cls = (typeof(c.cls) == 'undefined') ? config.cls : (c.cls + ' ' + config.cls);
821 // this is the bit that doesnt reall work at all...
823 if (this.responsive) {
826 ['xs','sm','md','lg'].map(function(size){
828 if(typeof(config[size]) == 'undefined'){
832 if (!config[size]) { // 0 = hidden
833 // BS 4 '0' is treated as hide that column and below.
834 c.cls += ' hidden-' + size + ' hidden' + size + '-down';
838 c.cls += ' col-' + size + '-' + config[size] + (
839 size == 'xs' ? (' col-' + config[size] ) : '' // bs4 col-{num} replaces col-xs
847 c.html +=' <span class="x-grid-split x-grid-split-' + i + '"></span>';
858 renderBody : function()
868 colspan : this.cm.getColumnCount()
878 renderFooter : function()
888 colspan : this.cm.getColumnCount()
902 // Roo.log('ds onload');
909 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
910 e.select('i', true).removeClass(['fa-arrow-up', 'fa-arrow-down']);
911 if (_this.store.sortInfo) {
913 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
914 e.select('i', true).addClass(['fa-arrow-up']);
917 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
918 e.select('i', true).addClass(['fa-arrow-down']);
923 var tbody = this.bodyEl;
925 if(ds.getCount() > 0){
926 ds.data.each(function(d,rowIndex){
927 var row = this.renderRow(cm, ds, rowIndex);
929 tbody.createChild(row);
933 if(row.cellObjects.length){
934 Roo.each(row.cellObjects, function(r){
935 _this.renderCellObject(r);
942 var tfoot = this.el.select('tfoot', true).first();
944 if(this.footerShow && this.auto_hide_footer && this.mainFoot){
946 this.mainFoot.setVisibilityMode(Roo.Element.DISPLAY).hide();
948 var total = this.ds.getTotalCount();
950 if(this.footer.pageSize < total){
951 this.mainFoot.show();
955 Roo.each(this.el.select('tbody td', true).elements, function(e){
956 e.on('mouseover', _this.onMouseover, _this);
959 Roo.each(this.el.select('tbody td', true).elements, function(e){
960 e.on('mouseout', _this.onMouseout, _this);
962 this.fireEvent('rowsrendered', this);
966 this.initCSS(); /// resize cols
972 onUpdate : function(ds,record)
974 this.refreshRow(record);
978 onRemove : function(ds, record, index, isUpdate){
979 if(isUpdate !== true){
980 this.fireEvent("beforerowremoved", this, index, record);
982 var bt = this.bodyEl.dom;
984 var rows = this.el.select('tbody > tr', true).elements;
986 if(typeof(rows[index]) != 'undefined'){
987 bt.removeChild(rows[index].dom);
990 // if(bt.rows[index]){
991 // bt.removeChild(bt.rows[index]);
994 if(isUpdate !== true){
995 //this.stripeRows(index);
996 //this.syncRowHeights(index, index);
998 this.fireEvent("rowremoved", this, index, record);
1002 onAdd : function(ds, records, rowIndex)
1004 //Roo.log('on Add called');
1005 // - note this does not handle multiple adding very well..
1006 var bt = this.bodyEl.dom;
1007 for (var i =0 ; i < records.length;i++) {
1008 //Roo.log('call insert row Add called on ' + rowIndex + ':' + i);
1009 //Roo.log(records[i]);
1010 //Roo.log(this.store.getAt(rowIndex+i));
1011 this.insertRow(this.store, rowIndex + i, false);
1018 refreshRow : function(record){
1019 var ds = this.store, index;
1020 if(typeof record == 'number'){
1022 record = ds.getAt(index);
1024 index = ds.indexOf(record);
1026 return; // should not happen - but seems to
1029 this.insertRow(ds, index, true);
1031 this.onRemove(ds, record, index+1, true);
1033 //this.syncRowHeights(index, index);
1035 this.fireEvent("rowupdated", this, index, record);
1037 // private - called by RowSelection
1038 onRowSelect : function(rowIndex){
1039 var row = this.getRowDom(rowIndex);
1040 row.addClass(['bg-info','info']);
1042 // private - called by RowSelection
1043 onRowDeselect : function(rowIndex)
1048 var row = this.getRowDom(rowIndex);
1049 row.removeClass(['bg-info','info']);
1052 * Focuses the specified row.
1053 * @param {Number} row The row index
1055 focusRow : function(row)
1057 //Roo.log('GridView.focusRow');
1058 var x = this.bodyEl.dom.scrollLeft;
1059 this.focusCell(row, 0, false);
1060 this.bodyEl.dom.scrollLeft = x;
1064 * Focuses the specified cell.
1065 * @param {Number} row The row index
1066 * @param {Number} col The column index
1067 * @param {Boolean} hscroll false to disable horizontal scrolling
1069 focusCell : function(row, col, hscroll)
1071 //Roo.log('GridView.focusCell');
1072 var el = this.ensureVisible(row, col, hscroll);
1073 // not sure what focusEL achives = it's a <a> pos relative
1074 //this.focusEl.alignTo(el, "tl-tl");
1076 // this.focusEl.focus();
1078 // this.focusEl.focus.defer(1, this.focusEl);
1083 * Scrolls the specified cell into view
1084 * @param {Number} row The row index
1085 * @param {Number} col The column index
1086 * @param {Boolean} hscroll false to disable horizontal scrolling
1088 ensureVisible : function(row, col, hscroll)
1090 //Roo.log('GridView.ensureVisible,' + row + ',' + col);
1091 //return null; //disable for testing.
1092 if(typeof row != "number"){
1095 if(row < 0 && row >= this.ds.getCount()){
1098 col = (col !== undefined ? col : 0);
1100 while(cm.isHidden(col)){
1104 var el = this.getCellDom(row, col);
1108 var c = this.bodyEl.dom;
1110 var ctop = parseInt(el.offsetTop, 10);
1111 var cleft = parseInt(el.offsetLeft, 10);
1112 var cbot = ctop + el.offsetHeight;
1113 var cright = cleft + el.offsetWidth;
1115 //var ch = c.clientHeight - this.mainHd.dom.offsetHeight;
1116 var ch = 0; //?? header is not withing the area?
1117 var stop = parseInt(c.scrollTop, 10);
1118 var sleft = parseInt(c.scrollLeft, 10);
1119 var sbot = stop + ch;
1120 var sright = sleft + c.clientWidth;
1122 Roo.log('GridView.ensureVisible:' +
1124 ' c.clientHeight:' + c.clientHeight +
1125 ' this.mainHd.dom.offsetHeight:' + this.mainHd.dom.offsetHeight +
1134 //Roo.log("set scrolltop to ctop DISABLE?");
1135 }else if(cbot > sbot){
1136 //Roo.log("set scrolltop to cbot-ch");
1137 c.scrollTop = cbot-ch;
1140 if(hscroll !== false){
1142 c.scrollLeft = cleft;
1143 }else if(cright > sright){
1144 c.scrollLeft = cright-c.clientWidth;
1152 insertRow : function(dm, rowIndex, isUpdate){
1155 this.fireEvent("beforerowsinserted", this, rowIndex);
1157 //var s = this.getScrollState();
1158 var row = this.renderRow(this.cm, this.store, rowIndex);
1159 // insert before rowIndex..
1160 var e = this.bodyEl.createChild(row,this.getRowDom(rowIndex));
1164 if(row.cellObjects.length){
1165 Roo.each(row.cellObjects, function(r){
1166 _this.renderCellObject(r);
1171 this.fireEvent("rowsinserted", this, rowIndex);
1172 //this.syncRowHeights(firstRow, lastRow);
1173 //this.stripeRows(firstRow);
1180 getRowDom : function(rowIndex)
1182 var rows = this.el.select('tbody > tr', true).elements;
1184 return (typeof(rows[rowIndex]) == 'undefined') ? false : rows[rowIndex];
1187 getCellDom : function(rowIndex, colIndex)
1189 var row = this.getRowDom(rowIndex);
1190 if (row === false) {
1193 var cols = row.select('td', true).elements;
1194 return (typeof(cols[colIndex]) == 'undefined') ? false : cols[colIndex];
1198 // returns the object tree for a tr..
1201 renderRow : function(cm, ds, rowIndex)
1203 var d = ds.getAt(rowIndex);
1207 cls : 'x-row-' + rowIndex,
1211 var cellObjects = [];
1213 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
1214 var config = cm.config[i];
1216 var renderer = cm.getRenderer(i);
1220 if(typeof(renderer) !== 'undefined'){
1221 value = renderer(d.data[cm.getDataIndex(i)], false, d);
1223 // if object are returned, then they are expected to be Roo.bootstrap.Component instances
1224 // and are rendered into the cells after the row is rendered - using the id for the element.
1226 if(typeof(value) === 'object'){
1236 rowIndex : rowIndex,
1241 this.fireEvent('rowclass', this, rowcfg);
1245 // this might end up displaying HTML?
1246 // this is too messy... - better to only do it on columsn you know are going to be too long
1247 //tooltip : (typeof(value) === 'object') ? '' : value,
1248 cls : rowcfg.rowClass + ' x-col-' + i,
1250 html: (typeof(value) === 'object') ? '' : value
1257 if(typeof(config.colspan) != 'undefined'){
1258 td.colspan = config.colspan;
1263 if(typeof(config.align) != 'undefined' && config.align.length){
1264 td.style += ' text-align:' + config.align + ';';
1266 if(typeof(config.valign) != 'undefined' && config.valign.length){
1267 td.style += ' vertical-align:' + config.valign + ';';
1270 if(typeof(config.width) != 'undefined'){
1271 td.style += ' width:' + config.width + 'px;';
1275 if(typeof(config.cursor) != 'undefined'){
1276 td.style += ' cursor:' + config.cursor + ';';
1279 if(typeof(config.cls) != 'undefined'){
1280 td.cls = (typeof(td.cls) == 'undefined') ? config.cls : (td.cls + ' ' + config.cls);
1282 if (this.responsive) {
1283 ['xs','sm','md','lg'].map(function(size){
1285 if(typeof(config[size]) == 'undefined'){
1291 if (!config[size]) { // 0 = hidden
1292 // BS 4 '0' is treated as hide that column and below.
1293 td.cls += ' hidden-' + size + ' hidden' + size + '-down';
1297 td.cls += ' col-' + size + '-' + config[size] + (
1298 size == 'xs' ? (' col-' + config[size] ) : '' // bs4 col-{num} replaces col-xs
1308 row.cellObjects = cellObjects;
1316 onBeforeLoad : function()
1325 this.el.select('tbody', true).first().dom.innerHTML = '';
1328 * Show or hide a row.
1329 * @param {Number} rowIndex to show or hide
1330 * @param {Boolean} state hide
1332 setRowVisibility : function(rowIndex, state)
1334 var bt = this.bodyEl.dom;
1336 var rows = this.el.select('tbody > tr', true).elements;
1338 if(typeof(rows[rowIndex]) == 'undefined'){
1341 rows[rowIndex][ state ? 'removeClass' : 'addClass']('d-none');
1346 getSelectionModel : function(){
1348 this.selModel = new Roo.bootstrap.Table.RowSelectionModel({grid: this});
1350 return this.selModel;
1353 * Render the Roo.bootstrap object from renderder
1355 renderCellObject : function(r)
1359 r.cfg.parentId = (typeof(r.container) == 'string') ? r.container : r.container.id;
1361 var t = r.cfg.render(r.container);
1364 Roo.each(r.cfg.cn, function(c){
1366 container: t.getChildContainer(),
1369 _this.renderCellObject(child);
1374 * get the Row Index from a dom element.
1375 * @param {Roo.Element} row The row to look for
1376 * @returns {Number} the row
1378 getRowIndex : function(row)
1382 Roo.each(this.el.select('tbody > tr', true).elements, function(el, index){
1393 * get the header TH element for columnIndex
1394 * @param {Number} columnIndex
1395 * @returns {Roo.Element}
1397 getHeaderIndex: function(colIndex)
1399 var cols = this.headEl.select('th', true).elements;
1400 return cols[colIndex];
1403 * get the Column Index from a dom element. (using regex on x-hcol-{colid})
1404 * @param {domElement} cell to look for
1405 * @returns {Number} the column
1407 getCellIndex : function(cell)
1409 var id = String(cell.className).match(Roo.bootstrap.Table.cellRE);
1411 return parseInt(id[1], 10);
1416 * Returns the grid's underlying element = used by panel.Grid
1417 * @return {Element} The element
1419 getGridEl : function(){
1423 * Forces a resize - used by panel.Grid
1424 * @return {Element} The element
1426 autoSize : function()
1428 //var ctr = Roo.get(this.container.dom.parentElement);
1429 var ctr = Roo.get(this.el.dom);
1431 var thd = this.getGridEl().select('thead',true).first();
1432 var tbd = this.getGridEl().select('tbody', true).first();
1433 var tfd = this.getGridEl().select('tfoot', true).first();
1435 var cw = ctr.getWidth();
1436 this.getGridEl().select('tfoot tr, tfoot td',true).setWidth(cw);
1440 tbd.setWidth(ctr.getWidth());
1441 // if the body has a max height - and then scrolls - we should perhaps set up the height here
1442 // this needs fixing for various usage - currently only hydra job advers I think..
1444 // ctr.getHeight() - ((thd ? thd.getHeight() : 0) + (tfd ? tfd.getHeight() : 0))
1446 var barsize = (tbd.dom.offsetWidth - tbd.dom.clientWidth);
1449 cw = Math.max(cw, this.totalWidth);
1450 this.getGridEl().select('tbody tr',true).setWidth(cw);
1453 // resize 'expandable coloumn?
1455 return; // we doe not have a view in this design..
1458 onBodyScroll: function()
1460 //Roo.log("body scrolled');" + this.bodyEl.dom.scrollLeft);
1462 this.headEl.setStyle({
1463 'position' : 'relative',
1464 'left': (-1* this.bodyEl.dom.scrollLeft) + 'px'
1470 var scrollHeight = this.bodyEl.dom.scrollHeight;
1472 var scrollTop = Math.ceil(this.bodyEl.getScroll().top);
1474 var height = this.bodyEl.getHeight();
1476 if(scrollHeight - height == scrollTop) {
1478 var total = this.ds.getTotalCount();
1480 if(this.footer.cursor + this.footer.pageSize < total){
1482 this.footer.ds.load({
1484 start : this.footer.cursor + this.footer.pageSize,
1485 limit : this.footer.pageSize
1494 onColumnSplitterMoved : function(i, diff)
1496 this.userResized = true;
1498 var cm = this.colModel;
1500 var w = this.getHeaderIndex(i).getWidth() + diff;
1503 cm.setColumnWidth(i, w, true);
1505 //var cid = cm.getColumnId(i); << not used in this version?
1506 /* Roo.log(['#' + this.id + ' .x-col-' + i, "width", w + "px"]);
1508 this.CSS.updateRule( '#' + this.id + ' .x-col-' + i, "width", w + "px");
1509 this.CSS.updateRule('#' + this.id + ' .x-hcol-' + i, "width", w + "px");
1510 this.CSS.updateRule('#' + this.id + ' .x-grid-split-' + i, "left", w + "px");
1512 //this.updateSplitters();
1513 //this.layout(); << ??
1514 this.fireEvent("columnresize", i, w);
1516 onHeaderChange : function()
1518 var header = this.renderHeader();
1519 var table = this.el.select('table', true).first();
1521 this.headEl.remove();
1522 this.headEl = table.createChild(header, this.bodyEl, false);
1524 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
1525 e.on('click', this.sort, this);
1528 if(this.enableColumnResize !== false && Roo.grid.SplitDragZone){
1529 new Roo.grid.SplitDragZone(this, this.headEl.dom, false); // not sure what 'lockedHd is for this implementation..)
1534 onHiddenChange : function(colModel, colIndex, hidden)
1538 var thSelector = '#' + this.id + ' .x-hcol-' + colIndex;
1539 var tdSelector = '#' + this.id + ' .x-col-' + colIndex;
1541 this.CSS.updateRule(thSelector, "display", "");
1542 this.CSS.updateRule(tdSelector, "display", "");
1545 this.CSS.updateRule(thSelector, "display", "none");
1546 this.CSS.updateRule(tdSelector, "display", "none");
1549 // onload calls initCSS()
1550 this.onHeaderChange();
1554 setColumnWidth: function(col_index, width)
1556 // width = "md-2 xs-2..."
1557 if(!this.colModel.config[col_index]) {
1561 var w = width.split(" ");
1563 var rows = this.el.dom.getElementsByClassName("x-col-"+col_index);
1565 var h_row = this.el.dom.getElementsByClassName("x-hcol-"+col_index);
1568 for(var j = 0; j < w.length; j++) {
1574 var size_cls = w[j].split("-");
1576 if(!Number.isInteger(size_cls[1] * 1)) {
1580 if(!this.colModel.config[col_index][size_cls[0]]) {
1584 if(!h_row[0].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1588 h_row[0].classList.replace(
1589 "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1590 "col-"+size_cls[0]+"-"+size_cls[1]
1593 for(var i = 0; i < rows.length; i++) {
1595 var size_cls = w[j].split("-");
1597 if(!Number.isInteger(size_cls[1] * 1)) {
1601 if(!this.colModel.config[col_index][size_cls[0]]) {
1605 if(!rows[i].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1609 rows[i].classList.replace(
1610 "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1611 "col-"+size_cls[0]+"-"+size_cls[1]
1615 this.colModel.config[col_index][size_cls[0]] = size_cls[1];
1620 // currently only used to find the split on drag..
1621 Roo.bootstrap.Table.cellRE = /(?:.*?)x-grid-(?:hd|cell|split)-([\d]+)(?:.*?)/;
1626 Roo.bootstrap.Table.AbstractSelectionModel = Roo.grid.AbstractSelectionModel;
1627 Roo.bootstrap.Table.RowSelectionModel = Roo.grid.RowSelectionModel;