N
N
Nazi2018-10-09 11:35:48
JavaScript
Nazi, 2018-10-09 11:35:48

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);
    })
});

And now a question about requests to the server through vuex.
normal practice so use to filter data?
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)
                })
        }
    }

In the component, I get value from the checkbox via v.-modal and it filters the data, but I'm sure that I'm moving in the wrong direction
data() {
            return {
                checkedColors: [],
                checkedCompositions: []
            }
        },
        methods: {
            search(){
                this.$store.dispatch('loadFilterProduct', [this.checkedColors, this.checkedCompositions])
            }
        },

There will be a lot
of filters Can someone write how the list should work correctly.
and I'll be googling

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Batukhtin, 2018-10-09
@0lorin

Perhaps you can pass paramsinstead of [p1,p2].

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question