4

When I click on the label of an ion-item the checkbox is triggered. I want to find a way of preventing this from happening as I want to trigger another function when clicking the label.

I found this answer for Ionic 3: https://forum.ionicframework.com/t/solved-can-i-disable-a-checkbox-from-activating-when-clicking-on-a-label/95120 However, it is not working for Ionic 4.

<ion-item>
    <ion-icon [name]="setIconDoc(item.document.documentType)" color="primary" (click)="editDocument(item.document)"></ion-icon>
        <ion-label padding-start color="none" (click)="editDocument(item.document)"> {{ item.document.customer.name }}
        </ion-label>
        <ion-checkbox slot="end" color="success" [(ngModel)]="item.isChecked"> 
   </ion-checkbox>
</ion-item>

I'd like to have two behaviors: - When clicking in the Checkbox trigger just the checkbox. - When clicking on the label or the icon open a modal to edit my document.

3 Answers3

3

I just had a similar problem and found a nice solution for it in ionic 4 by using the slots of ion-item.

<ion-item lines="full">
  <ion-icon slot="start" name="at" (click)="iconClicked()"></ion-icon>

  <ion-label slot="start" (click)="labelClicked()">
    This is a separately clickable label
  </ion-label>
  <ion-checkbox slot="end"></ion-checkbox>
</ion-item>

Explanation

  • The elements in the start slot of the ion-item are not triggered when clicking the checkbox.
  • The start slot has no bottom border by default so it must be set by adding lines="full" to the ion-item;

Be aware, that the main slot still is rendered with a large width. That may lead to some hidden content. In this case this can be fixed with a css tweak like this.

ion-item ion-label {
  overflow: visible;
}
Dave Gööck
  • 395
  • 1
  • 15
2

I found another solution. I added another hidden checkbox in the item.

<ion-item *ngFor="let task of tasks;let i of index;"  padding margin>
  <ion-checkbox slot="start" color="success" (click)="DeleteTask($event, task)"></ion-checkbox>
  
  <!-- another checkbox otherwise item clicks triggers checkbox click -->
  <ion-checkbox hidden=true ></ion-checkbox>

<ion-label >
     {{task.Name}}
  </ion-label>
  <ion-reorder slot="end"></ion-reorder>
  • I like this option because it allows you to still have a click handler for the whole ion-item, as opposed to having it on the label. That way a click anywhere else on the ion-item besides the checkbox still activates the click event, rather than having to click specifically in the label text. – fenix.shadow Jun 09 '22 at 20:57
0

You can also wrap the checkbox in a slotted div, which appears to break the link between the item and the checkbox.

<ion-item (click)="itemHandler()">
  <ion-icon />
  <ion-label>Label</ion-label>
  <ion-checkbox (click)="checkboxHandler()" />
</ion-item>

If you put the click handler on the ion-item then it will handle clicks from anywhere on the ion-item. However, that includes clicks from the checkbox, so you have to make sure to also call event.stopPropagation() in the checkbox click handler.

fenix.shadow
  • 402
  • 4
  • 9