Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
how to render an element for every selected element inside multi selectbox

Right now I render for every selected element inside my multiselect angular material selectbox.

The rendering works when I am selecting an element inside it. But when I deselect one it does just keeps adding.

My guess is that I have to set a property like deselect or something but I am not sure, could someone help me out?

HTML:

<mat-select [formControl]="person" required>
    <mat-option>--</mat-option>
    <mat-option *ngFor="let person of person" [value]="person" (click)="getPerson()">
      {{person.name}}
    </mat-option>
</mat-select>

<div *ngFor="let person of personsArray">
    <div class="card"  >
        <div class="card-body dl-card-body-no-padding-bottom" >  
            //element to render
    </div>
</div>
 

TS:

getPerson(){
   this.personsArray.push(this.form.controls.person.value)
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In your HTML for your mat-select you will need to include the multiple attribute.

Also, make sure you appropriately name the arrays to store the items from the source array and the selected array. It's rather confusing to refer to person twice in the select template.

You need to pass data into the selection handler. This is done as shown in the amended HTML below:

<mat-select [formControl]="person" required multiple>
  <mat-option>--</mat-option>
  <mat-option
    *ngFor="let person of personsArray"
    [value]="person"
    (click)="getPerson(person)"
  >
    {{ person.name }}
  </mat-option>
</mat-select>

In your component code, include an array for the source array and selected items array:

form: any;
person = new FormControl('');
personsArray: Person[] = [
    { name: 'bob' },
    { name: 'dave' },
    { name: 'fred' },
];
selectedPersonsArray: Person[] = [];

constructor() {
    this.form = new FormGroup({
      person: new FormControl(''),
    });
}

In your selection handler, you need to handle the appending and removal of items depending on whether they already exist (remove) or do not exist (append) in the select items array:

getPerson(val: Person) {
  console.log('selected person = ' + val.name);
  const person: Person = this.selectedPersonsArray.find(
    (p) => p.name === val.name
  );
  console.log('sel person=' + person?.name);
  if (!person) this.selectedPersonsArray.push(val);
  if (person?.name)
    this.selectedPersonsArray = this.selectedPersonsArray.filter(
      (p) => p.name !== val.name
    );
}

The person object is just an interface similar to this:

export interface Person {
    name: string;
}

The above is sufficient to allow selected or un-selected items within multi-selection drop down to display within the rendered list.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda