i have angular table <li *ngFor="let category of categoryListPagination; let i=index"…
- User
i have angular table <li *ngFor="let category of categoryListPagination; let i=index" [ngClass]="{'selectedItem' : selectedCategory.includes(category.gid) === category.gid}" class="productList_item" (click)="selectCategory(category)"> i want to add selectedItem class everytime i select row and this is the function in ts let selectedCategory='' selectCategory(category:any){ // this.selectedCategory=category // console.log(category.gid) // let selectedGids: string = ''; if(this.selectedCategory.includes(category.gid)){ this.selectedCategory = this.selectedCategory.replace(category.gid, ""); }else{ this.selectedCategory+=category.gid+','; }
- Assistant
To add the "selectedItem" class to the row every time it is selected, you can modify the existing code as follows:
- In your TypeScript file, initialize
selectedCategoryas an array instead of an empty string:
selectedCategory: string[] = [];- Modify the
selectCategoryfunction to handle the selection/deselection logic:
selectCategory(category: any) { if (this.selectedCategory.includes(category.gid)) { // Deselect the category const index = this.selectedCategory.indexOf(category.gid); this.selectedCategory.splice(index, 1); } else { // Select the category this.selectedCategory.push(category.gid); } }- Update the
[ngClass]attribute in the HTML to check if theselectedCategoryarray includes the category'sgidvalue:
[ngClass]="{ 'selectedItem': selectedCategory.includes(category.gid) }"With these changes, the "selectedItem" class will be applied to the row when it is selected and removed when it is deselected.
- In your TypeScript file, initialize