NGXS:更新深层嵌套数组状态

发布于 2025-01-22 23:06:28 字数 617 浏览 0 评论 0原文

因此,我有以下状态:

[
   {
      name: "Main Tree",
      branches: [
        {
          name: "Branch 1",
          branches: []
        },
        {
          name: "Branch 2",
          branches: [
            {
              name: "Sub Branch 1",
              branches: []
            }
          ]
        }
      ]
   }
]

我的问题是,如何使用状态操作员在分支中附加另一个分支 sub Branch 1 ?我在此,但它将数据从数组转换为不是所需输出的对象。

同样,上面的情况只是一个示例,我的用例是它可能是内部有分支的无限级分支。

先感谢您。

So I have the following state:

[
   {
      name: "Main Tree",
      branches: [
        {
          name: "Branch 1",
          branches: []
        },
        {
          name: "Branch 2",
          branches: [
            {
              name: "Sub Branch 1",
              branches: []
            }
          ]
        }
      ]
   }
]

My question is, how do I append another branch under the branches in Sub Branch 1 using state operators? I tried the solution on this link (State Operators) but it converts the data from array into object which is not the desired output.

Also the above scenario is just an example, my use-case is that it could be an infinite level of sub branches with branches inside them.

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

也只是曾经 2025-01-29 23:06:28

这应该适用于上述示例:

// import { append, patch, updateItem } from '@ngxs/store/operators';

interface BranchTree {
  name: string;
  branches: BranchTree[];
}

type BranchStateModel = Array<BranchTree>;

//..
@Action(UpdateSubBranch)
updateSubBranch({ setState }: StateContext<BranchStateModel>) {
  const newBranch: BranchTree = {
    name: 'Sub 1 of Sub Branch 1',
    branches: [],
  };
  setState(
    updateItem(
      (branch) => branch.name === 'Main Tree',
      patch({
        branches: updateItem<BranchTree>(
          (subBranch) => subBranch.name === 'Branch 2',
          patch({
            branches: updateItem<BranchTree>(
              (subBranch) => subBranch.name === 'Sub Branch 1',
              patch({ branches: append([newBranch]) })
            ),
          })
        ),
      })
    )
  );
}

但是,您需要根据上述解决方案为无限树实施递归解决方案。

在此处阅读有关NGXS州运营商的更多信息: https://wwww.ngxs.io/ v/v3.7/高级/操作员

This should work for the above example:

// import { append, patch, updateItem } from '@ngxs/store/operators';

interface BranchTree {
  name: string;
  branches: BranchTree[];
}

type BranchStateModel = Array<BranchTree>;

//..
@Action(UpdateSubBranch)
updateSubBranch({ setState }: StateContext<BranchStateModel>) {
  const newBranch: BranchTree = {
    name: 'Sub 1 of Sub Branch 1',
    branches: [],
  };
  setState(
    updateItem(
      (branch) => branch.name === 'Main Tree',
      patch({
        branches: updateItem<BranchTree>(
          (subBranch) => subBranch.name === 'Branch 2',
          patch({
            branches: updateItem<BranchTree>(
              (subBranch) => subBranch.name === 'Sub Branch 1',
              patch({ branches: append([newBranch]) })
            ),
          })
        ),
      })
    )
  );
}

However, you need to implement your recursive solution for the infinite tree based on the above solution.

Read more about NGXS State Operators here: https://www.ngxs.io/v/v3.7/advanced/operators

情话墙 2025-01-29 23:06:28

的嵌套阵列

tasks
   table
      cols: [{...}, {...}, {etc}]
      rows: [{...}, {...}, {etc}]

对于像我成功使用

ctx.setState(
  patch({
    table: patch({
      rows: iif<any>(
        (tasks) => tasks?.some((task: any) => task.id === updatedTask?.id),
        updateItem<any>((task: any) => task.id === updatedTask?.id, patch(updatedTask))
      ),
    }),
  }),
)

。我有一个NGXS迭代器的建议,我认为这是严重缺失的。
我稍后再进行更新。

For a nested array like

tasks
   table
      cols: [{...}, {...}, {etc}]
      rows: [{...}, {...}, {etc}]

I used this with success.

ctx.setState(
  patch({
    table: patch({
      rows: iif<any>(
        (tasks) => tasks?.some((task: any) => task.id === updatedTask?.id),
        updateItem<any>((task: any) => task.id === updatedTask?.id, patch(updatedTask))
      ),
    }),
  }),
)

I have a proposal for an NGXS iterator which I feel is seriously missing.
I'll update that later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文