0

I have a list of items and when the user clicks on an item, this item gets highlited. We do this by simple adding *ngIf="object.shouldHeight" and works.

My Problem:

When the user clicks on another item, the previous selected item should be unhighlighted

My first idea for the solution:

The first solution I thought was to iterate through all items, change their status to false and then set the current status to true. This would work!

My second idea for the solution:

The other was to use this, but this is not working as toogle, means, when I click on a next item, the previous one is still highlighted.


How can I achieve this in Angular2 without writing loops in my code?


UPDATE:

I achieved this, althought I still think it could be more elegant:

In my component I have the following function:

public showButtonbar(i: number) {
    this.hightlightStatus = []
    this.hightlightStatus[i] = true;
}

And that is my click event, where i is the index:

(click)="showButtonbar(i)"

2 Answers 2

0

Bind list item's CSS class, so that when the condition is true Angular2 adds that CSS class, that defines highlited item style.

E.g.:

<tr *ngFor="let row of rows; let rowIdx = index;"
  [class.myhighlight]="rowIdx === highlightRowIdx">
</tr>

The above adds CSS class myhighlight to the table row when the row index is equal to highlightRowIdx member variable of the model/controller.

Sign up to request clarification or add additional context in comments.

1 Comment

What about the click event? Where does it take place and what should I set to it?
0

Here is a short list-component-guide. This component is also able to hightlight the selected element. Maybe you can find some help there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.