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

Row comparator #1074

Merged
merged 5 commits into from
Oct 27, 2017
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
9 changes: 7 additions & 2 deletions docs/api/column/inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion src/components/datatable.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 14 additions & 5 deletions src/utils/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) :
-cachedDir.compareFn(propA, propB);
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;
Expand Down