Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
Matriz de filtrado en función del clic del botón de selección

Tengo una matriz de la siguiente manera:

 [ { "id":1, "active":1, "name":"paris" }, { "id":2, "active":0, "name":"london" }, { "id":3, "active":1, "name":"Australia" }, { "id":4, "active":0, "name":"india" } ]

Tengo un botón de selección de la siguiente manera:

stackblitz: https://stackblitz.com/edit/primeng-selectbutton-demo-gjxdgy?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component. CSS

codigo-

 <h5>Multiple Selection</h5> <p-selectButton [options]="paymentOptions" [(ngModel)]="value2" multiple="multiple" optionLabel="name" optionValue="value" ></p-selectButton>

componente -

 this.paymentOptions = [ { name: 'Option 1', value: 1 }, { name: 'Option 2', value: 2 } ];

Quiero filtrar la matriz de dichos datos, cuando se hace clic en 'Option 1' , entonces todos los elementos con 'active':1 deben estar presentes en la matriz. Si el usuario anula la selección 'option 1' , la matriz debe tener todos los elementos. Si se hace clic 'Option 2' , la matriz debe tener solo elementos con 'active':0 , si el usuario anula la selección 'Option 2' , la matriz debe tener todos los elementos. ¿Cómo puedo hacer eso?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Solo necesita pasar su selección al componente a través de ngModelChange y usarlo para ejecutar un filtro en su matriz para obtener el resultado deseado.

app.component.html (se agregó un código adicional para mostrar los valores coincidentes):

 <h5>Multiple Selection</h5> <p-selectButton [options]="paymentOptions" [(ngModel)]="value2" (ngModelChange)="filter($event)" multiple="multiple" optionLabel="name" optionValue="value" ></p-selectButton> <div>Selected option: {{value2}}</div> <div *ngFor="let item of filteredItems">{{ item.name}}</div>

aplicación.componente.ts:

 filter(data) { if (data.length === 0) { this.filteredItems = this.list; return; } this.filteredItems = []; this.filteredItems = this.list.filter( (item) => data.findIndex((x) => (x === item.active && x !== 2) || (item.active === 0 && x === 2)) > -1 ); }

enlace de stackblitz

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!