I fetch and do pagination on the data, no problem with that. But when I try to filter the data, the pagination not working with the filtered data. Still displaying next pages at bottom. And when I go(for example) page 2 there is no data in page two. Because I filtered it already. How can I fix this problem? Thank you!
Index Component
Data(){
meta_data: {
last_page: null,
current_page: 1,
prev_page_url: null
}
},
methods: {
fetchEstates(page = 1){
axios.get('/ajax', {
params: {
page
}}).then((response) => {
// console.log(response);
this.estates = response.data.data;
this.insertMarkers();
this.meta_data.last_page = response.data.last_page;
this.meta_data.current_page = response.data.current_page;
this.meta_data.prev_page_url = response.data.prev_page_url;
});
},
},
computed: {
one: function () {
let filteredStates = this.estates.filter((estate) => {
return (this.keyword.length === 0 || estate.address.includes(this.keyword)) &&
(this.rooms.length === 0 || this.rooms.includes(estate.rooms)) &&
(this.regions.length === 0 || this.regions.includes(estate.region))});
if(this.sortType == 'price') {
filteredStates = filteredStates.sort((prev, curr) => prev.price - curr.price);
}
if(this.sortType == 'created_at') {
filteredStates = filteredStates.sort((prev, curr) => Date.parse(curr.created_at) - Date.parse(prev.created_at));
}
filteredStates = filteredStates.filter((estate) => { return estate.price <= this.slider.value});
filteredStates = filteredStates.filter((estate) => { return estate.extend <= this.sliderX.value});
filteredStates = filteredStates.filter((estate) => { return estate.m2_price <= this.sliderT.value});
return filteredStates;
},
}
<pagination
:meta_data="meta_data"
v-on:next="fetchEstates">
</pagination>
Pagination Component
props: ['meta_data'],
methods: {
next(page) {
this.$emit('next', page);
}
}
}
<nav>
<ul class="pagination pagination-lg">
<li class="page-item"
:class="{ 'disabled': meta_data.prev_page_url === null }">
<a href="#"
class="page-link"
@click="next(meta_data.current_page-1)">
«
</a>
</li>
<li class="page-item"
v-for="page in meta_data.last_page"
:key="page"
:class="{ 'active':meta_data.current_page === page }">
<a href="#"
@click.prevent="next(page)"
class="page-link">
{{ page }}
</a>
</li>
<li class="page-item"
:class="{ 'disabled': meta_data.current_page === meta_data.last_page }">
<a href="#"
class="page-link"
@click="next(meta_data.current_page+1)">
»
</a>
</li>
</ul>
</nav>
Hello I'm not sure you are going about this in the best way. I have recently done something similar. Below is my simplified code:
HTML:
VUE:
You are doing it wrong you should send the data to backend/sql then filter them there. so you get the right
last_page
etc.Anyway if you still want to filter them as its is, you will need to calculate the pages agen.
Here is a sample, so you could calculate how many pages in
filteredStates
and generate pages, you may already able to generate them but the calculation could help you.