【问题标题】:Add and Remove nested items to array in AngularJS在 AngularJS 中向数组中添加和删除嵌套项
【发布时间】:2016-12-19 09:23:30
【问题描述】:

我想在 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 中选择(获取路径)一个选项以将子问题推送到其他子问题的选择中?

【问题讨论】:

  • 我可以添加和处理根本问题,并为不同的问题类型创建指令并处理他们的选择

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


【解决方案1】:

如果您想从 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;

我希望这会有所帮助。

【讨论】:

  • 谢谢!它确实有很大帮助,我的主要问题是如何在选择中添加/删除子问题,例如如何将子问题添加到“三级索引 2”中
  • 随时伴侣。乐于助人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多