Using this component a list with millions of items can be rendered without a drop in performance. It does that by “lazily” loading elements into the DOM when they come into view and removing items from the DOM when they are not visible.
In mobile apps it is often necessary to display very large lists of items. One problem with this is that a large number of DOM elements must be created which can affect performance.
Onsen UI provides an element called VOnsLazyRepeat
which helps rendering large numbers of items. It will automatically calculate what elements are visible and only render those. When the user scrolls, it will remove items that are outside the screen and add elements that become visible dynamically.
Notice that for a simple “load more items” functionality, it is recommended to use VOnsPage
‘s infiniteScroll
prop instead.
VOnsLazyRepeat
must we used inside the container component that will held all the items. Frequently it is used together with VOnsList
:
<v-ons-list>
<v-ons-lazy-repeat ...props></v-ons-lazy-repeat>
</v-ons-list>
The items will be rendered as siblings of VOnsLazyRepeat
.
VOnsLazyRepeat
needs at least two props: length
and renderItem
. The first one is just a number that indicates the maximum amount of items that could be virtually displayed. The second one must be a generator function like the following:
renderItem(index) {
return {
template: `<v-ons-list-item>...</v-ons-list-item>`;
// Set item data using 'index'
};
}
The requirement is that it must return an object describing a Vue component to be attached in the parent list. The generator function gets the index of the item in the list (absolute index, not the index of the visible list) that can be used to initialize the item’s data.
VOnsLazyRepeat
can dynamically calculate the height of every item once they are rendered, but in order to know how many items it should render beforehand we must provide a hint. By default, VOnsLazyRepeat
pre-renders the very first item and takes its height to do the calculations.
However, it is also possible to pass an optional calculateItemHeight
function prop that gets the element index and returns its approximate height. This can enhance the calculations and allow better scrolling.
calculateItemHeight(index) {
return myItems[index].image.height;
}
Name | Description |
---|---|
refresh |
VOnsLazyRepeat listens for refresh events in the parent context in order to trigger a refresh when the items change. E.g. $emit('refresh') . It is also possible to call $refs.lazyRepeat.refresh() instead.
|
VOnsLazyRepeat
listens for refresh
events in the parent context in order to trigger a refresh when the items change. E.g. $emit('refresh')
. It is also possible to call $refs.lazyRepeat.refresh()
instead.
Name | Type | Description |
---|
If you have any questions, use our Community Forum or talk to us on Discord chat. The Onsen UI team and your peers in the community will work together to help solve your issues.
For bug reports and feature requests use our GitHub Issues page.