Commit 2544c632 authored by esatakpunar's avatar esatakpunar

pagination server side

parent f3863459
...@@ -17,17 +17,37 @@ export default { ...@@ -17,17 +17,37 @@ export default {
"productList", "productList",
"filteredProductList", "filteredProductList",
"singleExpand", "singleExpand",
"expanded", "getExpanded",
"dialog", "dialog",
"loading", "loading",
"editedItem", "editedItem",
"editedIndex", "editedIndex",
"getOptions",
"totalProducts",
]), ]),
indexofColumn() { indexofColumn() {
return this.dialogHeaders.findIndex( return this.dialogHeaders.findIndex(
(header) => this.column == header.value (header) => this.column == header.value
); );
}, },
options: {
get() {
return this.getOptions;
},
set(data) {
this.setOptions(data);
},
},
expanded: {
get() {
return this.getExpanded;
},
set(data) {
this.setExpanded(data);
},
},
}, },
methods: { methods: {
...mapActions("products", [ ...mapActions("products", [
...@@ -38,7 +58,8 @@ export default { ...@@ -38,7 +58,8 @@ export default {
"deleteMultiProduct", "deleteMultiProduct",
"saveProduct", "saveProduct",
"editProduct", "editProduct",
"filterProduct", "setExpanded",
"setOptions",
]), ]),
}, },
created() { created() {
...@@ -57,6 +78,8 @@ export default { ...@@ -57,6 +78,8 @@ export default {
:headers="headers" :headers="headers"
:items="productList" :items="productList"
:loading="loading" :loading="loading"
:options.sync="options"
:server-items-length="Number(totalProducts)"
:single-expand="singleExpand" :single-expand="singleExpand"
:expanded.sync="expanded" :expanded.sync="expanded"
show-select show-select
......
import axios from "axios"; import axios from "axios";
export default { export default {
fetchProductData({ dispatch, commit }, data = {}) { fetchProductData({ commit, getters }, data = {}) {
let query; let query;
if (data.filterColumn == "all") { if (data.filterColumn == "all") {
query = "q=" + data.searchInput; query = "q=" + data.searchInput;
} else if (data.filterColumn) { } else if (data.filterColumn) {
query = data.filterColumn + "_like=" + data.searchInput; query = data.filterColumn + "_like=" + data.searchInput;
} }
let pageLimit = `&_page=${getters.getOptions.page}&_limit=${getters.getOptions.itemsPerPage}`;
let sort = `&_sort=${getters.getOptions.sortBy}&_order=${getters.getOptions.sortDesc}`;
commit("setLoading", true); commit("setLoading", true);
axios axios
.get(`http://localhost:3000/products?${query}`) .get(`http://localhost:3000/products?${query}${pageLimit}${sort}`)
.then((response) => { .then((response) => {
commit("setTotalProducts", response.headers["x-total-count"]);
commit("setProductList", response.data); commit("setProductList", response.data);
dispatch("filterProduct", { searchInput: "" });
commit("setLoading", false); commit("setLoading", false);
}) })
.catch((e) => { .catch((e) => {
...@@ -25,6 +29,14 @@ export default { ...@@ -25,6 +29,14 @@ export default {
commit("setSingleExpand"); commit("setSingleExpand");
}, },
setOptions({ commit, dispatch }, data) {
commit("setOptions", data);
dispatch("fetchProductData");
},
setExpanded({ commit }, data) {
commit("setExpanded", data);
},
closeDialog({ commit }) { closeDialog({ commit }) {
commit("setDialog", false); commit("setDialog", false);
}, },
...@@ -81,20 +93,4 @@ export default { ...@@ -81,20 +93,4 @@ export default {
commit("setEditedItem", Object.assign({}, item)); commit("setEditedItem", Object.assign({}, item));
commit("setDialog", true); commit("setDialog", true);
}, },
filterProduct({ getters, commit }, data) {
let x;
if (!data.searchInput) {
x = getters.productList;
} else {
x = getters.productList.filter((product) => {
if (product[data.filterColumn]) {
return product[data.filterColumn]
.toLowerCase()
.includes(data.searchInput.toLowerCase());
}
});
}
commit("setfilteredProductList", x);
},
}; };
export default { export default {
headers() { headers() {
return [ return [
{ text: "Description", value: "data-table-expand" },
{ {
text: "Product Name", text: "Product Name",
align: "left", align: "left",
...@@ -14,6 +13,7 @@ export default { ...@@ -14,6 +13,7 @@ export default {
{ text: "Color", value: "color", type: "text" }, { text: "Color", value: "color", type: "text" },
{ text: "Price", value: "price", type: "text" }, { text: "Price", value: "price", type: "text" },
{ text: "Image", value: "image", type: "text" }, { text: "Image", value: "image", type: "text" },
{ text: "Description", value: "data-table-expand" },
{ text: "Actions", value: "actions", sortable: false }, { text: "Actions", value: "actions", sortable: false },
]; ];
}, },
...@@ -41,7 +41,7 @@ export default { ...@@ -41,7 +41,7 @@ export default {
singleExpand(state) { singleExpand(state) {
return state.singleExpand; return state.singleExpand;
}, },
expanded(state) { getExpanded(state) {
return state.expanded; return state.expanded;
}, },
editedIndex(state) { editedIndex(state) {
...@@ -59,4 +59,10 @@ export default { ...@@ -59,4 +59,10 @@ export default {
filteredProductList(state) { filteredProductList(state) {
return state.filteredProductList; return state.filteredProductList;
}, },
getOptions(state) {
return state.options;
},
totalProducts(state) {
return state.totalProducts;
},
}; };
...@@ -5,12 +5,14 @@ export default { ...@@ -5,12 +5,14 @@ export default {
setSingleExpand(state) { setSingleExpand(state) {
state.singleExpand = !state.singleExpand; state.singleExpand = !state.singleExpand;
}, },
setExpanded(state) {
return state.expanded;
},
setDialog(state, value) { setDialog(state, value) {
state.dialog = value; state.dialog = value;
}, },
setLoading(state, value) { setLoading(state, value) {
state.loading = value; state.loading = value;
console.log(value);
}, },
setEditedIndex(state, index) { setEditedIndex(state, index) {
state.editedIndex = index; state.editedIndex = index;
...@@ -21,4 +23,10 @@ export default { ...@@ -21,4 +23,10 @@ export default {
setfilteredProductList(state, data) { setfilteredProductList(state, data) {
state.filteredProductList = data; state.filteredProductList = data;
}, },
setOptions(state, data) {
state.options = data;
},
setTotalProducts(state, data) {
state.totalProducts = data;
},
}; };
...@@ -7,4 +7,6 @@ export default () => ({ ...@@ -7,4 +7,6 @@ export default () => ({
editedItem: {}, editedItem: {},
dialog: false, dialog: false,
loading: false, loading: false,
options: {},
totalProducts: 0,
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment