I want to scrollTo a particular item in listview in ionic 2. I have a listview which is bound to array.
export class TestPage {
@ViewChild(Content) content: Content;
public items: Array<Object> = [];
ionViewWillEnter() {
console.log(this.navParams.data);
this.customService.getArray(this.params.index).then((items) => {
this.items = items;
//scroll to item
// this.content.scrollTo()
});
}
}
Here is the view:
<ion-list [virtualScroll]="items" approxItemHeight="120px" approxItemWidth="100%"
class="items-listview list list-md">
<button class="items-listview-item item item-md" *virtualItem="let item">
<div class="items-listview-text">
{{ item.text }}<span>{{item.index}}</span>
</div>
</button>
</ion-list>
I see that scrollTo only support position i.e top and left but not the element itself. How can i scrollTo listview item (e. item no 150) itself ? How can i get the position of item no 150 and pass it to scrollTo?
I am posting the solution i come up with. First you need to give unique id to each listview item and then select the
ListView
After that i created a function (following)
ScrollTo
which has a timeout of resizing the listview after scrolling too (as i was changing buffer ratio dynamically).Last i just called this function after a second delay as i waited till listview loads: