2025-07-04 21:31:43 +03:00
|
|
|
import type { ColumnComponent, ColumnDefinition, MenuObject, Tabulator } from "tabulator-tables";
|
2025-07-01 12:09:13 +03:00
|
|
|
|
2025-07-04 21:31:43 +03:00
|
|
|
export function applyHeaderMenu(columns: ColumnDefinition[]) {
|
2025-07-01 12:09:13 +03:00
|
|
|
for (let column of columns) {
|
2025-07-04 19:29:33 +03:00
|
|
|
if (column.headerSort !== false) {
|
|
|
|
|
column.headerMenu = headerMenu;
|
|
|
|
|
}
|
2025-06-29 22:26:25 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-01 12:09:13 +03:00
|
|
|
function headerMenu(this: Tabulator) {
|
2025-07-04 21:31:43 +03:00
|
|
|
const menu: MenuObject<ColumnComponent>[] = [];
|
2025-07-01 12:09:13 +03:00
|
|
|
const columns = this.getColumns();
|
2025-06-29 22:26:25 +03:00
|
|
|
|
2025-07-01 12:09:13 +03:00
|
|
|
for (let column of columns) {
|
2025-06-29 22:26:25 +03:00
|
|
|
//create checkbox element using font awesome icons
|
|
|
|
|
let icon = document.createElement("i");
|
|
|
|
|
icon.classList.add("bx");
|
|
|
|
|
icon.classList.add(column.isVisible() ? "bx-check" : "bx-empty");
|
|
|
|
|
|
|
|
|
|
//build label
|
|
|
|
|
let label = document.createElement("span");
|
|
|
|
|
let title = document.createElement("span");
|
|
|
|
|
|
|
|
|
|
title.textContent = " " + column.getDefinition().title;
|
|
|
|
|
|
|
|
|
|
label.appendChild(icon);
|
|
|
|
|
label.appendChild(title);
|
|
|
|
|
|
|
|
|
|
//create menu item
|
|
|
|
|
menu.push({
|
2025-07-01 12:09:13 +03:00
|
|
|
label: label,
|
|
|
|
|
action: function (e) {
|
2025-06-29 22:26:25 +03:00
|
|
|
//prevent menu closing
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
|
|
//toggle current column visibility
|
|
|
|
|
column.toggle();
|
|
|
|
|
|
|
|
|
|
//change menu item icon
|
2025-07-01 12:09:13 +03:00
|
|
|
if (column.isVisible()) {
|
2025-06-29 22:26:25 +03:00
|
|
|
icon.classList.remove("bx-empty");
|
|
|
|
|
icon.classList.add("bx-check");
|
2025-07-01 12:09:13 +03:00
|
|
|
} else {
|
2025-06-29 22:26:25 +03:00
|
|
|
icon.classList.remove("bx-check");
|
|
|
|
|
icon.classList.add("bx-empty");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-01 12:09:13 +03:00
|
|
|
return menu;
|
2025-06-29 22:26:25 +03:00
|
|
|
};
|