A
A
Artemio952021-11-13 19:11:43
Vue.js
Artemio95, 2021-11-13 19:11:43

Is it possible to sort through computed if there are several sorts?

There is such an option for sorting , but it sorts only in one direction 1 time, when we click again, we pass the same thing then computed, we return a sorted array, depending on the flag, it changes the sort order. But there is only one flag, i.e. this is suitable if there is only one sort. How to sort through computed if you need to sort several columns of a table?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-11-13
@0xD34F Vue.js

Can.
We add a property to the component - an array of objects that will contain the column by which to sort, and the sort direction.
When the user clicks on a column heading, we check if that column is in the array. If not, add in ascending sort order. If there is also an ascending sort order, we do the sort order in descending order. There is also a sort order in descending order - delete.
Sorting itself (element comparison function): bypass the array and compare the values ​​of the corresponding properties of the passed elements; as soon as unequal ones come across, we return the result, not forgetting to multiply it by +/- 1, depending on the sort direction.
https://jsfiddle.net/yp8nhLe5/1/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question