0

の配列にネストされた項目を追加し、削除します。は、私はこのような親の質問の選択肢に無制限のお子様質問を追加および削除する機能を持つAngualrJSにフォームビルダを作成したいAngularJS

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から選択肢を選択することができます方法を知りたいですか?

+0

ルート質問を追加して処理し、さまざまな質問タイプのディレクティブを作成し、その選択を処理することができます –

答えて

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; 

私はこれが役立ちます。

+1

ありがとうございます!それは確かに多くの助けになります。私の主な問題は、チャイルド・クエスチョンを選択肢に追加/削除する方法です。たとえば、子供質問を「第3インデックス2」に追加する方法です。 –

+0

いつでも仲間です。お力になれて、嬉しいです。 – Theo

関連する問題