【问题标题】:Ionic 2 Multiple Checkboxes with FormBuilder带有 FormBuilder 的 Ionic 2 多个复选框
【发布时间】:2017-11-02 06:50:39
【问题描述】:

Survey app checkbox photo

我目前面临在多项选择题上构建表单的问题。目前单选的形式很好,只剩下选择题。我想要的目标是这样的:

{
  "questions": [
    // if multiple choices question
    {
      "question_id": 17,
      "answer_ids": [81,82,83]
    },
    {
      "question_id": 20,
      "answer_ids": [101,102,104]
    }
  ]
}

调查.ts

    this.surveyForm = this.formBuilder.group({
      questions: formBuilder.array([])
    })

    for (var i = 0; i < this.questions.length; i++) {

      if(this.questions[i].question_type == '2') {
        // get multiple
        let question = formBuilder.group({
          question_id: [this.questions[i].id, Validators.required],
          answer_ids: formBuilder.array([])
        });
        this.surveyForm.controls['questions'].push(question);
        // for (var j = 0; j < this.questions[i].answers.length; j++) {
        //   console.log(j);
          // let answer = formBuilder.array(this.questions[i].answers)
          // console.log(answer)
          // this.questions[i].answers[j].push(new FormControl());
          // this.surveyForm.controls['questions'].controls['answer_ids'].push(new FormControl('', [Validators.required]))
        // };
        console.log('m')
      }
    }

调查.html

 <div *ngIf="surveyForm">
    <form [formGroup]="surveyForm">
      <div formArrayName="questions">
        <div *ngFor="let question of questions; let i = index" [formGroupName]="i" padding-bottom>
          <ion-row>
            <ion-col col-2>
              <h5>{{ i + 1 }}.</h5>
            </ion-col>
            <ion-col col-10>
              <h5>{{ question.text }}</h5>
              <p>{{ question.instruction }}</p>
              <div *ngIf="question.question_type == 1; else multiple_choice">
              <ng-template #multiple_choice>
                <ion-list formArrayName="answer_ids">
                  <div *ngFor="let choice of question.answers; let i = index">
                    <ion-item>
                      <ion-label style="white-space: normal;">{{ choice.id }}</ion-label>
                      <ion-checkbox (ionChange)="onChange(choice.id, $event._checked)" value="choice.id"></ion-checkbox>
                    </ion-item>
                  </div>
                </ion-list>
              </ng-template>
            </ion-col>
          </ion-row>
        </div>
      </div>
    </form>
  </div>

我可以从每个多项选择复选框中获取值 id 以推送到特定问题数组中的最佳方法是什么?

【问题讨论】:

  • @AJT_82 好的,它似乎很接近结构。似乎它必须为不同的 answer_id 引用 question_id。不知何故,我无法使用此代码获取推送数据 this.surveyForm.controls['questions'].controls['answer_ids'].push(new FormControl('' ", [Validators.required])) 我在做吗错了吗?
  • 你能创建一个我可以修补的 plunker 吗? :)
  • 哈哈,在我看到这个之前,你刚刚解决了我的问题。 :P
  • 没问题。是的,我昨天有一些额外的时间,所以我决定自己做 :D 现在不要被宠坏了,这可能不会每次都发生,哈哈。但它成功了,很高兴听到我能提供帮助。周末愉快,编码愉快! :)

标签: angular ionic2 survey ionic3


【解决方案1】:

这个问题和这个很相似:Angular 2 how to get the multiple checkbox value?

但由于您的嵌套更深,我们需要更深入地研究复选框的更改事件。

从模板中,我们需要在更改事件中传递choice.id, $event.checkedi。请注意索引模板中的命名约定。您对顶级迭代和嵌套迭代都使用相同的名称i,我已将嵌套迭代更改为j,所以:

<div *ngFor="let choice of question.answers; let j = index">
  <ion-item>
    <ion-label style="white-space: normal;">{{ choice.id }}</ion-label>
    <ion-checkbox (ionChange)="onChange(choice.id, $event.checked, i)" value="choice.id">
    </ion-checkbox>
  </ion-item>
</div>

请注意,在更改事件中,我们传递了i,这是我们对问题进行迭代的顶级迭代。

然后转到更改功能。这里与上面链接问题中的答案类似,但我们需要更深入地挖掘,因为我们需要达到的是内部 formArray。

更改事件看起来像这样,我们内部表单数组的路径是可怕的,从下面answers 的值可以看出:)

onChange(id, isChecked, index) {
   // nested formarray, which is inside nested formgroup, inside outer array
   const answers = <FormArray>this.surveyForm.controls.questions.controls[index].controls.answer_ids

  if(isChecked) {
     answers.push(new FormControl(id))
  } else {
     let idx = answers.controls.findIndex(x => x.value == id)
     answers.removeAt(idx)
  }
}

应该这样做! :)

这是一个DEMO

【讨论】:

  • 谢谢!这是我正在寻找的解决方案。它适用于我的单选题和多选题。
猜你喜欢
  • 1970-01-01
  • 2017-12-11
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 2021-09-09
  • 1970-01-01
  • 1970-01-01
  • 2019-11-30
相关资源
最近更新 更多