Skip to content

Commit

Permalink
feat(entity view): add nullsFirst option (#494)
Browse files Browse the repository at this point in the history
* fix(entity view): null values are properly sorted

* fix(entity): undefined, null and empty string are properly sorted

* fix(view): naturalCompare is fixed, no need to separate undefined and null values first
  • Loading branch information
cbourget authored and mbarbeau committed Oct 31, 2019
1 parent a1fc5de commit a51681a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ export class EntityTableComponent implements OnInit, OnDestroy, OnChanges {
@Input()
scrollBehavior: EntityTableScrollBehavior = EntityTableScrollBehavior.Auto;

/**
* Whether nulls should be first when sorting
*/
@Input()
sortNullsFirst: boolean = false;

/**
* Event emitted when an entity (row) is clicked
*/
Expand Down Expand Up @@ -196,7 +202,8 @@ export class EntityTableComponent implements OnInit, OnDestroy, OnChanges {
if (direction === 'asc' || direction === 'desc') {
this.store.view.sort({
valueAccessor: (entity: object) => this.getValue(entity, column),
direction
direction,
nullsFirst: this.sortNullsFirst
});
} else {
this.store.view.sort(undefined);
Expand Down
6 changes: 6 additions & 0 deletions packages/common/src/lib/entity/shared/entity.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export type EntityFilterClause<E = object> = (entity: E) => boolean;
export interface EntitySortClause<E = object> {
valueAccessor: (entity: E) => string | number;
direction: string;

// If true, null and undefined values will be first
// If false, null and undefined values will be last
// If undefined, default to true and false when sorting in descending and
// ascending order, respectively
nullsFirst?: boolean;
}

export interface EntityJoinClause {
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/lib/entity/shared/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export class EntityView<E extends object, V extends object = E> {
clause.valueAccessor(v1),
clause.valueAccessor(v2),
clause.direction,
false
clause.nullsFirst
);
});
}
Expand Down

0 comments on commit a51681a

Please sign in to comment.