Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #442 - Tabulator - Disable a column header sorting option #443

Merged
merged 3 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/tables/extensions/tableextensions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Table } from "../table";

interface ITableExtension {
export interface ITableExtension {
location: string;
name: string;
visibleIndex: number;
Expand All @@ -18,7 +18,7 @@ export class TableExtensions {
if (!!extensions) {
extensions = this.sortExtensions(extensions);
extensions.forEach((extension) => {
if (!!extension.render) {
if (!!extension.render && this.table.allowExtension(extension)) {
var action = extension.render(this.table, options);
if (!!action) {
targetNode.appendChild(action);
Expand Down
13 changes: 12 additions & 1 deletion src/tables/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "./config";
import { Details } from "./extensions/detailsextensions";
import { localization } from "../localizationManager";
import { TableExtensions } from "./extensions/tableextensions";
import { ITableExtension, TableExtensions } from "./extensions/tableextensions";
import { createCommercialLicenseLink, createImagesContainer, createLinksContainer, DocumentHelper } from "../utils";
import { ColumnsBuilderFactory } from "./columnbuilder";
import { DefaultColumn } from "./columns";
Expand Down Expand Up @@ -391,6 +391,17 @@ export abstract class Table {
* Fires when permissions changed
*/
public onPermissionsChangedCallback: any;

protected get allowSorting(): boolean {
return this.options.allowSorting === undefined || this.options.allowSorting === true;
}

public allowExtension(extension: ITableExtension): boolean {
if(extension.location === "column" && extension.name === "sort") {
return this.allowSorting;
}
return true;
}
}

export abstract class TableRow {
Expand Down
2 changes: 1 addition & 1 deletion src/tables/tabulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export class Tabulator extends Table {
widthShrink: !column.width ? 1 : 0,
visible: this.isColumnVisible(column),
headerSort: false,
download: this._options.downloadHiddenColumns ? true : undefined,
download: this.options.downloadHiddenColumns ? true : undefined,
formatter,
accessorDownload: this.accessorDownload,
titleFormatter: (cell: any, formatterParams: any, onRendered: any) => {
Expand Down
38 changes: 35 additions & 3 deletions tests/tables/tabulator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { SurveyModel } from "survey-core";
import { Tabulator } from "../../src/tables/tabulator";
import { TableExtensions } from "../../src/tables/extensions/tableextensions";
import { Table } from "../../src/tables/table";

jest.mock("tabulator-tables", () => {
return { default: jest.requireActual("tabulator-tables") };
Expand Down Expand Up @@ -93,7 +95,7 @@ test("check that tabulator takes into account column's width", () => {
isPublic: true,
location: 0,
},
];
] as any;
var tabulatorColumns = tabulator.getColumns();
expect(tabulatorColumns[1].width).toBe(50);
expect(tabulatorColumns[1].widthShrink).toBe(0);
Expand All @@ -112,7 +114,7 @@ test("check that tabulator take into account column's width after layout (check
isPublic: true,
location: 0,
},
];
] as any;
tabulator.render(document.createElement("table"));
var columnDefinitions = tabulator.tabulatorTables.getColumnDefinitions();
expect(columnDefinitions[1].width).toBe(undefined);
Expand Down Expand Up @@ -142,7 +144,7 @@ test("check that tabulator take into account downloadHiddenColumns option", () =
isPublic: true,
location: 0,
},
];
] as any;

tabulator.render(document.createElement("table"));
expect(tabulator.tabulatorTables.getColumnDefinitions()[1].download).toBe(
Expand Down Expand Up @@ -273,4 +275,34 @@ test("image and file export formatter", () => {

const imageCell = accessorDownload(undefined, undefined, undefined, undefined, { getDefinition: () => ({ field: "signature" }) }, { getPosition: () => 0 });
expect(imageCell).toBe("signature");
});

test("Tabulator allowSorting option", () => {
TableExtensions.registerExtension({
location: "column",
name: "sort",
visibleIndex: 1,
render: function (table: Table, options: any) {
const ext = document.createElement("div");
ext.className = "sorting";
return ext;
},
} as any);
let tabulator = new Tabulator(new SurveyModel({ questions: [{ type: "text", name: "q1" }] }), [], undefined as any);
let tableExtensions = new TableExtensions(tabulator);

let tableNode = document.createElement("div");
tableExtensions.render(tableNode, "column");

expect(tableNode.innerHTML).toContain("sorting");

tabulator = new Tabulator(new SurveyModel({ questions: [{ type: "text", name: "q1" }] }), [], { allowSorting: false } as any);
tableExtensions = new TableExtensions(tabulator);

tableNode = document.createElement("div");
tableExtensions.render(tableNode, "column");

expect(tableNode.innerHTML.indexOf("sorting")).toBe(-1);

TableExtensions.unregisterExtension("column", "sort");
});
Loading