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

75
Views
Agregue una clase en la selección de elementos y elimínela para agregar otra selección de elementos

Tengo una tabla que me muestra una lista de elementos. Quiero poder agregar una clase en el elemento seleccionado y eliminarlo cuando seleccione otro.

Aquí está mi plantilla:

 <div class="row"> <div class="col-10 pl-3"> <h1>My Shopping List</h1> <h3 class="mb-0">Add all the ingredients you need</h3> <app-shopping-edit></app-shopping-edit> <table> <tr> <th>Ingredient</th> <th>Amount</th> </tr> <tr *ngFor="let ingredient of ingredients; let i = index" (click)="onEditItem(i)" > <td>{{ ingredient.name }}</td> <td>{{ ingredient.amount }}</td> </tr> </table> </div> </div>

Aquí está mi código ts:

 onEditItem(index: number) { this.shoppingListService.startedEditing.next(index); this.addStyleOfSelection(event); } addStyleOfSelection(event: any) { let selection = event.target; let trSelected = selection.parentNode; trSelected.classList.toggle('selected'); }

También "evento" está en desuso, pero no puedo averiguar qué usar en su lugar.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

puedes hacer algo asi

 <div *ngFor="let item of items" [class.is-selected]="item === selectedItem" (click)="selectedItem = item">{{ item.name }}</div>
about 4 years ago · Juan Pablo Isaza Report

0

también puedes probar de otra manera, *.html

 <div *ngFor="let item of items; let idx = index" [ngClass]="{ selected: activeIdx == idx }" (click)="handleActiveRow(idx)" > {{ item.name }} </div>

y también se ve el archivo *.ts

 public activeIdx: number | null = null; public items: Array<any> = [{ name: 'hitee' }, { name: 'krish' }]; public handleActiveRow(event: any) { this.activeIdx = event; }

y también parece *.css,

 .selected { background-color: blueviolet; }
about 4 years ago · Juan Pablo Isaza 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!