To implement filters with infinite pagination in Inertia, you can follow these steps:
- Update the
Index.vuefile to include the filter component and pass theparams.filters.searchvalue as a prop to the filter component:
<template>
<!-- ... -->
<section class="hidden 2xl:block 2xl:w-full">
<Filters v-model="params.filters.search" />
</section>
<!-- ... -->
</template>
- Modify the
Filters.vuecomponent to emit an event whenever the filter value changes:
<template>
<input type="text" v-model="search" @input="updateFilter" />
</template>
<script>
export default {
props: ['value'],
emits: ['update:modelValue'],
data() {
return {
search: this.value,
}
},
methods: {
updateFilter() {
this.$emit('update:modelValue', this.search)
},
},
}
</script>
- Update the
Index.vuefile to watch for changes in the filter value and trigger a refetch of the data:
<script setup>
// ...
watch(params, () => refetch(), { deep: true })
</script>
- Modify the
useInfiniteScrollcomposable to include the filter value in the request:
export function useInfiniteScroll(propName, landmark = null) {
// ...
const loadMoreItems = () => {
if (!canLoadMoreItems.value) {
return
}
router.get(
value().next_page_url,
{
filters: {
search: params.value.filters.search,
},
},
{
preserveState: true,
preserveScroll: true,
onSuccess: () => {
window.history.replaceState({}, '', initialUrl)
items.value = [...items.value, ...value().data]
},
},
)
}
// ...
}
With these changes, the filter value will be passed to the back-end on each request, allowing you to filter the records accordingly.