-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ngrid): colum specific sorting is not working
Fixes #45
- Loading branch information
1 parent
b60f41e
commit 39428ad
Showing
6 changed files
with
213 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
apps/libs/ngrid-examples/features/column/column-sort/column-specific-sorting.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<pbl-ngrid [dataSource]="ds" [columns]="columns"> | ||
<div *pblNgridHeaderCellDef="'*'; col as col;"> | ||
{{col.label}} | ||
<span *ngIf="ds.sort.column && ds.sort.column.id === col.id"> [{{ ds.sort.sort.order | uppercase }}]</span> | ||
</div> | ||
</pbl-ngrid> | ||
|
||
<div fxLayout="row" fxLayoutGap="16px" style="padding: 8px"> | ||
<button *ngFor="let key of ['name', 'settings.emailFrequency']" | ||
fxFlex="noshrink" mat-stroked-button color="primary" (click)="toggleActive(key)">{{ key }} [{{ getNextDirection(key) }}]</button> | ||
<div fxFlex="*"></div> | ||
<button fxFlex="noshrink" mat-stroked-button color="accent" (click)="clear()">Clear</button> | ||
</div> |
Empty file.
101 changes: 101 additions & 0 deletions
101
apps/libs/ngrid-examples/features/column/column-sort/column-specific-sorting.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { ChangeDetectionStrategy, Component, ViewEncapsulation } from '@angular/core'; | ||
import { createDS, columnFactory, PblNgridSorter, PblColumn, PblNgridSortInstructions, PblNgridSortOrder } from '@pebula/ngrid'; | ||
|
||
import { Person, DemoDataSource } from '@pebula/apps/shared-data'; | ||
import { Example } from '@pebula/apps/shared'; | ||
|
||
/** | ||
* Sorts by the length of the field | ||
*/ | ||
const lengthSorter: PblNgridSorter = (column: PblColumn, sort: PblNgridSortInstructions, data: any[]): any[] => { | ||
const tol = sort.order === 'desc' ? -1 : 1; | ||
return data.sort( (p1: any, p2: any) => { | ||
const v1 = column.getValue(p1) || ''; | ||
const v2 = column.getValue(p2) || ''; | ||
if (v1.length > v2.length) { | ||
return -1 * tol; | ||
} else if (v2.length > v1.length) { | ||
return 1 * tol; | ||
} | ||
return 0; | ||
}); | ||
} | ||
|
||
/** | ||
* Sorts by the email frequency of the field | ||
*/ | ||
const emailFrequencySorter: PblNgridSorter = (column: PblColumn, sort: PblNgridSortInstructions, data: any[]): any[] => { | ||
const FREQ_MAP = { | ||
Never: 0, | ||
Yearly: 2, | ||
Seldom: 3, | ||
Often: 4, | ||
Weekly: 5, | ||
Daily: 6 | ||
}; | ||
|
||
const tol = sort.order === 'desc' ? -1 : 1; | ||
return data.sort( (p1: any, p2: any) => { | ||
const v1 = FREQ_MAP[column.getValue(p1) || 'Never']; | ||
const v2 = FREQ_MAP[column.getValue(p2) || 'Never']; | ||
if (v1 > v2) { | ||
return -1 * tol; | ||
} else if (v2 > v1) { | ||
return 1 * tol; | ||
} | ||
return 0; | ||
}); | ||
} | ||
|
||
@Component({ | ||
selector: 'pbl-column-specific-sorting-example', | ||
templateUrl: './column-specific-sorting.component.html', | ||
styleUrls: ['./column-specific-sorting.component.scss'], | ||
encapsulation: ViewEncapsulation.None, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
@Example('pbl-column-specific-sorting-example', { title: 'Column Specific Sorting' }) | ||
export class ColumnSpecificSortingExample { | ||
columns = columnFactory() | ||
.default({minWidth: 100}) | ||
.table( | ||
{ prop: 'id', width: '40px' }, | ||
{ prop: 'name', sort: lengthSorter }, | ||
{ prop: 'gender',width: '50px' }, | ||
{ prop: 'settings.emailFrequency', sort: emailFrequencySorter } | ||
) | ||
.build(); | ||
ds = createDS<Person>().onTrigger( () => this.datasource.getPeople(100, 500) ).create(); | ||
|
||
constructor(private datasource: DemoDataSource) { } | ||
|
||
clear(): void { | ||
this.ds.setSort(); | ||
} | ||
|
||
toggleActive(columnId: string): void { | ||
const currentSort = this.ds.sort; | ||
let order: PblNgridSortOrder = 'asc'; | ||
if (currentSort && currentSort.column && currentSort.column.id === columnId) { | ||
order = currentSort.sort && currentSort.sort.order as any; | ||
if (order === 'asc') { | ||
order = 'desc'; | ||
} else if (order === 'desc') { | ||
this.clear(); | ||
return; | ||
} else { | ||
order = 'asc'; | ||
} | ||
} | ||
this.ds.hostGrid.setSort(columnId, { order }); | ||
} | ||
|
||
getNextDirection(key: string): string { | ||
const sort = this.ds.sort; | ||
if (!sort.column || sort.column.id !== key) { | ||
return 'asc'; | ||
} else { | ||
return sort.sort.order === 'asc' ? 'desc' : 'clear'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters