1

I have a grid with checkbox, I am putting checked values in an array (currentFocusedRow), now I want to assign activeRow class to the checked row:

// app.ts
class contact {
  public contactlist = [{icontact_id: "contact1"}, {icontact_id: "contact2"}, {icontact_id: "contact3"}, {...}];
  public currentFocusedRow = ["contact2", "contact3"];
}

// app.html
<tr *ngFor="let contact of contactlist"
   [class.activeRow]="contact.icontact_id == currentFocusedRow">
   ...
</tr>

now since currentFocusedRow is an array i can't simply check like that (contact.icontact_id == currentFocusedRow) there should be something to check if the value is present in that array or not like indexOf.

3 Answers 3

2

I think this should work for you:

Typescript

isActive(id) {
    return this.currentFocusedRow.indexOf(id) !== -1
}

HTML

<tr *ngFor="let contact of contactlist"
    [class.activeRow]="isActive(contact.icontact_id)">
</tr>
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah I can do that as well but checking directly in DOM seems to be more convenient.
You can simply paste the code in to the [class.activeRow] block
like this: [class.activeRow]="currentFocusedRow.indexOf(contact.icontact_id) !== -1"
However i don't think there is any change in performance. It's simply a matter of how you want to format your code.
1

You can use index of the object that is current object, by using:

<tr *ngFor="let contact of contactlist; let i = index">

Which will enable you to get those indexes.

Apart from that, you can just use NgClass directive.

1 Comment

how indexes will help me here? Are you suggesting to put something like this [class.activeRow]="contact.icontact_id == currentFocusedRow[i]" it won't work
0

Checking with indexOf in directive itself worked, this is pretty amazing that we can use this JS methods directly in DOM:

<tr *ngFor="let contact of contactlist;"
    [class.activeRow]="currentFocusedRow.indexOf(contact.icontact_id) != '-1'">
</tr>

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.