Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,
I want to trigger onchange of Prime ng multiselect control dynamically from component.ts file where ever i require. but i'm getting issues to achieve it.

What I have tried:

html code of p multiselct
==============================
 lang="HTML"<p-multiSelect id="capturesByFilter"
ngModelChange)="value" 
#capFilter [options]="captures" 
placeholder="Please Select"
(onChange)="filter($event.value); 

applyCaptureNameFilter($event.value)" 
optionLabel="name" 
appendTo="body" 
formControlName="selectedCaptureInfo" 
[filter]="true">


component.ts
==============
TypeScript
@ViewChild('capFilter') capFilter;

loadAllFilterData(){
this.capFilter.nativeElement.dispatchEvent(new Event('change'));
}


i'm getting console error like capFilter.nativeElement is undefined.
How can i solve this to trigger onchange event.

Thanks in advance.
Posted
Updated 14-Jul-23 1:18am
v3

In order to trigger the onChange event of the PrimeNG multiselect control dynamically from the component.ts file, you can make use of the updateModel method provided by the MultiSelect component.
HTML code of p-multiselect:
<p-multiSelect #capFilter [options]="captures" placeholder="Please Select" optionLabel="name"
  [(ngModel)]="selectedCaptureInfo" (onChange)="filter($event.value); applyCaptureNameFilter($event.value)"
  [filter]="true"></p-multiSelect>
 
Share this answer
 
Comments
pavan_ kumar 13-Jul-23 14:31pm    
Hi,
Thanks for giving reply. I've tried by putting [(ngModel)]. But in my case, i've to use formcontrolname in order to work my other logic which i've written in my code. and using of ngmodel and formcontrolname at a time has deprecated in angular 12, So, i can't use both at a time.
Please let me know any other approach by using formcontrolname only.
Thanks.
You can trigger the 'onChange' event of your control by using the 'valueChanges' event of the form control, a method not deprecated. a Full tutorial is available at - ValueChanges in Angular Forms[^]

To do this, import the necessary classes in your component's TypeScript file -
TypeScript
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';


Now create a form control for your 'selectedCaptureInfo' field and subscribe to its 'valueChanges' observable -
TypeScript
export class YourComponent implements OnInit {
  selectedCaptureInfoControl: FormControl;

  ngOnInit() {
    this.selectedCaptureInfoControl = new FormControl();

    this.selectedCaptureInfoControl.valueChanges.subscribe(value => {
      //Call your function(s) when the value changes
      this.filter(value);
      this.applyCaptureNameFilter(value);
    });
  }

  //Rest of your component code
}


Bind the 'selectedCaptureInfoControl' to the 'formControlName' in your HTML code -
HTML
<p-multiSelect id="capturesByFilter"
               [options]="captures"
               placeholder="Please Select"
               optionLabel="name"
               appendTo="body"
               [filter]="true"
               [formControl]="selectedCaptureInfoControl">
</p-multiSelect>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900