Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set value in formArray

Tags:

forms

angular

I have got a formgroup named input form, having an formarray of topics. I want to change the value of an inputfield using a dynamically created filtered list.

the filter function works fine:

filtertopic(idx: number) {
  const test = this.topics.value[idx].topic;
  if (test !== "") {
    this.onderdeel = "topics";
    this.queryselector = idx;
    this.filteredList = this.Topics.filter(function(el) {
      return el.toLowerCase().indexOf(test.toLowerCase()) >-1;
    }.bind(this));
     } else { this.filteredList = [];
    }
    }

but the function handleBlur to change the value in the input field does not work

handleBlur() {
    console.log(this.selectedIdx);
    if (this.selectedIdx > -1) {
      if (this.onderdeel =="topics") {
        this.topics.value[this.queryselector].topic.setValue(this.filteredList[this.selectedIdx]);
      } else {
        this.query = this.filteredList[this.selectedIdx];
      }
    }
    this.filteredList = [];
    this.selectedIdx = -1;
}

I think it has to do with the

this.topics.value[this.queryselector].topic.setValue(this.filteredList[this.selectedIdx]);

to set the formcontrol value. Does anybody know the solution?

like image 999
Mihaly Avatar asked May 22 '26 15:05

Mihaly


1 Answers

 this.yourdataobject.forEach(task => {
      this.formcontrolname.push(
        this.fb.group({
          name: [task.name, Validators.required]
        })
      );
    });
like image 160
Laxmikanta Nayak Avatar answered May 24 '26 17:05

Laxmikanta Nayak