1 | # NgxMultiSortTable
|
2 |
|
3 | This is the implementation for a multiple sortable table based on the Angular Material Design. The focus is on server-side loaded and sorted data. Next to that, the library provides some useful classes to reduce the duplicated code when using the material `paginator`.
|
4 | The code is based on [Francisco Arantes Rodrigues](https://github.com/farantesrodrigues) repository [repo](https://github.com/farantesrodrigues/ng-mat-multi-sort), so thanks for your great work.
|
5 |
|
6 | ---
|
7 | **Warning:**
|
8 |
|
9 | - Older versions might have known security issues. So keep up-to-date!
|
10 | - Older versions of this library lack some features.
|
11 |
|
12 | ---
|
13 |
|
14 |
|
15 | ## Demo
|
16 | Visit the [GitHub pages demo](https://maxl94.github.io/ngx-multi-sort-table/) or clone and run it locally.
|
17 |
|
18 | To run the demo:
|
19 |
|
20 | 1. `clone` the repository
|
21 | 2. `npm install`
|
22 | 3. `ng build mat-multi-sort`
|
23 |
|
24 | ![demo gif](demo.gif)
|
25 |
|
26 | ## Changelog
|
27 |
|
28 | See the [GitHub release notes](https://github.com/Maxl94/ngx-multi-sort-table/releases).
|
29 |
|
30 | ## Documentation
|
31 |
|
32 | ### TableData
|
33 |
|
34 | The `TableData` a useful class, which handles a lot of work for your app, such as page events (`next`, `previous`, `sizeChange`) and sorting events. Next to that, it keeps the current state of the table, again sorting and pagination.
|
35 |
|
36 | #### Properties
|
37 |
|
38 | | Name | Description | default | Example |
|
39 | | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- | -------------------------------------------- |
|
40 | | columns | An array of the displayed columns of the table with `id`: name of the attribute and `name`: Name to display in the header | `none` | `[{ id: 'first_name', name: 'First Name' }]` |
|
41 | | displayedColumns | An array of the currently displayed columns (`id`) and their order | `all columns` | |
|
42 | | dataSource | A `MatMultiSortTableDataSource`, which is special `DataSource` for sorting. Only accesable via getter and setter | `none` | |
|
43 | | data | The table data of the dataSource | `Array<T>` |
|
44 | | pageSize | The current selected pageSize | first entry of `pageSizeOptions` | |
|
45 | | pageSizeOptions | The options for the pageSize, which the user can see in the menu | `[10, 20, 50, 100]` | |
|
46 | | pageIndex | The index of the page | `0` | |
|
47 | | totalElements | The total number of elements of the table, must be set from your component | `none` | |
|
48 | | sortParams | An Array of the columns (`id`), which the user had chosen to sort. The order of the sorting is represented by the order of the `id`s in the parameter | `[]` | `['first_name', 'last_name']` |
|
49 | | sortDirs | An Array of the column's sort-directions, which the user had chosen to sort. The order is the same like `sortParams` | `[]` | `['asc', 'desc']` |
|
50 | | nextObservable | An `Observable` that fires, when the user clicks the `next` button | | |
|
51 | | previousObservable | An `Observable` that fires, when the user clicks the `previous` button | | |
|
52 | | sizeObservable | An `Observable` that fires, when the user changes the `pageSize` | | |
|
53 | | sortObservable | An `Observable` that fires, when the user changes the sorted columns or direction | | |
|
54 |
|
55 | #### Methods
|
56 |
|
57 | | Name | Description | Parameter |
|
58 | | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
59 | | constructor | The constructor for the for the class, where you initialize your `columns`. Optionally, you can add the default `id`s of the default sort colum and direction. If `defaultSortParams` are provided, but not the directions `asc` will be default | `columns`: Array<{ id: string, name: string }>, `options`: { `defaultSortParams?`: string[], `defaultSortDirs?`: string[], `pageSizeOptions?`: number[], `totalElements?`: number } |
|
60 | | onSortEvent | The method to bind to the `matSortChange` output of the table | none |
|
61 | | onPaginationEvent | The method to bin to the `page` output of the `mat-paginator` | `$event`: PageEvent |
|
62 | | updateSortHeaders | The method triggers a rerendering of the headers to show the sorting directions correctly. The functions forces a complete new render of the data, what is not optimal, but only working solution right now. | none |
|
63 | | updateColumnNames | The method allows you to change the displayed name of the columns | { `id:` string, `name:` string }[] |
|
64 | | localStorageKey | A key to store the table settings, like selected columns, order of the columns, sorting directions in the local storage. If no key is passed the settings are not stored. **Do not set the same key for different tables, this might lead to unexpected behavior. There is an validation of the loaded settings, if that fails, the defaults are used** | string |
|
65 |
|
66 | ### MatMultiSortHeaderComponent
|
67 |
|
68 | This component manages the sorting of the table. To use the multi-sort add `matMultiSort` to your table and pass the `mat-multi-sort-header="<your-column-id>"` to the `<th mat-header-cell>`.
|
69 |
|
70 | ### MatMultiSortTableSettingsComponent
|
71 |
|
72 | This component display some settings for your table. The user can select the columns he wants to see in his table, next to that he can change the order of the columns. Additionally, the component shows the current chosen sorting columns as chips above the table.
|
73 | The user can easyly change the sorting order by drag and drop the chips and also change the sorting direction of each column.
|
74 |
|
75 | | Name | Description | Parameter |
|
76 | | ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- |
|
77 | | tableData | An input of `tableData` object which holds the complete table state | @Input: TableData |
|
78 | | sortToolTip | A input test for the tooltip to show up over the sorting chips | @Input: string |
|
79 | | closeDialogOnChoice | A input to control the behavior of the settings menu. If set to `true` the dialog closes after the user has selected a column, if `false` it stays open, so the user can select/deselect multiple columns with out reopening the dialog. | @Input: boolean |
|
80 | | scrollStrategy | An input of ScrollStrategy for the CDK overlay. Sets the behavior for scrolling when the dialog is opened. Possible options are the predefined strategies: Noop, Close, Block or Reposition, with Block being the default value. | @Input: ScrollStrategy |
|
81 |
|
82 | ### MatMultiSortTableDataSource
|
83 |
|
84 | This is the datasource of the MultiSortTable, it works like the `MatTableDataSource`´.
|
85 |
|
86 | | Name | Description | Parameter |
|
87 | | ----------- | ---------------------------- | ---------------------------------------------------------- |
|
88 | | constructor | The constructor of the class | `sort:` MatMultiSort, `clientSideSorting:` boolean = false |
|
89 |
|
90 | ## Example code for the template
|
91 |
|
92 | ```html
|
93 | <mat-multi-sort-table-settings [tableData]="table" sortToolTip="Sortierreihenfole ändern" [closeDialogOnChoice]="false">
|
94 | <button mat-stroked-button>
|
95 | Spalten bearbeiten
|
96 | <mat-icon>menu</mat-icon>
|
97 | </button>
|
98 | <!-- Optional custom content for the sort indicator chip (here column name with icons) -->
|
99 | <ng-template #sortIndicator let-direction='direction' let-columnName='columnName'>
|
100 | {{columnName}}
|
101 | <mat-icon *ngIf="direction">{{direction === 'asc' ? 'arrow_upward' : 'arrow_downward'}}</mat-icon>
|
102 | </ng-template>
|
103 | </mat-multi-sort-table-settings>
|
104 | <table mat-table [dataSource]="table.dataSource" matMultiSort (matSortChange)="table.onSortEvent()">
|
105 |
|
106 | <!-- Create all your columns with *ngfor, this is the lazy way out and only works if the display of the data does not differ -->
|
107 | <ng-container *ngFor="let column of table.columns" [matColumnDef]="column.id">
|
108 | <th mat-header-cell *matHeaderCellDef [mat-multi-sort-header]="column.id"> {{column.name}} </th>
|
109 | <td mat-cell *matCellDef="let row"> {{row[column.id]}} </td>
|
110 | </ng-container>
|
111 |
|
112 | <!-- Or define your in a normal, more individuell way -->
|
113 | <ng-container matColumnDef="id">
|
114 | <th mat-header-cell *matHeaderCellDef mat-multi-sort-header="id"> ID </th>
|
115 | <td mat-cell *matCellDef="let row"> {{row.id}} </td>
|
116 | </ng-container>
|
117 |
|
118 | <ng-container matColumnDef="progress">
|
119 | <th mat-header-cell *matHeaderCellDef mat-multi-sort-header="progress"> Progress </th>
|
120 | <td mat-cell *matCellDef="let row"> {{row.progress}} % </td>
|
121 | </ng-container>
|
122 |
|
123 | <ng-container matColumnDef="name">
|
124 | <th mat-header-cell *matHeaderCellDef mat-multi-sort-header="name"> Name </th>
|
125 | <td mat-cell *matCellDef="let row"> {{row.name}} </td>
|
126 | </ng-container>
|
127 |
|
128 | <tr mat-header-row *matHeaderRowDef="table.displayedColumns"></tr>
|
129 | <tr mat-row *matRowDef="let row; columns: table.displayedColumns;">
|
130 | </tr>
|
131 | </table>
|
132 | <mat-paginator [pageSize]="table.pageSize" [pageIndex]="table.pageIndex" [pageSizeOptions]="table.pageSizeOptions"
|
133 | [length]="table.totalElements ? table.totalElements : 0" (page)="table.onPaginationEvent($event)" [disabled]="CLIENT_SIDE">
|
134 | </mat-paginator>
|
135 | ```
|
136 |
|
137 | ## Example code for the component.ts
|
138 |
|
139 | ```typescript
|
140 | export class AppComponent implements OnInit {
|
141 | CLIENT_SIDE = true;
|
142 |
|
143 | table: TableData<UserData>;
|
144 | @ViewChild(MatMultiSort, { static: false }) sort: MatMultiSort;
|
145 |
|
146 | constructor(
|
147 | private dummyService: DummyService
|
148 | ) {
|
149 | this.table = new TableData<UserData>(
|
150 | [
|
151 | { id: 'id', name: 'ID' },
|
152 | { id: 'name', name: 'Name' },
|
153 | { id: 'progress', name: 'Progess' }
|
154 | ], { localStorageKey: 'settings' }
|
155 | );
|
156 | }
|
157 |
|
158 | ngOnInit() {
|
159 | this.table.nextObservable.subscribe(() => { this.getData(); });
|
160 | this.table.sortObservable.subscribe(() => { this.getData(); });
|
161 | this.table.previousObservable.subscribe(() => { this.getData(); });
|
162 | this.table.sizeObservable.subscribe(() => { this.getData(); });
|
163 |
|
164 | setTimeout(() => {
|
165 | this.initData();
|
166 | }, 0);
|
167 | }
|
168 |
|
169 | initData() {
|
170 | this.table.dataSource = new MatMultiSortTableDataSource(this.sort, this.CLIENT_SIDE);
|
171 | if (this.CLIENT_SIDE) {
|
172 | this.getOfflineData();
|
173 | } else {
|
174 | this.table.pageSize = 10;
|
175 | this.getData();
|
176 | }
|
177 | }
|
178 |
|
179 | getData() {
|
180 | if (!this.CLIENT_SIDE) {
|
181 | const res = this.dummyService.list(this.table.sortParams, this.table.sortDirs, this.table.pageIndex, this.table.pageSize);
|
182 | this.table.totalElements = res.totalElements;
|
183 | this.table.pageIndex = res.page;
|
184 | this.table.pageSize = res.pagesize;
|
185 | this.table.data = res.users;
|
186 | }
|
187 | }
|
188 |
|
189 | getOfflineData() {
|
190 | const res = this.dummyService.list([], [], 0, 25);
|
191 | this.table.totalElements = 25;
|
192 | this.table.pageIndex = res.page;
|
193 | this.table.pageSize = res.pagesize;
|
194 | this.table.data = res.users;
|
195 | }
|
196 | }
|
197 | ```
|
198 |
|
199 | ## Contributing
|
200 |
|
201 | ### How should I write my commits?
|
202 |
|
203 | Release Please assume you are using [Conventional Commit messages](https://www.conventionalcommits.org/).
|
204 |
|
205 | The most important prefixes you should have in mind are:
|
206 |
|
207 | - `fix:` which represents bug fixes, and correlates to a [SemVer](https://semver.org/)
|
208 | patch.
|
209 | - `feat:` which represents a new feature, and correlates to a SemVer minor.
|
210 | - `feat!:`, or `fix!:`, `refactor!:`, etc., which represent a breaking change
|
211 | (indicated by the `!`) and will result in a SemVer major.
|