Angular NGRX 实体计数相同的对象
Angular NGRX Entity counting the same object
我接到一个电话,我得到了一份有未完成报告要提交的人员的 ID 列表,我想跟踪该人有未完成报告的数量。我正在使用 EntityAdapter 存储该数据。现在我不知道如何在适配器中保持计数。
到目前为止,我已经检查了 ngrx 文档,对代码进行了相当多的操作,并在 gitter 聊天室中提出了我的问题,目前还没有结果。
我的服务returns 一个字符串数组,其中包含人员的 ID。示例数据可以是:
这里我有两次相同的Id,这意味着那个人有2个报告。我想存储该 Id 并保持计数“2”。目前我正在为适配器使用此代码:
export interface State extends EntityState<string> {
showRegistrations: boolean,
loading: boolean,
loaded: boolean,
}
export const adapter: EntityAdapter<string> = createEntityAdapter<string>({
selectId: (item: string) => item,
sortComparer: false,
});
export const initialState: State = adapter.getInitialState({
showRegistrations: true,
loading: false,
loaded: false,
});
这在我的商店中给出了以下结果:
但我实际上是在寻找以下结果,其中存储了每个 ID,并且我知道该 ID 具体被找到了多少次:
@ngrx/entity
旨在根据 id 存储和检索实体。
您描述的用例不适合 @ngrx/entity
.
为了跟进 Tim 的回答,我在 ngrx/entity 的框框外查看。我一开始就不应该专注于使用它,因为答案很简单。
我添加了一个包含字符串和数字的接口。在我的代码中,我将此称为 ReportCount。然后在 loadsuccess 操作中,我添加一个带有用户 ID 的新 reportCount 并将计数设置为 1,或者我将计数加 1。
最终我得到了以下按预期工作的代码:
(我将这篇文章发布给可能遇到同样问题的其他人)
export interface ReportCount{
superintendent: string,
count: number,
};
export interface State {
reportCounts: ReportCount[],
showRegistrations: boolean,
loading: boolean,
loaded: boolean,
};
export const initialState: State = {
reportCounts: [],
showRegistrations: true,
loading: false,
loaded: false,
};
export const reducer = createReducer(
initialState,
on(RegistrationActions.ShowRegistrations,
(state, { setShow }) => ({
...state,
showRegistrations: setShow,
})
),
on(RegistrationSuperintendentsCollectionActions.loadRegistrationSuperintendentCollection, (state) => ({
...state,
loading: true,
})),
on(RegistrationSuperintendentsCollectionApiActions.loadRegistrationSuperintendentsSuccess,
(state, { superintendents }) => {
const repCount: ReportCount[] = [];
superintendents.forEach(superintendent => {
let sup = repCount.find(s => s.superintendent === superintendent);
sup ? (sup.count = sup.count + 1) : repCount.push({superintendent, count:1});
})
return ({
...state,
reportCounts: repCount,
loading: false,
loaded: true
})
}
),
);
export const getShowRegistrations = (state: State) => state.showRegistrations;
export const getLoaded = (state: State) => state.loaded;
export const getLoading = (state: State) => state.loading;
export const getLoadedSuperintendentRegistrations = (state: State) => state.reportCounts;
我接到一个电话,我得到了一份有未完成报告要提交的人员的 ID 列表,我想跟踪该人有未完成报告的数量。我正在使用 EntityAdapter 存储该数据。现在我不知道如何在适配器中保持计数。
到目前为止,我已经检查了 ngrx 文档,对代码进行了相当多的操作,并在 gitter 聊天室中提出了我的问题,目前还没有结果。
我的服务returns 一个字符串数组,其中包含人员的 ID。示例数据可以是:
这里我有两次相同的Id,这意味着那个人有2个报告。我想存储该 Id 并保持计数“2”。目前我正在为适配器使用此代码:
export interface State extends EntityState<string> {
showRegistrations: boolean,
loading: boolean,
loaded: boolean,
}
export const adapter: EntityAdapter<string> = createEntityAdapter<string>({
selectId: (item: string) => item,
sortComparer: false,
});
export const initialState: State = adapter.getInitialState({
showRegistrations: true,
loading: false,
loaded: false,
});
这在我的商店中给出了以下结果:
但我实际上是在寻找以下结果,其中存储了每个 ID,并且我知道该 ID 具体被找到了多少次:
@ngrx/entity
旨在根据 id 存储和检索实体。
您描述的用例不适合 @ngrx/entity
.
为了跟进 Tim 的回答,我在 ngrx/entity 的框框外查看。我一开始就不应该专注于使用它,因为答案很简单。
我添加了一个包含字符串和数字的接口。在我的代码中,我将此称为 ReportCount。然后在 loadsuccess 操作中,我添加一个带有用户 ID 的新 reportCount 并将计数设置为 1,或者我将计数加 1。
最终我得到了以下按预期工作的代码:
(我将这篇文章发布给可能遇到同样问题的其他人)
export interface ReportCount{
superintendent: string,
count: number,
};
export interface State {
reportCounts: ReportCount[],
showRegistrations: boolean,
loading: boolean,
loaded: boolean,
};
export const initialState: State = {
reportCounts: [],
showRegistrations: true,
loading: false,
loaded: false,
};
export const reducer = createReducer(
initialState,
on(RegistrationActions.ShowRegistrations,
(state, { setShow }) => ({
...state,
showRegistrations: setShow,
})
),
on(RegistrationSuperintendentsCollectionActions.loadRegistrationSuperintendentCollection, (state) => ({
...state,
loading: true,
})),
on(RegistrationSuperintendentsCollectionApiActions.loadRegistrationSuperintendentsSuccess,
(state, { superintendents }) => {
const repCount: ReportCount[] = [];
superintendents.forEach(superintendent => {
let sup = repCount.find(s => s.superintendent === superintendent);
sup ? (sup.count = sup.count + 1) : repCount.push({superintendent, count:1});
})
return ({
...state,
reportCounts: repCount,
loading: false,
loaded: true
})
}
),
);
export const getShowRegistrations = (state: State) => state.showRegistrations;
export const getLoaded = (state: State) => state.loaded;
export const getLoading = (state: State) => state.loading;
export const getLoadedSuperintendentRegistrations = (state: State) => state.reportCounts;