Skip to content

Commit c7b6f32

Browse files
committed
feat(table): allow disabling column sorting through header interactions
1 parent 0cae339 commit c7b6f32

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

etc/lime-elements.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ export namespace Components {
788788
"pageSize": number;
789789
"selectable": boolean;
790790
"selection": object[];
791+
"sortableColumns": boolean;
791792
"sorting": ColumnSorter[];
792793
"totalRows": number;
793794
}
@@ -2011,6 +2012,7 @@ export namespace JSX {
20112012
"pageSize"?: number;
20122013
"selectable"?: boolean;
20132014
"selection"?: object[];
2015+
"sortableColumns"?: boolean;
20142016
"sorting"?: ColumnSorter[];
20152017
"totalRows"?: number;
20162018
}

src/components/table/examples/table-sorting-disabled.tsx

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, h, State } from '@stencil/core';
1+
import { Component, h, Host, State } from '@stencil/core';
22
import { Column } from '@limetech/lime-elements';
33
import { invoices, Invoice } from './invoices';
44

@@ -9,8 +9,13 @@ import { invoices, Invoice } from './invoices';
99
* a column header. An arrow icon on the header visualizes the
1010
* direction of sorting, when a column is sorted.
1111
*
12-
* However, you can disable the sorting possibility in individual columns,
13-
* by setting the `headerSort` to `false`.
12+
* To prevent sorting altogether, set the `sortableColumns` property on
13+
* `limel-table` to `false`. If you only want to disable sorting for a
14+
* specific column, set the column's `headerSort` property to `false`.
15+
*
16+
* The "Reference Person" column below has sorting disabled on the
17+
* column definition, while the control lets you disable sorting for
18+
* the whole table.
1419
*
1520
* @sourceFile invoices.ts
1621
*/
@@ -24,18 +29,43 @@ export class TableExampleSortingDisabled {
2429
private tableData: Invoice[] = invoices;
2530

2631
@State()
27-
public columns: Column[] = [
32+
private disableAllSorting: boolean = false;
33+
34+
@State()
35+
private columns: Column[] = [
2836
{ title: 'Invoice no.', field: 'invoiceNumber' },
2937
{ title: 'Invoice Date', field: 'invoiceDate' },
3038
{
31-
title: 'Reference Person',
39+
title: 'Reference Person (no sorting)',
3240
field: 'referencePerson',
3341
headerSort: false,
3442
},
3543
{ title: 'Amount', field: 'amount', horizontalAlign: 'right' },
3644
];
3745

3846
render() {
39-
return <limel-table data={this.tableData} columns={this.columns} />;
47+
return (
48+
<Host>
49+
<limel-table
50+
data={this.tableData}
51+
columns={this.columns}
52+
sortableColumns={!this.disableAllSorting}
53+
/>
54+
<limel-example-controls
55+
style={{ '--example-controls-column-layout': 'auto-fit' }}
56+
>
57+
<limel-checkbox
58+
checked={this.disableAllSorting}
59+
label="Disable sorting on all columns"
60+
onChange={this.setDisableAllSorting}
61+
/>
62+
</limel-example-controls>
63+
</Host>
64+
);
4065
}
66+
67+
private setDisableAllSorting = (event: CustomEvent<boolean>) => {
68+
event.stopPropagation();
69+
this.disableAllSorting = event.detail;
70+
};
4171
}

src/components/table/table.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ export class Table {
112112
@Prop()
113113
public movableColumns: boolean;
114114

115+
/**
116+
* Set to `false` to disable column sorting through header interactions.
117+
* Programmatic sorting through the `sorting` prop and `sort` event remains available.
118+
*/
119+
@Prop()
120+
public sortableColumns: boolean = true;
121+
115122
/**
116123
* Set to `true` to trigger loading animation
117124
*/
@@ -362,6 +369,16 @@ export class Table {
362369
this.init();
363370
}
364371

372+
@Watch('sortableColumns')
373+
protected updateSortableColumns() {
374+
if (!this.tabulator) {
375+
return;
376+
}
377+
378+
this.tabulator.setColumns(this.getColumnDefinitions());
379+
this.shouldSort = true;
380+
}
381+
365382
@Watch('sorting')
366383
protected updateSorting(
367384
newValue: ColumnSorter[],
@@ -538,7 +555,13 @@ export class Table {
538555
private getColumnDefinitions(): Tabulator.ColumnDefinition[] {
539556
const columnDefinitions = this.columns
540557
.map(this.addColumnAggregator)
541-
.map(this.columnFactory.create);
558+
.map((column) => {
559+
const definition = this.columnFactory.create(column);
560+
const columnSortable = column.headerSort ?? true;
561+
definition.headerSort = this.sortableColumns && columnSortable;
562+
563+
return definition;
564+
});
542565

543566
if (this.tableSelection) {
544567
return this.tableSelection.getColumnDefinitions(columnDefinitions);

0 commit comments

Comments
 (0)