Home > Blockchain >  How to select only one div at a time in angular 10
How to select only one div at a time in angular 10

Time:07-27

I have to create a sort filter in my application where I can select only one option at a time to sort my results. My UI is such that I can not use checkboxes. It is simple list. So I am creating the list through div. Can anyone suggest how to impleenter image description herement selection and deselection of div in angular. I can select only one div at a time. Also If I have selected any option and now i click on option 2 then option 1 div should get de-selected and option 2 div should get selected.

CodePudding user response:

One solution would be to set the 'current' value when the div is clicked. Add a class when the 'current' value matches the item's value.

<div *ngFor="let item of items" 
(click)="onItemSelection(item) 
[class.current-selection]="item === currentValue">
   {{ item }}
</div>

Handle the event in your component.ts file by storing the new value:

currentValue: string;

onItemSelection(newValue: string) {
   this.currentValue = newValue;
}

Then update the look of the current item in your component.scss file:

.current-selection {
  background-color: green;
}
  • Related