javascript - 在 AngularJS 中添加和删除数组中的嵌套项目

标签 javascript angularjs nested-loops nested-forms dynamic-forms

我想在 AngualrJS 中创建一个表单生成器,能够添加和删除无限的子问题来选择父问题,如下所示:

Question 1
  ---Choice1.1
  ---Choice1.2
     ---Child-Question 1.1
       ---Choice1
       ---Choice2
          ---Child-Question 1.1.2
            ---Choice1
            ---Choice2
       ---Choice3
  ---Choice1.3
Question 2
  ---Choice2.1
  ---Choice2.2
  ---Choice2.3

这是我想要动态创建的示例 JSON:

{
title: "string",
description: "string",
questionType: 0,
option: {
    min: 0,
    max: 0,
    step: 0,
    unit: "string"
},
choices: [
    {
        title: "string",
        order: 0,
        matrixPosition: 0,
        childQuestionTemplate: {}
    },
    {
        title: "string",
        order: 0,
        matrixPosition: 0,
        childQuestionTemplate: {
            title: "string",
            description: "string",
            questionType: 0,
            option: {},
            choices: [
                {
                    title: "string",
                    order: 0,
                    matrixPosition: 0,
                    childQuestionTemplate: {}
                }
            ]
        }
    },
    {
        title: "string",
        order: 0,
        matrixPosition: 0,
        childQuestionTemplate: {
            id: 0,
            title: "string",
            description: "string",
            questionType: 0,
            option: {
                min: 0,
                max: 0,
                step: 0,
                unit: "string"
            },
            choices: [
                {
                    title: "string",
                    order: 0,
                    matrixPosition: 0,
                    childQuestionTemplate: {
                        title: "string",
                        description: "string",
                        questionType: 0,
                        option: {},
                        choices: [
                            {
                                title: "string",
                                order: 0,
                                matrixPosition: 0,
                                childQuestionTemplate: {}
                            }
                        ]
                    }
                }
            ]
        }
    },
    {
        title: "string",
        order: 0,
        matrixPosition: 0,
        childQuestionTemplate: {}
    }
]
}

现在我想知道如何从 HTML 中选择一个选项(获取路径),将一个子问题推送到其他子问题的选择中?

最佳答案

如果你想从html中获取选择的路径,你可以通过为你的问题提供一个索引(唯一ID)来做到这一点,我建议你将“选择”从数组更改为对象,这样你就可以直接访问它使用指定的唯一 ID。

示例是:

var jsonObject = {
    "1-1" : {
        title: "primary index",
        description: "string",
        questionType: 0,
        option: {
            min: 0,
            max: 0,
            step: 0,
            unit: "string"
        },
        choices: {
            "2-1" : {
                title: "secondary index",
                order: 0,
                matrixPosition: 0,
                childQuestionTemplate: {}
            },
            "2-2" : {
                title: "string",
                order: 0,
                matrixPosition: 0,
                childQuestionTemplate: {
                    title: "string",
                    description: "string",
                    questionType: 0,
                    option: {},
                    choices: {
                       "3-1" : {
                            title: "tertiary index 1",
                            order: 0,
                            matrixPosition: 0,
                            childQuestionTemplate: {}
                        },
                        "3-2" : {
                            title: "tertiary index 2",
                            order: 0,
                            matrixPosition: 0,
                            childQuestionTemplate: {}
                        }
                    }
                }
            }
        }
    }
}

所以如果你想删除一个选择,你可以这样做

var primaryIndex = "1-1";

//remove on the tertiary index
var secondaryIndex = "2-2";
var tertiaryIndexToDelete = "3-1";
delete jsonObject[primaryIndex].choices[secondaryIndex].childQuestionTemplate.choices[tertiaryIndexToDelete]; 

//remove on the secondary index
var secondaryIndexToDelete = "2-2";
delete jsonObject[primaryIndex].choices[secondaryIndexToDelete]; 

现在,如果您想为问题添加选项,您可以这样做

var newChoice = {
    title: "new choice",
    order: 0,
    matrixPosition: 0,
    childQuestionTemplate: {}
}
var indexOfNewChoice = "2-3";
jsonObject["1-1"].choices[indexOfNewChoice] = newChoice;

我希望这会有所帮助。

关于javascript - 在 AngularJS 中添加和删除数组中的嵌套项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41219345/

相关文章:

javascript - 使用 Material 表库时如何更改删除过滤器图标?

javascript - Ajax - 处理正确的数组以传递给 PHP

javascript - Bootstrap 模式没有出现屏幕空白

javascript - $state.go 将/#q=hello 的 url 更改为/#/q=hello

python - 将参数从父函数传递给嵌套函数Python

javascript - 使用需要 header 并提供内容的 REST 请求下载文件

javascript - render 方法中的 react map() 函数晚了一步

javascript - Angular ,如何停止跟踪服务对象

c - 在由数组、循环和 if 语句组成的简单 Tic-Tac-Toe 游戏中打印标记为 'X' 'O'

c++ - 在运行时定义循环