I'm using Ionic 3 and trying to make a simple 5-star rating element. A cool feature I'd like to implement is sliding/panning to change the rating, similar to how an ion-range works but with no bar or knob, just the stars lighting up as the user slides over them.
I tried using the pan gesture and passing $event as a parameter, and checking the id of the element in the target field of the $event, like
$event.target.id
and assigned each star a corresponding id from 1 through 5.
Testing in the browser it worked just fine, but not in my phone, I checked the whole $event.target object and it is filled with undefined values :( it only changes when the first pan event is fired, all subsequent pans before I release the touch stop working.
Is this a bug? Or maybe is there any workaround for this?
Additional info:
HTML (without style related attributes):
<ion-grid (pan)="ratingChanged($event)">
<ion-row>
<ion-col *ngFor="let i of [1,2,3,4,5]">
<ion-icon [name]="(rating >= i)?'star':'star-outline'" [id]="i"></ion-icon>
</ion-col>
</ion-row>
</ion-grid>
TS:
ratingChanged(ev) {
if (ev && ev.target && ev.target.id)
this.rating = +ev.target.id; //id comes as a string
}
EDIT: @Sergey Rudenko's suggestion solved the problem, here's how I implemented it:
HTML:
<ion-grid (pan)="ratingChanged($event)">
<ion-row>
<ion-col *ngFor="let i of [1,2,3,4,5]">
<ion-icon [name]="(rating >= i)?'star':'star-outline'" [id]="'star' + i"></ion-icon>
</ion-col>
</ion-row>
</ion-grid>
TS:
ratingChanged($ev) {
let id: string = document.elementFromPoint($ev.center.x, $ev.center.y).id;
if (id.match(/star\d/))
this.rating = +id.match(/star(\d)/)[1];
}