|
| 1 | +// Custom fitDataStretch variant: stretch the column marked with widthGrow instead of the last column. |
| 2 | +// See https://github.com/olifolkerd/tabulator/issues/2725 and Tabulator's fitDataStretch layout. |
| 3 | +export function registerFitDataStretchGrowLayout(Tabulator, tabulatorOptions) { |
| 4 | + if(tabulatorOptions.layout !== "fitDataStretchGrow") { |
| 5 | + return |
| 6 | + } |
| 7 | + |
| 8 | + Tabulator.extendModule("layout", "modes", { |
| 9 | + fitDataStretchGrow: fitDataStretchGrow |
| 10 | + }) |
| 11 | + |
| 12 | + if(Tabulator.moduleBindings.layout?.modes) { |
| 13 | + Tabulator.moduleBindings.layout.modes.fitDataStretchGrow = fitDataStretchGrow |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +function fitDataStretchGrow(columns) { |
| 19 | + let colsWidth = 0, |
| 20 | + tableWidth = this.table.rowManager.element.clientWidth, |
| 21 | + gap = 0, |
| 22 | + lastCol = false, |
| 23 | + stretchCol = false |
| 24 | + |
| 25 | + columns.forEach((column) => { |
| 26 | + if(column.modules.fitDataStretchGrowWidth) { |
| 27 | + column.widthFixed = false |
| 28 | + column.modules.fitDataStretchGrowWidth = false |
| 29 | + } |
| 30 | + |
| 31 | + if(column.widthFixed && typeof column.definition.width === "undefined") { |
| 32 | + column.widthFixed = false |
| 33 | + } |
| 34 | + |
| 35 | + if(!column.widthFixed) { |
| 36 | + column.reinitializeWidth() |
| 37 | + } |
| 38 | + |
| 39 | + if(this.table.options.responsiveLayout ? column.modules.responsive.visible : column.visible) { |
| 40 | + lastCol = column |
| 41 | + if(column.definition.widthGrow) { |
| 42 | + stretchCol = column |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if(column.visible) { |
| 47 | + colsWidth += column.getWidth() |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + const targetCol = stretchCol || lastCol |
| 52 | + |
| 53 | + if(targetCol) { |
| 54 | + gap = tableWidth - colsWidth + targetCol.getWidth() |
| 55 | + |
| 56 | + if(this.table.options.responsiveLayout && this.table.modExists("responsiveLayout", true)) { |
| 57 | + targetCol.setWidth(0) |
| 58 | + this.table.modules.responsiveLayout.update() |
| 59 | + } |
| 60 | + |
| 61 | + if(gap > 0) { |
| 62 | + targetCol.setWidth(gap) |
| 63 | + targetCol.modules.fitDataStretchGrowWidth = true |
| 64 | + } else { |
| 65 | + targetCol.reinitializeWidth() |
| 66 | + } |
| 67 | + } else if(this.table.options.responsiveLayout && this.table.modExists("responsiveLayout", true)) { |
| 68 | + this.table.modules.responsiveLayout.update() |
| 69 | + } |
| 70 | +} |
0 commit comments