Skip to content

Column Definition

The column definition holds the configuration for a table column, including its field name, data type, formatters, and other options.

Below you can check the available attributes of the ColumnDefinition class and their descriptions. Also, a full code is provided at the end of this document for reference.

Column definition.

ATTRIBUTE DESCRIPTION
field

The field name in the data dictionary.

TYPE: str

title

The display title for the column. Defaults to a formatted version of field.

TYPE: str | None

formatters

List of custom server-side formatters to apply to the column's value.

TYPE: list[tuple[type[BaseFormatter], dict[str, Any]]]

tabulator_formatter

The name of a built-in Tabulator.js formatter (e.g., "plaintext").

TYPE: str | None

tabulator_formatter_params

Parameters for the built-in tabulator formatter.

TYPE: dict[str, Any]

width

The width of the column in pixels.

TYPE: int | None

min_width

The minimum width of the column in pixels.

TYPE: int | None

visible

Whether the column is visible.

TYPE: bool

sorter

The default sorter for the column (e.g., "string", "number").

TYPE: bool

filterable

Whether the column can be filtered by the user.

TYPE: bool

resizable

Whether the column is resizable by the user.

TYPE: bool

tooltip

Whether to show a tooltip with the full content on hover.

TYPE: bool

vertical_align

Vertical alignment of the column content. Defaults to "middle".

TYPE: str

horizontal_align

Horizontal alignment of the column content. Defaults to "".

TYPE: str

Source code in ckanext/tables/table.py
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
@dataclass(frozen=True)
class ColumnDefinition:
    """Column definition.

    Attributes:
        field: The field name in the data dictionary.
        title: The display title for the column. Defaults to a formatted version of `field`.
        formatters: List of custom server-side formatters to apply to the column's value.
        tabulator_formatter: The name of a built-in Tabulator.js formatter (e.g., "plaintext").
        tabulator_formatter_params: Parameters for the built-in tabulator formatter.
        width: The width of the column in pixels.
        min_width: The minimum width of the column in pixels.
        visible: Whether the column is visible.
        sorter: The default sorter for the column (e.g., "string", "number").
        filterable: Whether the column can be filtered by the user.
        resizable: Whether the column is resizable by the user.
        tooltip: Whether to show a tooltip with the full content on hover.
        vertical_align: Vertical alignment of the column content. Defaults to "middle".
        horizontal_align: Horizontal alignment of the column content. Defaults to "".
    """

    field: str
    title: str | None = None
    formatters: list[tuple[type[formatters.BaseFormatter], dict[str, Any]]] = dataclass_field(default_factory=list)
    tabulator_formatter: str | None = None
    tabulator_formatter_params: dict[str, Any] = dataclass_field(default_factory=dict)
    width: int | None = None
    min_width: int | None = None
    visible: bool = True
    sortable: bool = True
    filterable: bool = True
    resizable: bool = True
    tooltip: bool = False
    vertical_align: str = "middle"
    horizontal_align: str = ""

    def __post_init__(self):
        if self.title is None:
            object.__setattr__(self, "title", self.field.replace("_", " ").title())

    def to_dict(self) -> dict[str, Any]:
        """Convert the column definition to a dict for JSON serialization."""
        result = {
            "field": self.field,
            "title": self.title,
            "visible": self.visible,
            "resizable": self.resizable,
            "tooltip": self.tooltip,
            "vertAlign": self.vertical_align,
            "hozAlign": self.horizontal_align,
        }

        mappings = {
            "width": "width",
            "min_width": "minWidth",
            "tabulator_formatter": "formatter",
            "tabulator_formatter_params": "formatterParams",
        }

        for name, tabulator_name in mappings.items():
            if value := getattr(self, name):
                result[tabulator_name] = value

        if self.sortable:
            result["sorter"] = "string"
        else:
            result["headerSort"] = False

        if self.filterable:
            result["headerFilter"] = True
            result["headerFilterPlaceholder"] = tk._("Search")

        return result

to_dict()

Convert the column definition to a dict for JSON serialization.

Source code in ckanext/tables/table.py
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
def to_dict(self) -> dict[str, Any]:
    """Convert the column definition to a dict for JSON serialization."""
    result = {
        "field": self.field,
        "title": self.title,
        "visible": self.visible,
        "resizable": self.resizable,
        "tooltip": self.tooltip,
        "vertAlign": self.vertical_align,
        "hozAlign": self.horizontal_align,
    }

    mappings = {
        "width": "width",
        "min_width": "minWidth",
        "tabulator_formatter": "formatter",
        "tabulator_formatter_params": "formatterParams",
    }

    for name, tabulator_name in mappings.items():
        if value := getattr(self, name):
            result[tabulator_name] = value

    if self.sortable:
        result["sorter"] = "string"
    else:
        result["headerSort"] = False

    if self.filterable:
        result["headerFilter"] = True
        result["headerFilterPlaceholder"] = tk._("Search")

    return result