Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

Commit d9550de

Browse files
authored
Merge pull request #8 from briebugconsulting/develop
feat(sort): add custom sorting functionality
2 parents 8df50c3 + b8e4aa8 commit d9550de

File tree

5 files changed

+49
-19
lines changed

5 files changed

+49
-19
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ The templates use Font Awesome CSS class names, so the component requires Font A
1818
## Installing:
1919
`npm install angular-datatable --save`
2020

21+
## Usage:
22+
23+
### Custom Sorting
24+
25+
To make use of custom sort functionality, one must add a `[customSort]` binding to the data-table (if necessary for the default sort) or to the data-column (if necessary to sort that column). One must still specify the `[sortBy]` binding and specify the primary property of the bound data set that is being sorted, as this is fundamental to maintaining sort order (ASC or DESC).
26+
27+
The `[customSort]` binding must bind to a function that implements the function signature defined by `DataTableSortCallback`. This function is the same as any standard JS Array.sort() callback, and in fact is actually passed to array.sort() when sorting occurs.
28+
29+
If custom sort is not required, then only the `[sortBy]` binding is necessary.
2130

2231
#### Licensing
2332
MIT License

src/components/column.component.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Directive, Input, ContentChild, OnInit } from '@angular/core';
22
import { DataTableRow } from './row.component';
3-
import { CellCallback } from './types';
3+
import { CellCallback, DataTableSortCallback } from './types';
44

55

66
@Directive({
7-
selector: 'data-table-column'
7+
selector: 'data-table-column'
88
})
99
export class DataTableColumn implements OnInit {
1010

@@ -15,6 +15,7 @@ export class DataTableColumn implements OnInit {
1515
@Input() property: string;
1616
@Input() styleClass: string;
1717
@Input() cellColors: CellCallback;
18+
@Input() customSort?: DataTableSortCallback;
1819

1920
// init and state:
2021
@Input() width: number | string;
@@ -50,4 +51,4 @@ export class DataTableColumn implements OnInit {
5051
};
5152
}
5253
}
53-
}
54+
}

src/components/table.component.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from '@angular/core';
55
import { DataTableColumn } from './column.component';
66
import { DataTableRow } from './row.component';
7-
import { DataTableParams } from './types';
7+
import {DataTableParams, DataTableSortCallback} from './types';
88
import { RowCallback } from './types';
99
import { DataTableTranslations, defaultTranslations } from './types';
1010
import { drag } from '../utils/drag';
@@ -14,9 +14,9 @@ import { TABLE_STYLE } from "./table.style";
1414

1515

1616
@Component({
17-
selector: 'data-table',
18-
template: TABLE_TEMPLATE,
19-
styles: [TABLE_STYLE]
17+
selector: 'data-table',
18+
template: TABLE_TEMPLATE,
19+
styles: [TABLE_STYLE]
2020
})
2121
export class DataTable implements DataTableParams, OnInit {
2222

@@ -68,6 +68,7 @@ export class DataTable implements DataTableParams, OnInit {
6868

6969
private _sortBy: string;
7070
private _sortAsc = true;
71+
private _customSort: DataTableSortCallback = null;
7172

7273
private _offset = 0;
7374
private _limit = 10;
@@ -92,6 +93,16 @@ export class DataTable implements DataTableParams, OnInit {
9293
this._triggerReload();
9394
}
9495

96+
@Input()
97+
get customSort() {
98+
return this._customSort;
99+
}
100+
101+
set customSort(value) {
102+
this._customSort = value;
103+
this._triggerReload();
104+
}
105+
95106
@Input()
96107
get offset() {
97108
return this._offset;
@@ -129,9 +140,10 @@ export class DataTable implements DataTableParams, OnInit {
129140

130141
// setting multiple observable properties simultaneously
131142

132-
sort(sortBy: string, asc: boolean) {
143+
sort(sortBy: string, asc: boolean, customSort: DataTableSortCallback = null) {
133144
this.sortBy = sortBy;
134145
this.sortAsc = asc;
146+
this.customSort = customSort;
135147
}
136148

137149
// init
@@ -190,6 +202,7 @@ export class DataTable implements DataTableParams, OnInit {
190202
_updateDisplayParams() {
191203
this._displayParams = {
192204
sortBy: this.sortBy,
205+
customSort: this.customSort,
193206
sortAsc: this.sortAsc,
194207
offset: this.offset,
195208
limit: this.limit
@@ -250,6 +263,7 @@ export class DataTable implements DataTableParams, OnInit {
250263

251264
if (this.sortBy) {
252265
params.sortBy = this.sortBy;
266+
params.customSort = this.customSort;
253267
params.sortAsc = this.sortAsc;
254268
}
255269
if (this.pagination) {
@@ -262,7 +276,7 @@ export class DataTable implements DataTableParams, OnInit {
262276
private sortColumn(column: DataTableColumn) {
263277
if (column.sortable) {
264278
let ascending = this.sortBy === column.property ? !this.sortAsc : true;
265-
this.sort(column.property, ascending);
279+
this.sort(column.property, ascending, column.customSort);
266280
}
267281
}
268282

@@ -371,4 +385,4 @@ export class DataTable implements DataTableParams, OnInit {
371385
}
372386
return true;
373387
}
374-
}
388+
}

src/components/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type CellCallback = (item: any, row: DataTableRow, column: DataTableColum
88

99
// export type HeaderCallback = (column: DataTableColumn) => string;
1010

11+
export type DataTableSortCallback = (a: any, b: any) => number;
1112

1213
export interface DataTableTranslations {
1314
indexColumn: string;
@@ -30,5 +31,6 @@ export interface DataTableParams {
3031
offset?: number;
3132
limit?: number;
3233
sortBy?: string;
34+
customSort?: DataTableSortCallback;
3335
sortAsc?: boolean;
34-
}
36+
}

src/tools/data-table-resource.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ export class DataTableResource<T> {
1515
}
1616

1717
if (params.sortBy) {
18-
result.sort((a, b) => {
19-
if (typeof a[params.sortBy] === 'string') {
20-
return a[params.sortBy].localeCompare(b[params.sortBy]);
21-
} else {
22-
return a[params.sortBy] - b[params.sortBy];
23-
}
24-
});
18+
if (!params.customSort) {
19+
result.sort((a, b) => {
20+
if (typeof a[params.sortBy] === 'string') {
21+
return a[params.sortBy].localeCompare(b[params.sortBy]);
22+
} else {
23+
return a[params.sortBy] - b[params.sortBy];
24+
}
25+
});
26+
} else {
27+
result.sort(params.customSort);
28+
}
2529
if (params.sortAsc === false) {
2630
result.reverse();
2731
}
@@ -45,4 +49,4 @@ export class DataTableResource<T> {
4549
});
4650

4751
}
48-
}
52+
}

0 commit comments

Comments
 (0)