Skip to content

Commit

Permalink
feat(grid): support sort by specified attribute value for field which…
Browse files Browse the repository at this point in the history
… is object type (#223)
  • Loading branch information
minlovehua authored Jan 7, 2025
1 parent 143233c commit 14308c4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/grid/src/types/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export interface AIFieldConfig {
export interface AITableUserInfo {
uid?: string;
display_name?: string;
display_name_pinyin?: string;
avatar?: string;
[key: string]: any;
}

export interface AITableReferences {
Expand Down
8 changes: 7 additions & 1 deletion packages/grid/src/utils/field/model/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { isEmpty } from '../../common';

export abstract class Field {
// 排序
abstract compare(cellValue1: FieldValue, cellValue2: FieldValue, field: AITableField, references?: AITableReferences): number;
abstract compare(
cellValue1: FieldValue,
cellValue2: FieldValue,
field: AITableField,
references?: AITableReferences,
sortKey?: string
): number;

// 筛选
isMeetFilter(condition: AITableFilterCondition, cellValue: FieldValue) {
Expand Down
29 changes: 21 additions & 8 deletions packages/grid/src/utils/field/model/member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ export class MemberField extends Field {
}
}

override compare(cellValue1: FieldValue, cellValue2: FieldValue, field: AITableField, references: AITableReferences): number {
const value1 = cellValueToSortValue(cellValue1, field, references);
const value2 = cellValueToSortValue(cellValue2, field, references);
override compare(
cellValue1: FieldValue,
cellValue2: FieldValue,
field: AITableField,
references: AITableReferences,
sortKey: string
): number {
const value1 = cellValueToSortValue(cellValue1, field, references, sortKey);
const value2 = cellValueToSortValue(cellValue2, field, references, sortKey);
return compareString(value1, value2);
}

Expand All @@ -43,18 +49,25 @@ export class MemberField extends Field {
}
}

function cellValueToSortValue(cellValue: MemberFieldValue, field: AITableField, references: AITableReferences): string | null {
let names: string[] = [];
function cellValueToSortValue(
cellValue: MemberFieldValue,
field: AITableField,
references: AITableReferences,
sortKey = 'display_name'
): string | null {
let values: string[] = [];
if (cellValue?.length && references) {
for (let index = 0; index < cellValue.length; index++) {
const userInfo = references?.members[cellValue[index]];
if (!userInfo) {
continue;
}
if (userInfo.display_name_pinyin) {
names.push(userInfo.display_name_pinyin);

const value = userInfo[sortKey];
if (value) {
values.push(value);
}
}
}
return names && names.length ? names.join(', ') : null;
return values && values.length ? values.join(', ') : null;
}
11 changes: 9 additions & 2 deletions packages/state/src/utils/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ import { AITableView, AITableViewFields, AITableViewRecords, AIViewTable } from
import { getSortRecords } from './record/sort';
import { getFilteredRecords } from './record/filter';
import { getSortFields } from './field/sort-fields';
import { AITableFieldType } from '@ai-table/grid';

export function buildRecordsByView(aiTable: AIViewTable, records: AITableViewRecords, fields: AITableViewFields, activeView: AITableView) {
export function buildRecordsByView(
aiTable: AIViewTable,
records: AITableViewRecords,
fields: AITableViewFields,
activeView: AITableView,
sortKeysMap?: Partial<Record<AITableFieldType, string>>
) {
const filteredRecords = getFilteredRecords(aiTable, records, fields, activeView);
return getSortRecords(aiTable, filteredRecords, activeView);
return getSortRecords(aiTable, filteredRecords, activeView, sortKeysMap);
}

export function buildFieldsByView(aiTable: AIViewTable, fields: AITableViewFields, activeView: AITableView) {
Expand Down
21 changes: 16 additions & 5 deletions packages/state/src/utils/record/sort.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { AITable, AITableQueries, ViewOperationMap } from '@ai-table/grid';
import { AITable, AITableFieldType, AITableQueries, ViewOperationMap } from '@ai-table/grid';
import { AITableView, AITableViewRecords } from '../../types';
import { sortByViewPosition } from '../common';

export function getSortRecords(aiTable: AITable, records: AITableViewRecords, activeView: AITableView) {
export function getSortRecords(
aiTable: AITable,
records: AITableViewRecords,
activeView: AITableView,
sortKeysMap?: Partial<Record<AITableFieldType, string>>
) {
if (!activeView?.settings || !activeView.settings.sorts?.length) {
return records;
}
const { is_keep_sort, sorts } = activeView.settings;
if (is_keep_sort && sorts?.length) {
return sortRecordsBySortInfo(aiTable, records, activeView);
return sortRecordsBySortInfo(aiTable, records, activeView, sortKeysMap);
}
return sortByViewPosition(records, activeView);
}

export function sortRecordsBySortInfo(aiTable: AITable, records: AITableViewRecords, activeView: AITableView) {
export function sortRecordsBySortInfo(
aiTable: AITable,
records: AITableViewRecords,
activeView: AITableView,
sortKeysMap?: Partial<Record<AITableFieldType, string>>
) {
const shallowRows = [...records];
if (activeView.settings?.sorts?.length) {
shallowRows.sort((prev, current) => {
Expand All @@ -23,11 +33,12 @@ export function sortRecordsBySortInfo(aiTable: AITable, records: AITableViewReco
return acc;
}
const fieldMethod = ViewOperationMap[field.type];
const sortKey = sortKeysMap?.[field.type];

const cellValue1 = AITableQueries.getFieldValue(aiTable, [prev._id, field._id]);
const cellValue2 = AITableQueries.getFieldValue(aiTable, [current._id, field._id]);
const references = aiTable.references();
const res = fieldMethod.compare(cellValue1, cellValue2, field, references);
const res = fieldMethod.compare(cellValue1, cellValue2, field, references, sortKey);
return res * rule.direction;
}, 0);
});
Expand Down
16 changes: 14 additions & 2 deletions src/app/service/table.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Router } from '@angular/router';
import { WebsocketProvider } from 'y-websocket';
import { getProvider } from '../provider';
import { getCanvasDefaultValue, sortDataByView } from '../utils/utils';
import { AITableValue } from '@ai-table/grid';
import { AITableFieldType, AITableValue } from '@ai-table/grid';

export const LOCAL_STORAGE_KEY = 'ai-table-active-view-id';
const LOCAL_STORAGE_AI_TABLE_SHARED_DATA = 'ai-table-demo-shared-data';
Expand Down Expand Up @@ -52,8 +52,20 @@ export class TableService {
return this.activeView().short_id;
});

sortKeysMap: Partial<Record<AITableFieldType, string>> = {
[AITableFieldType.createdBy]: 'display_name_pinyin',
[AITableFieldType.updatedBy]: 'display_name_pinyin',
[AITableFieldType.member]: 'display_name_pinyin'
};

renderRecords = computed(() => {
return buildRecordsByView(this.aiTable, this.records(), this.fields(), this.activeView()) as AITableViewRecords;
return buildRecordsByView(
this.aiTable,
this.records(),
this.fields(),
this.activeView() as AITableView,
this.sortKeysMap
) as AITableViewRecords;
});

renderFields = computed(() => {
Expand Down

0 comments on commit 14308c4

Please sign in to comment.