2 * @class Roo.bootstrap.Table
4 * @extends Roo.bootstrap.Component
5 * Bootstrap Table class. This class represents the primary interface of a component based grid control.
6 * Similar to Roo.grid.Grid
8 var table = Roo.factory({
11 autoSizeColumns: true,
18 sortInfo : { direction : 'ASC', field: 'name' },
23 url : 'https://example.com/some.data.url.json'
28 fields : [ 'id', 'name', whatever' ],
35 xtype : 'ColumnModel',
39 dataIndex : 'is_in_group',
42 renderer : function(v, x , r) {
44 return String.format("{0}", v)
50 xtype : 'RowSelectionModel',
51 xns : Roo.bootstrap.Table
52 // you can add listeners to catch selection change here....
58 grid.render(Roo.get("some-div"));
61 Currently the Table uses multiple headers to try and handle XL / Medium etc... styling
66 * @cfg {Roo.grid.RowSelectionModel|Roo.grid.CellSelectionModel} sm The selection model to use (cell selection is not supported yet)
67 * @cfg {Roo.data.Store|Roo.data.SimpleStore} store The data store to use
68 * @cfg {Roo.grid.ColumnModel} cm[] A column for th grid.
70 * @cfg {String} cls table class
73 * @cfg {boolean} striped Should the rows be alternative striped
74 * @cfg {boolean} bordered Add borders to the table
75 * @cfg {boolean} hover Add hover highlighting
76 * @cfg {boolean} condensed Format condensed
77 * @cfg {boolean} responsive Format condensed
78 * @cfg {Boolean} loadMask (true|false) default false
79 * @cfg {Boolean} footerShow (true|false) generate tfoot, default true
80 * @cfg {Boolean} headerShow (true|false) generate thead, default true
81 * @cfg {Boolean} rowSelection (true|false) default false
82 * @cfg {Boolean} cellSelection (true|false) default false
83 * @cfg {Boolean} scrollBody (true|false) default false - body scrolled / fixed header
84 * @cfg {Roo.bootstrap.PagingToolbar} footer a paging toolbar
85 * @cfg {Boolean} lazyLoad auto load data while scrolling to the end (default false)
86 * @cfg {Boolean} auto_hide_footer auto hide footer if only one page (default false)
87 * @cfg {Boolean} enableColumnResize default true if columns can be resized (drag/drop)
88 * @cfg {Number} minColumnWidth default 50 pixels minimum column width
92 * @param {Object} config The config object
95 Roo.bootstrap.Table = function(config)
97 Roo.bootstrap.Table.superclass.constructor.call(this, config);
100 this.rowSelection = (typeof(config.rowSelection) != 'undefined') ? config.rowSelection : this.rowSelection;
101 this.cellSelection = (typeof(config.cellSelection) != 'undefined') ? config.cellSelection : this.cellSelection;
102 this.headerShow = (typeof(config.thead) != 'undefined') ? config.thead : this.headerShow;
103 this.footerShow = (typeof(config.tfoot) != 'undefined') ? config.tfoot : this.footerShow;
105 this.view = this; // compat with grid.
107 this.sm = this.sm || {xtype: 'RowSelectionModel'};
110 this.selModel = Roo.factory(this.sm, Roo.grid);
111 this.sm = this.selModel;
112 this.sm.xmodule = this.xmodule || false;
115 if (this.cm && typeof(this.cm.config) == 'undefined') {
116 this.colModel = new Roo.grid.ColumnModel(this.cm);
117 this.cm = this.colModel;
118 this.cm.xmodule = this.xmodule || false;
121 this.store= Roo.factory(this.store, Roo.data);
122 this.ds = this.store;
123 this.ds.xmodule = this.xmodule || false;
126 if (this.footer && this.store) {
127 this.footer.dataSource = this.ds;
128 this.footer = Roo.factory(this.footer);
135 * Fires when a cell is clicked
136 * @param {Roo.bootstrap.Table} this
137 * @param {Roo.Element} el
138 * @param {Number} rowIndex
139 * @param {Number} columnIndex
140 * @param {Roo.EventObject} e
144 * @event celldblclick
145 * Fires when a cell is double clicked
146 * @param {Roo.bootstrap.Table} this
147 * @param {Roo.Element} el
148 * @param {Number} rowIndex
149 * @param {Number} columnIndex
150 * @param {Roo.EventObject} e
152 "celldblclick" : true,
155 * Fires when a row is clicked
156 * @param {Roo.bootstrap.Table} this
157 * @param {Roo.Element} el
158 * @param {Number} rowIndex
159 * @param {Roo.EventObject} e
164 * Fires when a row is double clicked
165 * @param {Roo.bootstrap.Table} this
166 * @param {Roo.Element} el
167 * @param {Number} rowIndex
168 * @param {Roo.EventObject} e
170 "rowdblclick" : true,
173 * Fires when a mouseover occur
174 * @param {Roo.bootstrap.Table} this
175 * @param {Roo.Element} el
176 * @param {Number} rowIndex
177 * @param {Number} columnIndex
178 * @param {Roo.EventObject} e
183 * Fires when a mouseout occur
184 * @param {Roo.bootstrap.Table} this
185 * @param {Roo.Element} el
186 * @param {Number} rowIndex
187 * @param {Number} columnIndex
188 * @param {Roo.EventObject} e
193 * Fires when a row is rendered, so you can change add a style to it.
194 * @param {Roo.bootstrap.Table} this
195 * @param {Object} rowcfg contains record rowIndex colIndex and rowClass - set rowClass to add a style.
199 * @event rowsrendered
200 * Fires when all the rows have been rendered
201 * @param {Roo.bootstrap.Table} this
203 'rowsrendered' : true,
206 * The raw contextmenu event for the entire grid.
207 * @param {Roo.EventObject} e
209 "contextmenu" : true,
211 * @event rowcontextmenu
212 * Fires when a row is right clicked
213 * @param {Roo.bootstrap.Table} this
214 * @param {Number} rowIndex
215 * @param {Roo.EventObject} e
217 "rowcontextmenu" : true,
219 * @event cellcontextmenu
220 * Fires when a cell is right clicked
221 * @param {Roo.bootstrap.Table} this
222 * @param {Number} rowIndex
223 * @param {Number} cellIndex
224 * @param {Roo.EventObject} e
226 "cellcontextmenu" : true,
228 * @event headercontextmenu
229 * Fires when a header is right clicked
230 * @param {Roo.bootstrap.Table} this
231 * @param {Number} columnIndex
232 * @param {Roo.EventObject} e
234 "headercontextmenu" : true,
237 * The raw mousedown event for the entire grid.
238 * @param {Roo.EventObject} e
245 Roo.extend(Roo.bootstrap.Table, Roo.bootstrap.Component, {
261 enableColumnResize: true,
263 rowSelection : false,
264 cellSelection : false,
269 // Roo.Element - the tbody
270 bodyEl: false, // <tbody> Roo.Element - thead element
271 headEl: false, // <thead> Roo.Element - thead element
272 resizeProxy : false, // proxy element for dragging?
276 container: false, // used by gridpanel...
282 auto_hide_footer : false,
284 view: false, // actually points to this..
286 getAutoCreate : function()
288 var cfg = Roo.apply({}, Roo.bootstrap.Table.superclass.getAutoCreate.call(this));
295 // this get's auto added by panel.Grid
296 if (this.scrollBody) {
297 cfg.cls += ' table-body-fixed';
300 cfg.cls += ' table-striped';
304 cfg.cls += ' table-hover';
307 cfg.cls += ' table-bordered';
309 if (this.condensed) {
310 cfg.cls += ' table-condensed';
313 if (this.responsive) {
314 cfg.cls += ' table-responsive';
318 cfg.cls+= ' ' +this.cls;
324 cfg.style = (typeof(cfg.style) == 'undefined') ? ('table-layout:' + this.layout + ';') : (cfg.style + ('table-layout:' + this.layout + ';'));
327 if(this.store || this.cm){
329 cfg.cn.push(this.renderHeader());
332 cfg.cn.push(this.renderBody());
335 cfg.cn.push(this.renderFooter());
337 // where does this come from?
338 //cfg.cls+= ' TableGrid';
341 return { cn : [ cfg ] };
344 initEvents : function()
346 if(!this.store || !this.cm){
350 this.selModel.initEvents();
354 //Roo.log('initEvents with ds!!!!');
356 this.bodyEl = this.el.select('tbody', true).first();
357 this.headEl = this.el.select('thead', true).first();
358 this.mainFoot = this.el.select('tfoot', true).first();
363 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
364 e.on('click', this.sort, this);
368 // why is this done????? = it breaks dialogs??
369 //this.parent().el.setStyle('position', 'relative');
373 this.footer.parentId = this.id;
374 this.footer.onRender(this.el.select('tfoot tr td').first(), null);
377 this.el.select('tfoot tr td').first().addClass('hide');
382 this.maskEl = new Roo.LoadMask(this.el, { store : this.ds, msgCls: 'roo-el-mask-msg' });
385 this.store.on('load', this.onLoad, this);
386 this.store.on('beforeload', this.onBeforeLoad, this);
387 this.store.on('update', this.onUpdate, this);
388 this.store.on('add', this.onAdd, this);
389 this.store.on("clear", this.clear, this);
391 this.el.on("contextmenu", this.onContextMenu, this);
394 this.cm.on("headerchange", this.onHeaderChange, this);
395 this.cm.on("hiddenchange", this.onHiddenChange, this, arguments);
397 //?? does bodyEl get replaced on render?
398 this.bodyEl.on("click", this.onClick, this);
399 this.bodyEl.on("dblclick", this.onDblClick, this);
400 this.bodyEl.on('scroll', this.onBodyScroll, this);
402 // guessing mainbody will work - this relays usually caught by selmodel at present.
403 this.relayEvents(this.bodyEl, ["mousedown","mouseup","mouseover","mouseout","keypress"]);
406 this.resizeProxy = Roo.get(document.body).createChild({ cls:"x-grid-resize-proxy", html: ' ' });
409 if(this.headEl && this.enableColumnResize !== false && Roo.grid.SplitDragZone){
410 new Roo.grid.SplitDragZone(this, this.headEl.dom, false); // not sure what 'lockedHd is for this implementation..)
415 // Compatibility with grid - we implement all the view features at present.
425 var cm = this.cm, styles = [];
426 this.CSS.removeStyleSheet(this.id + '-cssrules');
427 var headHeight = this.headEl ? this.headEl.dom.clientHeight : 0;
428 // we can honour xs/sm/md/xl as widths...
429 // we first have to decide what widht we are currently at...
430 var sz = Roo.getGridSize();
434 var cols = []; // visable cols.
436 for(var i = 0, len = cm.getColumnCount(); i < len; i++) {
437 var w = cm.getColumnWidth(i, false);
439 cols.push( { rel : false, abs : 0 });
443 cols.push( { rel : false, abs : w });
445 last = i; // not really..
448 var w = cm.getColumnWidth(i, sz);
453 cols.push( { rel : w, abs : false });
456 var avail = this.bodyEl.dom.clientWidth - total_abs;
458 var unitWidth = Math.floor(avail / total);
459 var rem = avail - (unitWidth * total);
461 var hidden, width, pos = 0 , splithide , left;
462 for(var i = 0, len = cm.getColumnCount(); i < len; i++) {
464 hidden = 'display:none;';
466 width = 'width:0px;';
472 // we can honour xs/sm/md/xl ?
473 var w = cols[i].rel == false ? cols[i].abs : (cols[i].rel * unitWidth);
475 hidden = 'display:none;';
477 // width should return a small number...
479 w+=rem; // add the remaining with..
482 left = "left:" + (pos -4) + "px;";
483 width = "width:" + w+ "px;";
486 if (this.responsive) {
489 hidden = cm.isHidden(i) ? 'display:none' : '';
490 splithide = 'display: none';
493 styles.push( '#' , this.id , ' .x-col-' , i, " {", cm.config[i].css, width, hidden, "}\n" );
496 splithide = 'display:none;';
499 styles.push('#' , this.id , ' .x-hcol-' , i, " { ", width, hidden," }\n",
500 '#' , this.id , ' .x-grid-split-' , i, " { ", left, splithide,'height:', (headHeight - 4), "px;}\n"
505 //Roo.log(styles.join(''));
506 this.CSS.createStyleSheet( styles.join(''), this.id + '-cssrules');
512 onContextMenu : function(e, t)
514 this.processEvent("contextmenu", e);
517 processEvent : function(name, e)
519 if (name != 'touchstart' ) {
520 this.fireEvent(name, e);
523 var t = e.getTarget();
525 var cell = Roo.get(t);
531 if(cell.findParent('tfoot', false, true)){
535 if(cell.findParent('thead', false, true)){
537 if(e.getTarget().nodeName.toLowerCase() != 'th'){
538 cell = Roo.get(t).findParent('th', false, true);
540 Roo.log("failed to find th in thead?");
541 Roo.log(e.getTarget());
546 var cellIndex = cell.dom.cellIndex;
548 var ename = name == 'touchstart' ? 'click' : name;
549 this.fireEvent("header" + ename, this, cellIndex, e);
554 if(e.getTarget().nodeName.toLowerCase() != 'td'){
555 cell = Roo.get(t).findParent('td', false, true);
557 Roo.log("failed to find th in tbody?");
558 Roo.log(e.getTarget());
563 var row = cell.findParent('tr', false, true);
564 var cellIndex = cell.dom.cellIndex;
565 var rowIndex = row.dom.rowIndex - 1;
569 this.fireEvent("row" + name, this, rowIndex, e);
573 this.fireEvent("cell" + name, this, rowIndex, cellIndex, e);
579 onMouseover : function(e, el)
581 var cell = Roo.get(el);
587 if(e.getTarget().nodeName.toLowerCase() != 'td'){
588 cell = cell.findParent('td', false, true);
591 var row = cell.findParent('tr', false, true);
592 var cellIndex = cell.dom.cellIndex;
593 var rowIndex = row.dom.rowIndex - 1; // start from 0
595 this.fireEvent('mouseover', this, cell, rowIndex, cellIndex, e);
599 onMouseout : function(e, el)
601 var cell = Roo.get(el);
607 if(e.getTarget().nodeName.toLowerCase() != 'td'){
608 cell = cell.findParent('td', false, true);
611 var row = cell.findParent('tr', false, true);
612 var cellIndex = cell.dom.cellIndex;
613 var rowIndex = row.dom.rowIndex - 1; // start from 0
615 this.fireEvent('mouseout', this, cell, rowIndex, cellIndex, e);
619 onClick : function(e, el)
621 var cell = Roo.get(el);
623 if(!cell || (!this.cellSelection && !this.rowSelection)){
627 if(e.getTarget().nodeName.toLowerCase() != 'td'){
628 cell = cell.findParent('td', false, true);
631 if(!cell || typeof(cell) == 'undefined'){
635 var row = cell.findParent('tr', false, true);
637 if(!row || typeof(row) == 'undefined'){
641 var cellIndex = cell.dom.cellIndex;
642 var rowIndex = this.getRowIndex(row);
644 // why??? - should these not be based on SelectionModel?
645 //if(this.cellSelection){
646 this.fireEvent('cellclick', this, cell, rowIndex, cellIndex, e);
649 //if(this.rowSelection){
650 this.fireEvent('rowclick', this, row, rowIndex, e);
655 onDblClick : function(e,el)
657 var cell = Roo.get(el);
659 if(!cell || (!this.cellSelection && !this.rowSelection)){
663 if(e.getTarget().nodeName.toLowerCase() != 'td'){
664 cell = cell.findParent('td', false, true);
667 if(!cell || typeof(cell) == 'undefined'){
671 var row = cell.findParent('tr', false, true);
673 if(!row || typeof(row) == 'undefined'){
677 var cellIndex = cell.dom.cellIndex;
678 var rowIndex = this.getRowIndex(row);
680 if(this.cellSelection){
681 this.fireEvent('celldblclick', this, cell, rowIndex, cellIndex, e);
684 if(this.rowSelection){
685 this.fireEvent('rowdblclick', this, row, rowIndex, e);
688 findRowIndex : function(el)
690 var cell = Roo.get(el);
694 var row = cell.findParent('tr', false, true);
696 if(!row || typeof(row) == 'undefined'){
699 return this.getRowIndex(row);
701 sort : function(e,el)
703 var col = Roo.get(el);
705 if(!col.hasClass('sortable')){
709 var sort = col.attr('sort');
712 if(col.select('i', true).first().hasClass('fa-arrow-up')){
716 this.store.sortInfo = {field : sort, direction : dir};
719 Roo.log("calling footer first");
720 this.footer.onClick('first');
723 this.store.load({ params : { start : 0 } });
727 renderHeader : function()
737 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
739 var config = cm.config[i];
746 html: cm.getColumnHeader(i)
749 var tooltip = cm.getColumnTooltip(i);
757 if(typeof(config.sortable) != 'undefined' && config.sortable){
758 c.cls += ' sortable';
759 c.html = '<i class="fa"></i>' + c.html;
762 // could use BS4 hidden-..-down
764 if(typeof(config.lgHeader) != 'undefined'){
765 hh += '<span class="hidden-xs hidden-sm hidden-md ">' + config.lgHeader + '</span>';
768 if(typeof(config.mdHeader) != 'undefined'){
769 hh += '<span class="hidden-xs hidden-sm hidden-lg">' + config.mdHeader + '</span>';
772 if(typeof(config.smHeader) != 'undefined'){
773 hh += '<span class="hidden-xs hidden-md hidden-lg">' + config.smHeader + '</span>';
776 if(typeof(config.xsHeader) != 'undefined'){
777 hh += '<span class="hidden-sm hidden-md hidden-lg">' + config.xsHeader + '</span>';
784 if(typeof(config.tooltip) != 'undefined'){
785 c.tooltip = config.tooltip;
788 if(typeof(config.colspan) != 'undefined'){
789 c.colspan = config.colspan;
792 // hidden is handled by CSS now
794 if(typeof(config.dataIndex) != 'undefined'){
795 c.sort = config.dataIndex;
800 if(typeof(config.align) != 'undefined' && config.align.length){
801 c.style += ' text-align:' + config.align + ';';
804 /* width is done in CSS
805 *if(typeof(config.width) != 'undefined'){
806 c.style += ' width:' + config.width + 'px;';
807 this.totalWidth += config.width;
809 this.totalWidth += 100; // assume minimum of 100 per column?
813 if(typeof(config.cls) != 'undefined'){
814 c.cls = (typeof(c.cls) == 'undefined') ? config.cls : (c.cls + ' ' + config.cls);
816 // this is the bit that doesnt reall work at all...
818 if (this.responsive) {
821 ['xs','sm','md','lg'].map(function(size){
823 if(typeof(config[size]) == 'undefined'){
827 if (!config[size]) { // 0 = hidden
828 // BS 4 '0' is treated as hide that column and below.
829 c.cls += ' hidden-' + size + ' hidden' + size + '-down';
833 c.cls += ' col-' + size + '-' + config[size] + (
834 size == 'xs' ? (' col-' + config[size] ) : '' // bs4 col-{num} replaces col-xs
842 c.html +=' <span class="x-grid-split x-grid-split-' + i + '"></span>';
853 renderBody : function()
863 colspan : this.cm.getColumnCount()
873 renderFooter : function()
883 colspan : this.cm.getColumnCount()
897 // Roo.log('ds onload');
904 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
905 e.select('i', true).removeClass(['fa-arrow-up', 'fa-arrow-down']);
906 if (_this.store.sortInfo) {
908 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'ASC'){
909 e.select('i', true).addClass(['fa-arrow-up']);
912 if(e.hasClass('sortable') && e.attr('sort') == _this.store.sortInfo.field && _this.store.sortInfo.direction.toUpperCase() == 'DESC'){
913 e.select('i', true).addClass(['fa-arrow-down']);
918 var tbody = this.bodyEl;
920 if(ds.getCount() > 0){
921 ds.data.each(function(d,rowIndex){
922 var row = this.renderRow(cm, ds, rowIndex);
924 tbody.createChild(row);
928 if(row.cellObjects.length){
929 Roo.each(row.cellObjects, function(r){
930 _this.renderCellObject(r);
937 var tfoot = this.el.select('tfoot', true).first();
939 if(this.footerShow && this.auto_hide_footer && this.mainFoot){
941 this.mainFoot.setVisibilityMode(Roo.Element.DISPLAY).hide();
943 var total = this.ds.getTotalCount();
945 if(this.footer.pageSize < total){
946 this.mainFoot.show();
950 Roo.each(this.el.select('tbody td', true).elements, function(e){
951 e.on('mouseover', _this.onMouseover, _this);
954 Roo.each(this.el.select('tbody td', true).elements, function(e){
955 e.on('mouseout', _this.onMouseout, _this);
957 this.fireEvent('rowsrendered', this);
961 this.initCSS(); /// resize cols
967 onUpdate : function(ds,record)
969 this.refreshRow(record);
973 onRemove : function(ds, record, index, isUpdate){
974 if(isUpdate !== true){
975 this.fireEvent("beforerowremoved", this, index, record);
977 var bt = this.bodyEl.dom;
979 var rows = this.el.select('tbody > tr', true).elements;
981 if(typeof(rows[index]) != 'undefined'){
982 bt.removeChild(rows[index].dom);
985 // if(bt.rows[index]){
986 // bt.removeChild(bt.rows[index]);
989 if(isUpdate !== true){
990 //this.stripeRows(index);
991 //this.syncRowHeights(index, index);
993 this.fireEvent("rowremoved", this, index, record);
997 onAdd : function(ds, records, rowIndex)
999 //Roo.log('on Add called');
1000 // - note this does not handle multiple adding very well..
1001 var bt = this.bodyEl.dom;
1002 for (var i =0 ; i < records.length;i++) {
1003 //Roo.log('call insert row Add called on ' + rowIndex + ':' + i);
1004 //Roo.log(records[i]);
1005 //Roo.log(this.store.getAt(rowIndex+i));
1006 this.insertRow(this.store, rowIndex + i, false);
1013 refreshRow : function(record){
1014 var ds = this.store, index;
1015 if(typeof record == 'number'){
1017 record = ds.getAt(index);
1019 index = ds.indexOf(record);
1021 return; // should not happen - but seems to
1024 this.insertRow(ds, index, true);
1026 this.onRemove(ds, record, index+1, true);
1028 //this.syncRowHeights(index, index);
1030 this.fireEvent("rowupdated", this, index, record);
1032 // private - called by RowSelection
1033 onRowSelect : function(rowIndex){
1034 var row = this.getRowDom(rowIndex);
1035 row.addClass(['bg-info','info']);
1037 // private - called by RowSelection
1038 onRowDeselect : function(rowIndex)
1043 var row = this.getRowDom(rowIndex);
1044 row.removeClass(['bg-info','info']);
1047 * Focuses the specified row.
1048 * @param {Number} row The row index
1050 focusRow : function(row)
1052 //Roo.log('GridView.focusRow');
1053 var x = this.bodyEl.dom.scrollLeft;
1054 this.focusCell(row, 0, false);
1055 this.bodyEl.dom.scrollLeft = x;
1059 * Focuses the specified cell.
1060 * @param {Number} row The row index
1061 * @param {Number} col The column index
1062 * @param {Boolean} hscroll false to disable horizontal scrolling
1064 focusCell : function(row, col, hscroll)
1066 //Roo.log('GridView.focusCell');
1067 var el = this.ensureVisible(row, col, hscroll);
1068 // not sure what focusEL achives = it's a <a> pos relative
1069 //this.focusEl.alignTo(el, "tl-tl");
1071 // this.focusEl.focus();
1073 // this.focusEl.focus.defer(1, this.focusEl);
1078 * Scrolls the specified cell into view
1079 * @param {Number} row The row index
1080 * @param {Number} col The column index
1081 * @param {Boolean} hscroll false to disable horizontal scrolling
1083 ensureVisible : function(row, col, hscroll)
1085 //Roo.log('GridView.ensureVisible,' + row + ',' + col);
1086 //return null; //disable for testing.
1087 if(typeof row != "number"){
1090 if(row < 0 && row >= this.ds.getCount()){
1093 col = (col !== undefined ? col : 0);
1095 while(cm.isHidden(col)){
1099 var el = this.getCellDom(row, col);
1103 var c = this.bodyEl.dom;
1105 var ctop = parseInt(el.offsetTop, 10);
1106 var cleft = parseInt(el.offsetLeft, 10);
1107 var cbot = ctop + el.offsetHeight;
1108 var cright = cleft + el.offsetWidth;
1110 //var ch = c.clientHeight - this.mainHd.dom.offsetHeight;
1111 var ch = 0; //?? header is not withing the area?
1112 var stop = parseInt(c.scrollTop, 10);
1113 var sleft = parseInt(c.scrollLeft, 10);
1114 var sbot = stop + ch;
1115 var sright = sleft + c.clientWidth;
1117 Roo.log('GridView.ensureVisible:' +
1119 ' c.clientHeight:' + c.clientHeight +
1120 ' this.mainHd.dom.offsetHeight:' + this.mainHd.dom.offsetHeight +
1129 //Roo.log("set scrolltop to ctop DISABLE?");
1130 }else if(cbot > sbot){
1131 //Roo.log("set scrolltop to cbot-ch");
1132 c.scrollTop = cbot-ch;
1135 if(hscroll !== false){
1137 c.scrollLeft = cleft;
1138 }else if(cright > sright){
1139 c.scrollLeft = cright-c.clientWidth;
1147 insertRow : function(dm, rowIndex, isUpdate){
1150 this.fireEvent("beforerowsinserted", this, rowIndex);
1152 //var s = this.getScrollState();
1153 var row = this.renderRow(this.cm, this.store, rowIndex);
1154 // insert before rowIndex..
1155 var e = this.bodyEl.createChild(row,this.getRowDom(rowIndex));
1159 if(row.cellObjects.length){
1160 Roo.each(row.cellObjects, function(r){
1161 _this.renderCellObject(r);
1166 this.fireEvent("rowsinserted", this, rowIndex);
1167 //this.syncRowHeights(firstRow, lastRow);
1168 //this.stripeRows(firstRow);
1175 getRowDom : function(rowIndex)
1177 var rows = this.el.select('tbody > tr', true).elements;
1179 return (typeof(rows[rowIndex]) == 'undefined') ? false : rows[rowIndex];
1182 getCellDom : function(rowIndex, colIndex)
1184 var row = this.getRowDom(rowIndex);
1185 if (row === false) {
1188 var cols = row.select('td', true).elements;
1189 return (typeof(cols[colIndex]) == 'undefined') ? false : cols[colIndex];
1193 // returns the object tree for a tr..
1196 renderRow : function(cm, ds, rowIndex)
1198 var d = ds.getAt(rowIndex);
1202 cls : 'x-row-' + rowIndex,
1206 var cellObjects = [];
1208 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
1209 var config = cm.config[i];
1211 var renderer = cm.getRenderer(i);
1215 if(typeof(renderer) !== 'undefined'){
1216 value = renderer(d.data[cm.getDataIndex(i)], false, d);
1218 // if object are returned, then they are expected to be Roo.bootstrap.Component instances
1219 // and are rendered into the cells after the row is rendered - using the id for the element.
1221 if(typeof(value) === 'object'){
1231 rowIndex : rowIndex,
1236 this.fireEvent('rowclass', this, rowcfg);
1240 // this might end up displaying HTML?
1241 // this is too messy... - better to only do it on columsn you know are going to be too long
1242 //tooltip : (typeof(value) === 'object') ? '' : value,
1243 cls : rowcfg.rowClass + ' x-col-' + i,
1245 html: (typeof(value) === 'object') ? '' : value
1252 if(typeof(config.colspan) != 'undefined'){
1253 td.colspan = config.colspan;
1258 if(typeof(config.align) != 'undefined' && config.align.length){
1259 td.style += ' text-align:' + config.align + ';';
1261 if(typeof(config.valign) != 'undefined' && config.valign.length){
1262 td.style += ' vertical-align:' + config.valign + ';';
1265 if(typeof(config.width) != 'undefined'){
1266 td.style += ' width:' + config.width + 'px;';
1270 if(typeof(config.cursor) != 'undefined'){
1271 td.style += ' cursor:' + config.cursor + ';';
1274 if(typeof(config.cls) != 'undefined'){
1275 td.cls = (typeof(td.cls) == 'undefined') ? config.cls : (td.cls + ' ' + config.cls);
1277 if (this.responsive) {
1278 ['xs','sm','md','lg'].map(function(size){
1280 if(typeof(config[size]) == 'undefined'){
1286 if (!config[size]) { // 0 = hidden
1287 // BS 4 '0' is treated as hide that column and below.
1288 td.cls += ' hidden-' + size + ' hidden' + size + '-down';
1292 td.cls += ' col-' + size + '-' + config[size] + (
1293 size == 'xs' ? (' col-' + config[size] ) : '' // bs4 col-{num} replaces col-xs
1303 row.cellObjects = cellObjects;
1311 onBeforeLoad : function()
1320 this.el.select('tbody', true).first().dom.innerHTML = '';
1323 * Show or hide a row.
1324 * @param {Number} rowIndex to show or hide
1325 * @param {Boolean} state hide
1327 setRowVisibility : function(rowIndex, state)
1329 var bt = this.bodyEl.dom;
1331 var rows = this.el.select('tbody > tr', true).elements;
1333 if(typeof(rows[rowIndex]) == 'undefined'){
1336 rows[rowIndex][ state ? 'removeClass' : 'addClass']('d-none');
1341 getSelectionModel : function(){
1343 this.selModel = new Roo.bootstrap.Table.RowSelectionModel({grid: this});
1345 return this.selModel;
1348 * Render the Roo.bootstrap object from renderder
1350 renderCellObject : function(r)
1354 r.cfg.parentId = (typeof(r.container) == 'string') ? r.container : r.container.id;
1356 var t = r.cfg.render(r.container);
1359 Roo.each(r.cfg.cn, function(c){
1361 container: t.getChildContainer(),
1364 _this.renderCellObject(child);
1369 * get the Row Index from a dom element.
1370 * @param {Roo.Element} row The row to look for
1371 * @returns {Number} the row
1373 getRowIndex : function(row)
1377 Roo.each(this.el.select('tbody > tr', true).elements, function(el, index){
1388 * get the header TH element for columnIndex
1389 * @param {Number} columnIndex
1390 * @returns {Roo.Element}
1392 getHeaderIndex: function(colIndex)
1394 var cols = this.headEl.select('th', true).elements;
1395 return cols[colIndex];
1398 * get the Column Index from a dom element. (using regex on x-hcol-{colid})
1399 * @param {domElement} cell to look for
1400 * @returns {Number} the column
1402 getCellIndex : function(cell)
1404 var id = String(cell.className).match(Roo.bootstrap.Table.cellRE);
1406 return parseInt(id[1], 10);
1411 * Returns the grid's underlying element = used by panel.Grid
1412 * @return {Element} The element
1414 getGridEl : function(){
1418 * Forces a resize - used by panel.Grid
1419 * @return {Element} The element
1421 autoSize : function()
1423 //var ctr = Roo.get(this.container.dom.parentElement);
1424 var ctr = Roo.get(this.el.dom);
1426 var thd = this.getGridEl().select('thead',true).first();
1427 var tbd = this.getGridEl().select('tbody', true).first();
1428 var tfd = this.getGridEl().select('tfoot', true).first();
1430 var cw = ctr.getWidth();
1431 this.getGridEl().select('tfoot tr, tfoot td',true).setWidth(cw);
1435 tbd.setWidth(ctr.getWidth());
1436 // if the body has a max height - and then scrolls - we should perhaps set up the height here
1437 // this needs fixing for various usage - currently only hydra job advers I think..
1439 // ctr.getHeight() - ((thd ? thd.getHeight() : 0) + (tfd ? tfd.getHeight() : 0))
1441 var barsize = (tbd.dom.offsetWidth - tbd.dom.clientWidth);
1444 cw = Math.max(cw, this.totalWidth);
1445 this.getGridEl().select('tbody tr',true).setWidth(cw);
1448 // resize 'expandable coloumn?
1450 return; // we doe not have a view in this design..
1453 onBodyScroll: function()
1455 //Roo.log("body scrolled');" + this.bodyEl.dom.scrollLeft);
1457 this.headEl.setStyle({
1458 'position' : 'relative',
1459 'left': (-1* this.bodyEl.dom.scrollLeft) + 'px'
1465 var scrollHeight = this.bodyEl.dom.scrollHeight;
1467 var scrollTop = Math.ceil(this.bodyEl.getScroll().top);
1469 var height = this.bodyEl.getHeight();
1471 if(scrollHeight - height == scrollTop) {
1473 var total = this.ds.getTotalCount();
1475 if(this.footer.cursor + this.footer.pageSize < total){
1477 this.footer.ds.load({
1479 start : this.footer.cursor + this.footer.pageSize,
1480 limit : this.footer.pageSize
1489 onColumnSplitterMoved : function(i, diff)
1491 this.userResized = true;
1493 var cm = this.colModel;
1495 var w = this.getHeaderIndex(i).getWidth() + diff;
1498 cm.setColumnWidth(i, w, true);
1500 //var cid = cm.getColumnId(i); << not used in this version?
1501 /* Roo.log(['#' + this.id + ' .x-col-' + i, "width", w + "px"]);
1503 this.CSS.updateRule( '#' + this.id + ' .x-col-' + i, "width", w + "px");
1504 this.CSS.updateRule('#' + this.id + ' .x-hcol-' + i, "width", w + "px");
1505 this.CSS.updateRule('#' + this.id + ' .x-grid-split-' + i, "left", w + "px");
1507 //this.updateSplitters();
1508 //this.layout(); << ??
1509 this.fireEvent("columnresize", i, w);
1511 onHeaderChange : function()
1513 var header = this.renderHeader();
1514 var table = this.el.select('table', true).first();
1516 this.headEl.remove();
1517 this.headEl = table.createChild(header, this.bodyEl, false);
1519 Roo.each(this.el.select('thead th.sortable', true).elements, function(e){
1520 e.on('click', this.sort, this);
1523 if(this.enableColumnResize !== false && Roo.grid.SplitDragZone){
1524 new Roo.grid.SplitDragZone(this, this.headEl.dom, false); // not sure what 'lockedHd is for this implementation..)
1529 onHiddenChange : function(colModel, colIndex, hidden)
1533 var thSelector = '#' + this.id + ' .x-hcol-' + colIndex;
1534 var tdSelector = '#' + this.id + ' .x-col-' + colIndex;
1536 this.CSS.updateRule(thSelector, "display", "");
1537 this.CSS.updateRule(tdSelector, "display", "");
1540 this.CSS.updateRule(thSelector, "display", "none");
1541 this.CSS.updateRule(tdSelector, "display", "none");
1544 // onload calls initCSS()
1545 this.onHeaderChange();
1549 setColumnWidth: function(col_index, width)
1551 // width = "md-2 xs-2..."
1552 if(!this.colModel.config[col_index]) {
1556 var w = width.split(" ");
1558 var rows = this.el.dom.getElementsByClassName("x-col-"+col_index);
1560 var h_row = this.el.dom.getElementsByClassName("x-hcol-"+col_index);
1563 for(var j = 0; j < w.length; j++) {
1569 var size_cls = w[j].split("-");
1571 if(!Number.isInteger(size_cls[1] * 1)) {
1575 if(!this.colModel.config[col_index][size_cls[0]]) {
1579 if(!h_row[0].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1583 h_row[0].classList.replace(
1584 "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1585 "col-"+size_cls[0]+"-"+size_cls[1]
1588 for(var i = 0; i < rows.length; i++) {
1590 var size_cls = w[j].split("-");
1592 if(!Number.isInteger(size_cls[1] * 1)) {
1596 if(!this.colModel.config[col_index][size_cls[0]]) {
1600 if(!rows[i].classList.contains("col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]])) {
1604 rows[i].classList.replace(
1605 "col-"+size_cls[0]+"-"+this.colModel.config[col_index][size_cls[0]],
1606 "col-"+size_cls[0]+"-"+size_cls[1]
1610 this.colModel.config[col_index][size_cls[0]] = size_cls[1];
1615 // currently only used to find the split on drag..
1616 Roo.bootstrap.Table.cellRE = /(?:.*?)x-grid-(?:hd|cell|split)-([\d]+)(?:.*?)/;
1621 Roo.bootstrap.Table.AbstractSelectionModel = Roo.grid.AbstractSelectionModel;
1622 Roo.bootstrap.Table.RowSelectionModel = Roo.grid.RowSelectionModel;