Skip to content

Commit

Permalink
Merge pull request #563 from revolist/cellParser-support
Browse files Browse the repository at this point in the history
feat: Cell parser support
  • Loading branch information
revolist authored Sep 12, 2024
2 parents 6772bea + 7eabc87 commit 9117a91
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/components/data/column.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Observable,
CELL_CLASS,
DISABLED_CLASS,
getCellRaw,
} from '../../utils';
import { getRange } from '@store';

Expand Down Expand Up @@ -155,7 +156,6 @@ export default class ColumnService {
const column = this.columns[colIndex];
const prop: ColumnProp | undefined = column?.prop;
const model = getSourceItem(this.dataStore, rowIndex) || {};
const value = model[prop];
const type = this.dataStore.get('type');
return {
prop,
Expand All @@ -166,7 +166,7 @@ export default class ColumnService {
colIndex,
colType: this.type,
type,
value,
value: getCellRaw(model, column),
};
}

Expand Down
12 changes: 7 additions & 5 deletions src/plugins/sorting/sorting.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
DimensionRows,
PluginProviders,
} from '@type';
import { getColumnByProp } from '../../utils/column.utils';
import { getCellRaw, getColumnByProp } from '../../utils/column.utils';
import { rowTypes } from '@store';

export type SortingOrder = Record<ColumnProp, Order>;
Expand Down Expand Up @@ -123,7 +123,7 @@ export default class SortingPlugin extends BasePlugin {

getComparer(column: ColumnRegular | undefined, order: Order): CellCompareFunc | undefined {
const cellCmp: CellCompareFunc =
column?.cellCompare?.bind({ order }) || this.defaultCellCompare;
column?.cellCompare?.bind({ order }) || this.defaultCellCompare?.bind({ column });
if (order == 'asc') {
return cellCmp;
}
Expand Down Expand Up @@ -258,9 +258,11 @@ export default class SortingPlugin extends BasePlugin {
this.emit('aftersortingapply');
}

defaultCellCompare(prop: ColumnProp, a: DataType, b: DataType) {
const av = a?.[prop]?.toString().toLowerCase();
const bv = b?.[prop]?.toString().toLowerCase();
defaultCellCompare(this: { column?: ColumnRegular }, prop: ColumnProp, a: DataType, b: DataType) {
const aRaw = this.column ? getCellRaw(a, this.column) : a?.[prop];
const bRaw = this.column ? getCellRaw(b, this.column) : b?.[prop];
const av = aRaw?.toString().toLowerCase();
const bv = bRaw?.toString().toLowerCase();

return av == bv ? 0 : av > bv ? 1 : -1;
}
Expand Down
10 changes: 8 additions & 2 deletions src/utils/column.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ export function getCellData(val?: any) {
return val;
}

export function getCellRaw(model: DataType, column: ColumnRegular) {
if (column.cellParser) {
return column.cellParser(model, column);
}
return model[column.prop];
}

export function getCellDataParsed(model: DataType, column: ColumnRegular) {
const val = column.cellParser ? column.cellParser(model, column) : model[column.prop];
return getCellData(val);
return getCellData(getCellRaw(model, column));
}

/**
Expand Down

0 comments on commit 9117a91

Please sign in to comment.