VDataTable server-side itemsPerPage change without automatic page reset
0 reputation · 03 Sept 2024, 16:41 UTC
0 reputation · 03 Sept 2024, 16:41 UTC
VDataTable in server-side mode exposes page, itemsPerPage and sort as controllable props and emits update:page, update:items-per-page and update:sort.
The goal is to keep the table from requesting a page beyond the new total when itemsPerPage changes while the user is on a later page. The component does not provide a built-in constraint to prevent an out-of-range page request or to clamp itemsPerPage to server-allowed values.
With Vuetify as a presentation layer, state management remains in the application, and version differences between Vuetify 2 DataTable and Vuetify 3 VDataTable affect prop names and serverItems behavior.
Is there a documented option to auto-reset page to 1 on itemsPerPage change? What is the intended permission boundary for itemsPerPage values between the app and the server? Should the application clamp the page to the last valid page or reset to 1?
32275 reputation · 03 Sept 2024, 20:17 UTC
Vuetify’s VDataTable does not provide a built‑in option to automatically reset the page when itemsPerPage changes. The component watches the prop and, when the watcher fires, calls its internal paginate() method which sets pagination.page to 1. If the watcher does not fire (e.g., because the prop value is unchanged by reference or you are using a .sync binding that bypasses the watch), the current page number is retained.
itemsPerPage, page and sort as controllable props and emits the corresponding update:* events.itemsPerPage triggers a pagination reset in Vuetify 2.x and the default VDataTable implementation.!==), so mutating the same numeric value does not fire the watcher and the page stays unchanged.itemsPerPage to a local data property so you control when it changes: :items-per-page="localItemsPerPage"Vue.nextTick to read the server’s total count and adjust the page: this.$nextTick(() => {
const lastPage = Math.max(1, Math.ceil(this.total / this.localItemsPerPage));
if (this.pagination.page > lastPage) {
this.pagination.page = lastPage;
}
});To confirm whether the watcher behaves as described, please specify the exact Vuetify/VDataTable version you are using (e.g., Vuetify 2.x with v‑data‑table, Vuetify 3.x VDataTable, or a community wrapper). The version determines whether the watcher fires on value changes or only on reference changes, which influences whether you need the manual nextTick clamp.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.