1 var ColumnManager =
function(table){
3 this.blockHozScrollEvent =
false;
4 this.headersElement = this.createHeadersElement();
5 this.element = this.createHeaderElement();
6 this.rowManager = null;
8 this.columnsByIndex = [];
9 this.columnsByField = {};
12 this.element.insertBefore(this.headersElement, this.element.firstChild);
17 ColumnManager.prototype.createHeadersElement =
function (){
18 var el = document.createElement(
"div");
20 el.classList.add(
"tabulator-headers");
25 ColumnManager.prototype.createHeaderElement =
function (){
26 var el = document.createElement(
"div");
28 el.classList.add(
"tabulator-header");
30 if(!this.table.options.headerVisible){
31 el.classList.add(
"tabulator-header-hidden");
37 ColumnManager.prototype.initialize =
function (){
50 ColumnManager.prototype.setRowManager =
function(manager){
51 this.rowManager = manager;
55 ColumnManager.prototype.getElement =
function(){
60 ColumnManager.prototype.getHeadersElement =
function(){
61 return this.headersElement;
70 ColumnManager.prototype.scrollHorizontal =
function(left){
72 scrollWidth = this.element.scrollWidth - this.table.element.clientWidth;
75 this.element.scrollLeft = left;
78 if(left > scrollWidth){
79 hozAdjust = left - scrollWidth;
80 this.element.style.marginLeft = (-(hozAdjust)) +
"px";
82 this.element.style.marginLeft = 0;
88 this.scrollLeft = left;
90 if(this.table.modExists(
"frozenColumns")){
91 this.table.modules.frozenColumns.scrollHorizontal();
98 ColumnManager.prototype.generateColumnsFromRowData =
function(data){
102 if(data && data.length){
112 let value = row[key];
114 switch(typeof value){
124 if(Array.isArray(value)){
132 if(!isNaN(value) && value !==
""){
135 if(value.match(/((^[0-9]+[a-z]+)|(^[a-z]+[0-9]+))+$/i)){
149 this.table.options.columns = cols;
150 this.setColumns(this.table.options.columns);
154 ColumnManager.prototype.setColumns =
function(cols, row){
157 while(
self.headersElement.firstChild)
self.headersElement.removeChild(
self.headersElement.firstChild);
160 self.columnsByIndex = [];
161 self.columnsByField = {};
165 if(
self.table.modExists(
"frozenColumns")){
166 self.table.modules.frozenColumns.reset();
169 cols.forEach(
function(def, i){
170 self._addColumn(def);
173 self._reIndexColumns();
175 if(
self.table.options.responsiveLayout &&
self.table.modExists(
"responsiveLayout",
true)){
176 self.table.modules.responsiveLayout.initialize();
182 ColumnManager.prototype._addColumn =
function(definition, before, nextToColumn){
183 var column =
new Column(definition,
this),
184 colEl = column.getElement(),
185 index = nextToColumn ? this.findColumnIndex(nextToColumn) : nextToColumn;
187 if(nextToColumn && index > -1){
189 var parentIndex = this.columns.indexOf(nextToColumn.getTopColumn());
190 var nextEl = nextToColumn.getElement();
193 this.columns.splice(parentIndex, 0, column);
194 nextEl.parentNode.insertBefore(colEl, nextEl);
196 this.columns.splice(parentIndex + 1, 0, column);
197 nextEl.parentNode.insertBefore(colEl, nextEl.nextSibling);
202 this.columns.unshift(column);
203 this.headersElement.insertBefore(column.getElement(), this.headersElement.firstChild);
205 this.columns.push(column);
206 this.headersElement.appendChild(column.getElement());
209 column.columnRendered();
215 ColumnManager.prototype.registerColumnField =
function(col){
216 if(col.definition.field){
217 this.columnsByField[col.definition.field] = col;
221 ColumnManager.prototype.registerColumnPosition =
function(col){
222 this.columnsByIndex.push(col);
225 ColumnManager.prototype._reIndexColumns =
function(){
226 this.columnsByIndex = [];
228 this.columns.forEach(
function(column){
229 column.reRegisterPosition();
234 ColumnManager.prototype._verticalAlignHeaders =
function(){
235 var
self =
this, minHeight = 0;
237 self.columns.forEach(
function(column){
240 column.clearVerticalAlign();
242 height = column.getHeight();
244 if(height > minHeight){
249 self.columns.forEach(
function(column){
250 column.verticalAlign(
self.table.options.columnHeaderVertAlign, minHeight);
253 self.rowManager.adjustTableSize();
258 ColumnManager.prototype.findColumn =
function(subject){
261 if(typeof subject ==
"object"){
263 if(subject instanceof Column){
266 }
else if(subject instanceof ColumnComponent){
268 return subject._getSelf() ||
false;
269 }
else if(typeof HTMLElement !==
"undefined" && subject instanceof HTMLElement){
271 let match =
self.columns.find(
function(column){
272 return column.element === subject;
275 return match ||
false;
280 return this.columnsByField[subject] ||
false;
288 ColumnManager.prototype.getColumnByField =
function(field){
289 return this.columnsByField[field];
293 ColumnManager.prototype.getColumnsByFieldRoot =
function(root){
297 Object.keys(this.columnsByField).forEach((field) => {
298 var fieldRoot = field.split(
".")[0];
299 if(fieldRoot === root){
300 matches.push(this.columnsByField[field]);
307 ColumnManager.prototype.getColumnByIndex =
function(index){
308 return this.columnsByIndex[index];
311 ColumnManager.prototype.getFirstVisibileColumn =
function(index){
312 var index = this.columnsByIndex.findIndex(
function(col){
316 return index > -1 ? this.columnsByIndex[index] :
false;
319 ColumnManager.prototype.getColumns =
function(){
323 ColumnManager.prototype.findColumnIndex =
function(column){
324 return this.columnsByIndex.findIndex(
function(col){
325 return column === col;
330 ColumnManager.prototype.getRealColumns =
function(){
331 return this.columnsByIndex;
335 ColumnManager.prototype.traverse =
function(callback){
338 self.columnsByIndex.forEach(
function(column,i){
344 ColumnManager.prototype.getDefinitions =
function(active){
348 self.columnsByIndex.forEach(
function(column){
349 if(!active || (active && column.visible)){
350 output.push(column.getDefinition());
358 ColumnManager.prototype.getDefinitionTree =
function(){
362 self.columns.forEach(
function(column){
363 output.push(column.getDefinition(
true));
369 ColumnManager.prototype.getComponents =
function(structured){
372 columns = structured ?
self.columns :
self.columnsByIndex;
374 columns.forEach(
function(column){
375 output.push(column.getComponent());
381 ColumnManager.prototype.getWidth =
function(){
384 this.columnsByIndex.forEach(
function(column){
386 width += column.getWidth();
394 ColumnManager.prototype.moveColumn =
function(from, to, after){
395 this.moveColumnActual(from, to, after);
397 if(this.table.options.responsiveLayout &&
this.table.modExists(
"responsiveLayout",
true)){
398 this.table.modules.responsiveLayout.initialize();
401 if(this.table.modExists(
"columnCalcs")){
402 this.table.modules.columnCalcs.recalc(this.table.rowManager.activeRows);
405 to.element.parentNode.insertBefore(from.element, to.element);
408 to.element.parentNode.insertBefore(to.element, from.element);
411 this._verticalAlignHeaders();
413 this.table.rowManager.reinitialize();
416 ColumnManager.prototype.moveColumnActual =
function(from, to, after){
418 this._moveColumnInArray(this.columns, from, to, after);
419 this._moveColumnInArray(this.columnsByIndex, from, to, after,
true);
421 if(this.table.options.responsiveLayout &&
this.table.modExists(
"responsiveLayout",
true)){
422 this.table.modules.responsiveLayout.initialize();
425 if(this.table.options.columnMoved){
426 this.table.options.columnMoved.call(this.table, from.getComponent(), this.table.columnManager.getComponents());
429 if(this.table.options.persistence &&
this.table.modExists(
"persistence",
true) && this.table.modules.persistence.config.columns){
430 this.table.modules.persistence.save(
"columns");
434 ColumnManager.prototype._moveColumnInArray =
function(columns, from, to, after, updateRows){
435 var fromIndex = columns.indexOf(from),
438 if (fromIndex > -1) {
440 columns.splice(fromIndex, 1);
442 toIndex = columns.indexOf(to);
454 columns.splice(toIndex, 0, from);
458 this.table.rowManager.rows.forEach(
function(row){
459 if(row.cells.length){
460 var cell = row.cells.splice(fromIndex, 1)[0];
461 row.cells.splice(toIndex, 0, cell);
468 ColumnManager.prototype.scrollToColumn =
function(column, position, ifVisible){
472 colEl = column.getElement();
474 return new Promise((resolve, reject) => {
476 if(typeof position ===
"undefined"){
477 position = this.table.options.scrollToColumnPosition;
480 if(typeof ifVisible ===
"undefined"){
481 ifVisible = this.table.options.scrollToColumnIfVisible;
490 adjust = -
this.element.clientWidth / 2;
494 adjust = colEl.clientWidth -
this.headersElement.clientWidth;
501 offset = colEl.offsetLeft;
503 if(offset > 0 && offset + colEl.offsetWidth <
this.element.clientWidth){
509 left = colEl.offsetLeft + this.element.scrollLeft + adjust;
511 left = Math.max(Math.min(left,
this.table.rowManager.element.scrollWidth -
this.table.rowManager.element.clientWidth),0);
513 this.table.rowManager.scrollHorizontal(left);
514 this.scrollHorizontal(left);
518 console.warn(
"Scroll Error - Column not visible");
519 reject(
"Scroll Error - Column not visible");
527 ColumnManager.prototype.generateCells =
function(row){
532 self.columnsByIndex.forEach(
function(column){
533 cells.push(column.generateCell(row));
542 ColumnManager.prototype.getFlexBaseWidth =
function(){
544 totalWidth =
self.table.element.clientWidth,
548 if(
self.rowManager.element.scrollHeight >
self.rowManager.element.clientHeight){
549 totalWidth -=
self.rowManager.element.offsetWidth -
self.rowManager.element.clientWidth;
552 this.columnsByIndex.forEach(
function(column){
553 var width, minWidth, colWidth;
557 width = column.definition.width || 0;
559 minWidth = typeof column.minWidth ==
"undefined" ?
self.table.options.columnMinWidth : parseInt(column.minWidth);
561 if(typeof(width) ==
"string"){
562 if(width.indexOf(
"%") > -1){
563 colWidth = (totalWidth / 100) * parseInt(width) ;
565 colWidth = parseInt(width);
571 fixedWidth += colWidth > minWidth ? colWidth : minWidth;
579 ColumnManager.prototype.addColumn =
function(definition, before, nextToColumn){
580 return new Promise((resolve, reject) => {
581 var column = this._addColumn(definition, before, nextToColumn);
583 this._reIndexColumns();
585 if(this.table.options.responsiveLayout &&
this.table.modExists(
"responsiveLayout",
true)){
586 this.table.modules.responsiveLayout.initialize();
589 if(this.table.modExists(
"columnCalcs")){
590 this.table.modules.columnCalcs.recalc(this.table.rowManager.activeRows);
595 if(this.table.modules.layout.getMode() !=
"fitColumns"){
596 column.reinitializeWidth();
599 this._verticalAlignHeaders();
601 this.table.rowManager.reinitialize();
608 ColumnManager.prototype.deregisterColumn =
function(column){
609 var field = column.getField(),
614 delete this.columnsByField[field];
618 index = this.columnsByIndex.indexOf(column);
621 this.columnsByIndex.splice(index, 1);
625 index = this.columns.indexOf(column);
628 this.columns.splice(index, 1);
631 if(this.table.options.responsiveLayout &&
this.table.modExists(
"responsiveLayout",
true)){
632 this.table.modules.responsiveLayout.initialize();
639 ColumnManager.prototype.redraw =
function(force){
642 if(Tabulator.prototype.helpers.elVisible(
this.element)){
643 this._verticalAlignHeaders();
646 this.table.rowManager.resetScroll();
647 this.table.rowManager.reinitialize();
650 if([
"fitColumns",
"fitDataStretch"].indexOf(this.table.modules.layout.getMode()) > -1){
651 this.table.modules.layout.layout();
654 this.table.modules.layout.layout();
656 if(this.table.options.responsiveLayout &&
this.table.modExists(
"responsiveLayout",
true)){
657 this.table.modules.responsiveLayout.update();
662 if(this.table.modExists(
"frozenColumns")){
663 this.table.modules.frozenColumns.layout();
666 if(this.table.modExists(
"columnCalcs")){
667 this.table.modules.columnCalcs.recalc(this.table.rowManager.activeRows);
671 if(this.table.options.persistence &&
this.table.modExists(
"persistence",
true) && this.table.modules.persistence.config.columns){
672 this.table.modules.persistence.save(
"columns");
675 if(this.table.modExists(
"columnCalcs")){
676 this.table.modules.columnCalcs.redraw();
680 this.table.footerManager.redraw();