I'm having trouble figuring out how to change the vuetify pagination.page property back to 1, when a user changes the rowsPerPage property.
Say there are a total of 23 rows in a result set and rowsPerPage is currently set to 10. If the users goes to the 3rd (last) page and then selectes 50 rowsPerPage, vue calls my ajax query to get new data from the backend server, but it passes rowsPerPage as 50 and it passes page as 3.
Since this causes the sql offset property to be 100, which is way more than the 23 records in the table, it returns no data and so the screen re-renders with no records.
What I would like to do to fix this is, when the rowsPerPage property changes, reset the page property back to 1.
I have googled a bunch but cannot find the answer. Am I trying to solve this problem the wrong way?
Edit: Here is a sample of my rails view code:
<v-card flat>
<v-card-title class="pt-0 pb-0">
<h2>No RSP/Participating Apps</h2>
<%= render :partial => "search" %>
<%= render :partial => "rows_per_page" %>
</v-card-title>
<v-data-table
:headers="headers"
:items="results"
:pagination.sync="pagination"
hide-actions
:total-items="totalItems"
:must-sort=true
:search="pagination.search"
>
...
</v-data-table>
<div class="text-xs-center pt-2">
<v-pagination v-model="pagination.page" :length="pages"></v-pagination>
</div>
</v-card>
And my js code:
data: {
search: '',
drawer: null,
miniVariant: false,
loading: true,
totalItems: 0,
results: [],
pagination: {
rowsPerPage: 10,
},
rowsPerPageChoices: [
{ text: '2 rows per page', value: 2 },
{ text: '5 rows per page', value: 5 },
{ text: '10 rows per page', value: 10 },
{ text: '20 rows per page', value: 20 },
{ text: '30 rows per page', value: 30 }
],
},
methods: {
commonQueryParams() {
return '?sortBy=' + this.pagination.sortBy +
'&descending=' + this.pagination.descending +
'&page=' + this.pagination.page +
'&rowsPerPage=' + this.pagination.rowsPerPage +
'&onlyTotal=0' +
'&filter=' + this.search;
},
queryParams() {
return this.commonQueryParams();
},
getData() {
this.loading = true;
axios.get(this.dataApiUrl + '/' + this.dataEndPoint + this.queryParams(), {withCredentials: true})
.then(response => {
this.results = response.data.data;
this.totalItems = response.data.control_data.total;
this.loading = false;
});
},
},
computed: {
pages () {
return this.pagination.rowsPerPage ? Math.ceil(this.totalItems / this.pagination.rowsPerPage) : 0;
}
},
watch: {
pagination: {
handler () {
this.getData();
},
deep: true
},
search: _.debounce(function () {
this.getData()
}, 500),
}