Answer the question
In order to leave comments, you need to log in
How should dynamic filters work on the frontend?
Perhaps I misunderstand something and do it wrong, I would love to read on this topic.
I understand that if I need data on the front, I make a request to the server, and if I need to filter the data, then through the parameters I make a new request to the server, and it, in turn, takes data from the database by selection.
Below is the code that returns the data and if you specify the color parameter, it filters the data
router.get('/products', function (req, res, next) {
const {color} = req.query;
const query = {};
if (color) {
query.color = color;
}
Products.find(query).then(function (products) {
res.send(products);
})
});
state: {
products: [],
},
mutations: {
SET_PRODUCTS(state, products) {
state.products = products
},
},
actions: {
loadFilterProduct({commit}, [p1,p2]) {
const url = 'http://localhost:5000/api/products';
let options = {
params: {
color: p1,
compositions: p2
},
'paramsSerializer': function(params) {
return qs.stringify(params, {arrayFormat: 'repeat'})
},
};
axios
.get(url, options)
.then(r => r.data)
.then(products => {
commit('SET_PRODUCTS', products)
})
}
}
data() {
return {
checkedColors: [],
checkedCompositions: []
}
},
methods: {
search(){
this.$store.dispatch('loadFilterProduct', [this.checkedColors, this.checkedCompositions])
}
},
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question