在 AngularJS 中的数组中添加和删除嵌套项
Add and Remove nested items to array in AngularJS
我想在 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 select(获取路径)一个选择中将一个子问题推入另一个子问题的选择中?
如果您想从 html 中获取选择的路径,您可以通过为您的问题提供索引(唯一 ID)来实现,我建议您从数组中更改 "choices"到对象,以便您可以使用指定的唯一 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;
希望对您有所帮助。
我想在 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 select(获取路径)一个选择中将一个子问题推入另一个子问题的选择中?
如果您想从 html 中获取选择的路径,您可以通过为您的问题提供索引(唯一 ID)来实现,我建议您从数组中更改 "choices"到对象,以便您可以使用指定的唯一 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;
希望对您有所帮助。