Skip to content

Commit 48e3610

Browse files
committed
feat: added fitDataStretchGrow layout and widthGrow support for Tabulator
1 parent b0d8422 commit 48e3610

4 files changed

Lines changed: 79 additions & 1 deletion

File tree

src/django_smartbase_admin/engine/field.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class TabulatorFieldOptions(JSONSerializableMixin):
3737
frozen = False
3838
title = None
3939
editorParams = None
40+
width = None
41+
widthGrow = None
4042

4143
def __init__(
4244
self,
@@ -46,6 +48,8 @@ def __init__(
4648
frozen=None,
4749
title=None,
4850
editorParams=None,
51+
width=None,
52+
widthGrow=None,
4953
) -> None:
5054
super().__init__()
5155
self.headerFilter = headerFilter
@@ -54,6 +58,8 @@ def __init__(
5458
self.frozen = frozen
5559
self.title = title
5660
self.editorParams = editorParams
61+
self.width = width
62+
self.widthGrow = widthGrow
5763

5864

5965
class XLSXFieldOptions(JSONSerializableMixin):

src/django_smartbase_admin/static/sb_admin/src/css/_tabulator.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
.tabulator.tabulator--sticky-header-and-footer .tabulator-header {
3232
position: sticky;
3333
top: 0;
34-
z-index: 10;
34+
z-index: 100;
3535
}
3636

3737
.tabulator .tabulator-header.tabulator-header-hidden {

src/django_smartbase_admin/static/sb_admin/src/js/table.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { HeaderTabsModule } from "./table_modules/header_tabs_module"
1313
import { DataTreeModule } from "./table_modules/data_tree_module"
1414
import { StickyHeaderAndFooterModule } from "./table_modules/sticky_header_and_footer_module"
1515
import { SBAjaxParamsTabulatorModifier } from "./sb_ajax_params_tabulator_modifier"
16+
import { registerFitDataStretchGrowLayout } from "./tabulator_layouts/fit_data_stretch_grow"
1617

1718

1819
class SBAdminTable {
@@ -252,6 +253,7 @@ class SBAdminTable {
252253
return "<a href='" + self.tableDetailUrl.replace(self.constants.OBJECT_ID_PLACEHOLDER, dataId) + "'>" + cellContent + "</a>"
253254
}
254255
})
256+
registerFitDataStretchGrowLayout(Tabulator, this.tabulatorOptions)
255257

256258
this.defaultRowSelectionFormatter = Tabulator.moduleBindings.format.formatters.rowSelection
257259
const self = this
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)