Created
February 13, 2020 09:46
-
-
Save Juhlinus/cd9a0bde82705767c9e9cdf3be176e1c to your computer and use it in GitHub Desktop.
Infinite Scroll in InertiaJS + Vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<ul> | |
<li v-for="product in products_data" class="py-6"> | |
<inertia-link :href="'products/' + product.id">{{ product.name }}</inertia-link> | |
</li> | |
</ul> | |
</div> | |
</template> | |
<script> | |
import Layout from '@/Shared/Layout' | |
export default { | |
layout: Layout, | |
data() { | |
return { | |
products_data: this.products.data, | |
} | |
}, | |
props: { | |
products: Object, | |
}, | |
methods: { | |
scroll: function () { | |
const self = this; | |
window.onscroll = function(ev) { | |
if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) { | |
const product_links = self.products.links; | |
const next = product_links[product_links.length - 1]; | |
if (next.url) { | |
self.$inertia.visit(next.url, { | |
preserveScroll: true, | |
preserveState: true, | |
}); | |
self.products_data.push(...self.products.data); | |
} | |
} | |
}; | |
} | |
}, | |
mounted () { | |
this.scroll(); | |
} | |
} | |
</script> |
this link is also useful. it has inertia URL change problem solution.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem on that solution is that the first page disappears when the 2nd page is loaded. So it's not possible to scroll back or refresh the page. I've seen some solutions using axios in the www.