【发布时间】:2021-10-27 17:26:04
【问题描述】:
在文档中我找到了一个使用sortComparer 的简单示例,问题是我们只按名称排序,如果我想按不同的属性进行排序,我必须以某种方式提供有关键/属性的信息。我知道我可以在每个实体中保持排序信息,但我不想复制它......我更喜欢从主状态获取它......我可以以某种方式注入一个将提供sort 状态的服务来自主要州?
reducer.ts
export function advancedSorting(a, b): any {
const sortState = { key: 'name', order: 1 }; // temporary mock - get sort state from main store
const itemA = a[sortState.key];
const itemB = b[sortState.key];
return itemA < itemB
? -1 * sortState.order
: itemA > itemB
? sortState.order
: 0;
}
export const adapter: EntityAdapter<ProductDetails> =
createEntityAdapter<ProductDetails>({
sortComparer: advancedSorting
});
export const initialState: ProductsState = adapter.getInitialState({
productType: null,
productTags: [],
sort: {
key: 'name',
order: 1
}
});
【问题讨论】:
标签: angular typescript ngrx ngrx-entity