Created
August 21, 2019 10:26
-
-
Save YuraKolesnikov/b2fad7b2344b875e106f8d28fea14adc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Реквестим данные с новым query string, записываем в переменную */ | |
actions: { | |
fetchData: async (ctx, params) => { | |
const data = await DataService.fetchData(params) | |
ctx.commit('recordData', data) | |
} | |
}, | |
mutations: { | |
recordData: (state, data) => state.data = data | |
}, | |
getters: { | |
getData: state => { | |
/* Сортируем данные */ | |
return state.sort.ascending === true | |
? state.data.sort((a, b) => | |
a[state.sort.on] > b[state.sort.on] ? 1 : -1) | |
: state.data.sort((a, b) => | |
a[state.sort.on] < b[state.sort.on] ? 1 : -1) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Так как фильтр множественный, делаем так. Есть инпуты/селекты. На keydown.enter/change ебашим filterData. | |
Функция проверяет, есть ли такой фильтр уже в массиве, если нет, добавляет, если есть - проверяет, новое значение | |
пустое или нет. Если пустое - нахрен из массива, фильтр не нужен. Если не пустое - обновить значение. */ | |
methods: { | |
async filterData({target}, id) { | |
const index = this.filters.findIndex(filter => filter.key == id) | |
const keyinFilters = this.filters[index] | |
if (keyinFilters) { | |
keyinFilters.value = target.value | |
if (keyinFilters.value == '') { | |
this.filters.splice(index, 1) | |
} | |
} else { | |
this.filters.push({ | |
key: id, | |
value: target.value | |
}) | |
} | |
const query = new URLSearchParams() | |
this.filters.forEach(filter => query.append(`filter[${filter.key}]`, filter.value)) | |
this.$store.dispatch('fetchData', query) | |
} | |
}, | |
computed: { | |
...mapGetters({ | |
data: 'getData' | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment