From 98565710f6846cd616652cb5b999984b944d0c75 Mon Sep 17 00:00:00 2001 From: Samuli-Petrus Korhonen Date: Wed, 25 Oct 2017 12:23:58 +0300 Subject: [PATCH 1/5] Type function parameters with explicit any Suppresses linter warning / build error --- src/components/datatable.component.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/datatable.component.ts b/src/components/datatable.component.ts index e7a4af4ba..56cf0d159 100644 --- a/src/components/datatable.component.ts +++ b/src/components/datatable.component.ts @@ -394,7 +394,7 @@ export class DatatableComponent implements OnInit, DoCheck, AfterViewInit { * return row.name !== 'Ethel Price'; * } */ - @Input() displayCheck: (row, column?, value?) => boolean; + @Input() displayCheck: (row: any, column?: any, value?: any) => boolean; /** * A boolean you can use to set the detault behaviour of rows and groups From 3e425f12e4131e529e88be8a817166af9a365c03 Mon Sep 17 00:00:00 2001 From: Samuli-Petrus Korhonen Date: Wed, 25 Oct 2017 12:25:04 +0300 Subject: [PATCH 2/5] Allow sorting based on whole row Somewhat strange parameter order preserves backward compatibility --- src/utils/sort.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/sort.ts b/src/utils/sort.ts index c47af28ce..7b0b9aa00 100644 --- a/src/utils/sort.ts +++ b/src/utils/sort.ts @@ -83,8 +83,8 @@ export function sortRows(rows: any[], columns: any[], dirs: SortPropDir[]): any[ const propB = valueGetter(b, prop); const comparison = cachedDir.dir !== SortDirection.desc ? - cachedDir.compareFn(propA, propB) : - -cachedDir.compareFn(propA, propB); + cachedDir.compareFn(propA, propB, a, b) : + -cachedDir.compareFn(propA, propB, a, b); // Don't return 0 yet in case of needing to sort by next property if (comparison !== 0) return comparison; From c533183a7f057ca7bbb7e8c21d20341a84067f74 Mon Sep 17 00:00:00 2001 From: Samuli-Petrus Korhonen Date: Wed, 25 Oct 2017 13:44:58 +0300 Subject: [PATCH 3/5] Also give sort direction to comparator as 5th parameter --- src/utils/sort.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/sort.ts b/src/utils/sort.ts index 7b0b9aa00..c901262f1 100644 --- a/src/utils/sort.ts +++ b/src/utils/sort.ts @@ -83,8 +83,8 @@ export function sortRows(rows: any[], columns: any[], dirs: SortPropDir[]): any[ const propB = valueGetter(b, prop); const comparison = cachedDir.dir !== SortDirection.desc ? - cachedDir.compareFn(propA, propB, a, b) : - -cachedDir.compareFn(propA, propB, a, b); + cachedDir.compareFn(propA, propB, a, b, true) : + -cachedDir.compareFn(propA, propB, a, b, false); // Don't return 0 yet in case of needing to sort by next property if (comparison !== 0) return comparison; From 8e2c8d2ca9cbf9b21317503083d06cbc6311fb70 Mon Sep 17 00:00:00 2001 From: Samuli-Petrus Korhonen Date: Wed, 25 Oct 2017 16:54:29 +0300 Subject: [PATCH 4/5] Added comments and used less cryptic variable names --- src/utils/sort.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/utils/sort.ts b/src/utils/sort.ts index c901262f1..ffce9a77d 100644 --- a/src/utils/sort.ts +++ b/src/utils/sort.ts @@ -75,16 +75,25 @@ export function sortRows(rows: any[], columns: any[], dirs: SortPropDir[]): any[ }; }); - return temp.sort(function(a: any, b: any) { + return temp.sort(function(rowA: any, rowB: any) { for(const cachedDir of cachedDirs) { + // Get property and valuegetters for column to be sorted const { prop, valueGetter } = cachedDir; - const propA = valueGetter(a, prop); - const propB = valueGetter(b, prop); + // Get A and B cell values from rows based on properties of the columns + const propA = valueGetter(rowA, prop); + const propB = valueGetter(rowB, prop); + // Compare function gets five parameters: + // Two cell values to be compared as propA and propB + // Two rows corresponding to the cells as rowA and rowB + // Direction of the sort for this column as SortDirection + // Compare can be a standard JS comparison function (a,b) => -1|0|1 + // as additional parameters are silently ignored. The whole row and sort + // direction enable more complex sort logic. const comparison = cachedDir.dir !== SortDirection.desc ? - cachedDir.compareFn(propA, propB, a, b, true) : - -cachedDir.compareFn(propA, propB, a, b, false); + cachedDir.compareFn(propA, propB, rowA, rowB, cachedDir.dir) : + -cachedDir.compareFn(propA, propB, rowA, rowB, cachedDir.dir); // Don't return 0 yet in case of needing to sort by next property if (comparison !== 0) return comparison; From 97761785de3d45c604e0724468ce62ff400695d2 Mon Sep 17 00:00:00 2001 From: Samuli-Petrus Korhonen Date: Wed, 25 Oct 2017 16:55:27 +0300 Subject: [PATCH 5/5] Added documentation for comparator --- docs/api/column/inputs.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/api/column/inputs.md b/docs/api/column/inputs.md index f2bf252c5..1a983c3c4 100644 --- a/docs/api/column/inputs.md +++ b/docs/api/column/inputs.md @@ -23,8 +23,13 @@ The width of the column by default in pixels. Default value: `150` The column can be resized manually by the user. Default value: `true` ### `comparator` -Custom sort comparator, used to apply custom sorting via client-side. See -[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) for more info. +Custom sort comparator, used to apply custom sorting via client-side. +Function receives five parameters, namely values and rows of items to be sorted as well as direction of the sort ('asc'|'desc'): +``` +(valueA, valueB, rowA, rowB, sortDirection) => -1|0|1 +``` +NOTE: Compare can be a standard JS comparison function (a,b) => -1|0|1 as additional parameters are silently ignored. +See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) for more info. ### `sortable`: `boolean` Sorting of the row values by this column. Default value: `true`