1

I'm trying to replace the checkbox with an image. This image will change based on css class whether or not it's checked. Adding a class doesn't work on change of checked because it changes all of the icons in the column. I'm using ion-checkbox for my checkbox. Doing ion-checkbox:checked does not work either in css.

A look at the images below I would like to replace the pin icon with the checkbox and when it's checked the class changes the icon to a red color and changes one of them, not all.

HTML

<tr *ngFor="let object of array">
  <td>
     <fa name="map-marker" [ngClass]="pinIcon ? 'unclick-pin' : 'click-pin'"></fa>
     <ion-checkbox (ionChange)="selectTwo(object, $event.checked)"></ion-checkbox>
  </td>
</tr>

TS

selectTwo(object, isChecked: boolean){
    this.pinIcon = !this.pinIcon;
}

before . after

5
  • I don't understand this part "Adding a class doesn't work on change of checked because it changes all of the icons in the column". What do you mean by that? Commented Nov 16, 2017 at 2:41
  • Let's say I click on one of the icons (aka supposed to be checking one of the checkboxes, the other rows with the same column - same icon will change because the value is associated with the on click of the icon change and not the checked change) Commented Nov 16, 2017 at 3:02
  • can you reproduce this issue in stackblitz.com ? it is way more easier to understand. I don't understand your requirement at all. At least can you provide some images showing actual result ? Commented Nov 16, 2017 at 3:30
  • Adding images now, sorry about that Commented Nov 16, 2017 at 3:32
  • Hi can you add the markup for other checkboxes as well Commented Nov 16, 2017 at 3:51

1 Answer 1

2

The issue with your code is, you are using single variable for all checkbox, and on ionChange you are changing that variable , so it will change all the checkbox.

Here it is, Change your code as below and it will work

Template side :

<tr *ngFor="let object of array">
  <td>
     <fa name="map-marker" [ngClass]="object.pinIcon ? 'unclick-pin' : 'click-pin'"></fa>
     <ion-checkbox (ionChange)="selectTwo(object, $event.checked)"></ion-checkbox>
  </td>
</tr>

Component side :

selectTwo(object, isChecked: boolean){
    object.pinIcon = isChecked;
}
Sign up to request clarification or add additional context in comments.

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.