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

Add flag to Column to sort nulls to the bottom #3506

Merged
merged 6 commits into from
Oct 25, 2023
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 60.0.0-SNAPSHOT - unreleased

### 🎁 New Features

* Added `Column.sortToBottom` which supports always sorting specified values to the bottom,
regardless of sort direction.

## 59.2.0 - 2023-10-16

### 🐞 Bug Fixes
Expand Down
47 changes: 45 additions & 2 deletions cmp/grid/columns/Column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ export interface ColumnSpec {
*/
sortValue?: string | ColumnSortValueFn;

/**
* Values to match or functions to check to determine if a value should always be sorted
* to the bottom, regardless of sort order. If more than one entry is provided, values will be
* sorted according to the order they appear here.
*/
sortToBottom?: Some<unknown | ((v: unknown) => boolean)>;

/** Function to compare cell values for sorting.*/
comparator?: ColumnComparator;

Expand Down Expand Up @@ -434,6 +441,7 @@ export class Column {
sortingOrder: ColumnSortSpec[];
absSort: boolean;
sortValue: string | ColumnSortValueFn;
sortToBottom: Array<(v: unknown) => boolean>;
comparator: ColumnComparator;
resizable: boolean;
sortable: boolean;
Expand Down Expand Up @@ -506,6 +514,7 @@ export class Column {
absSort,
sortingOrder,
sortValue,
sortToBottom,
comparator,
resizable,
movable,
Expand Down Expand Up @@ -600,6 +609,7 @@ export class Column {
this.absSort = withDefault(absSort, false);
this.sortingOrder = this.parseSortingOrder(sortingOrder);
this.sortValue = sortValue;
this.sortToBottom = this.parseSortToBottom(sortToBottom);
this.comparator = comparator;

this.resizable = withDefault(resizable, true);
Expand Down Expand Up @@ -897,12 +907,20 @@ export class Column {
if (this.comparator === undefined) {
// Use default comparator with appropriate inputs
ret.comparator = (valueA, valueB, agNodeA, agNodeB) => {
const recordA = agNodeA?.data,
const {gridModel, colId} = this,
// Note: sortCfg and agNodes can be undefined if comparator called during show
// of agGrid column header set filter menu.
sortCfg = find(gridModel.sortBy, {colId}),
sortDir = sortCfg?.sort || 'asc',
recordA = agNodeA?.data,
recordB = agNodeB?.data;

valueA = this.getSortValue(valueA, recordA);
valueB = this.getSortValue(valueB, recordB);

const sortToBottom = this.sortToBottomComparator(valueA, valueB, sortDir);
if (sortToBottom !== 0) return sortToBottom;

return this.defaultComparator(valueA, valueB);
};
} else {
Expand All @@ -921,14 +939,17 @@ export class Column {
recordB,
column: this,
gridModel,
defaultComparator: (a, b) => this.defaultComparator(a, b),
defaultComparator: this.defaultComparator,
lbwexler marked this conversation as resolved.
Show resolved Hide resolved
agNodeA,
agNodeB
};

valueA = this.getSortValue(valueA, recordA);
valueB = this.getSortValue(valueB, recordB);

const sortToBottom = this.sortToBottomComparator(valueA, valueB, sortDir);
if (sortToBottom !== 0) return sortToBottom;

return this.comparator(valueA, valueB, sortDir, abs, params);
};
}
Expand Down Expand Up @@ -979,6 +1000,23 @@ export class Column {
return sortCfg ? sortCfg.comparator(v1, v2) : GridSorter.defaultComparator(v1, v2);
};

private sortToBottomComparator = (v1, v2, sortDir: 'asc' | 'desc') => {
const {sortToBottom} = this;

if (isNil(sortToBottom)) return 0;

for (let fn of sortToBottom) {
const v1ToBottom = fn(v1),
v2ToBottom = fn(v2);
const isAsc = sortDir === 'asc';
if (v1ToBottom != v2ToBottom) {
return v1ToBottom ? (isAsc ? 1 : -1) : isAsc ? -1 : 1;
}
lbwexler marked this conversation as resolved.
Show resolved Hide resolved
}

return 0;
};

private defaultSetValueFn = ({value, record, store, field}) => {
const data = {id: record.id};
data[field] = value;
Expand Down Expand Up @@ -1011,6 +1049,11 @@ export class Column {
return sortingOrder?.map(spec => (isString(spec) || spec === null ? {sort: spec} : spec));
}

private parseSortToBottom(sortToBottom): Array<(v: unknown) => boolean> {
if (isNil(sortToBottom)) return null;
return castArray(sortToBottom).map(v => (isFunction(v) ? v : it => it === v));
lbwexler marked this conversation as resolved.
Show resolved Hide resolved
}

private parseFilterable(filterable) {
if (!filterable) return false;

Expand Down
Loading