[英]chaining multiple filters in vuex getters
I have a building list.我有一个建筑清单。 I am able to fetch the building data into component by storing it into building list state.我可以通过将建筑数据存储到建筑列表 state 中来将建筑数据提取到组件中。
There are multiple dropdownlist as a different filters(eg: status, state, country etc).有多个下拉列表作为不同的过滤器(例如:状态、state、国家等)。 I want to chain up all the filters and getting filtered data.我想链接所有过滤器并获取过滤后的数据。
Here is the faker data regarding filter structure这是有关过滤器结构的伪造数据
const buildingFilters = {
fields: [
{
field: 'id',
title: 'Id',
},
{
field: 'name',
title: 'Name',
},
{
field: 'type',
title: 'Type'
},
{
field: 'status',
title: 'Status'
},
{
field: 'city',
title: 'City'
},
{
field: 'state',
title: 'State'
},
{
field: 'country',
title: 'Country'
},
{
field: 'reporting Zone',
type: 'Reporting Zone'
}
],
filtersType: {
country: {
field: 'country',
listofValues: ['USA', 'CANADA']
},
state: {
field: 'state',
listofValues: ['Maryland', 'New Jersey']
},
city: {
field: 'city',
listofValues: ['Bethesda', 'Monmoth Junction']
},
reporting_zone: {
field: 'reporting_zone',
listofValues: ['RZ1', 'RZ2']
},
status: {
field: 'status',
listofValues: ['Draft', 'Inprogress']
},
type: {
field: 'type',
listofValues: ['Type 1', 'Type 2']
}
}
Now, i am calling this by service to load the initial filters into component.现在,我通过服务调用它以将初始过滤器加载到组件中。 Now,i have to configure it into store.现在,我必须将其配置到商店中。
const defaultState = {
bldgList: emptyArray,
bldgFilter: emptyArray,
filteredData: [],
filterTypes: {
filterByStatus: '',
filterByCountry: '',
filterByState: ''
}
};
const getters = {
filteredData: state => {
return state.filteredData = state.bldgList.slice()
if(state.filterTypes.filterByStatus !== '') {
// not understand how will i do that
}
}
};
const mutations = {
FILTER_FIELD_CHANGE(state,payload) {
// here we need to update Filters change
}
},
const actions = {
async filteredData({commit}, filterTypes) {
commit('FILTER_FIELD_CHANGE', filterTypes)
}
}
This store is not correct i know but how to filter building list based on multiple different filters and chaining filters based on condition.我知道这家商店不正确,但如何根据多个不同的过滤器过滤建筑列表并根据条件链接过滤器。
The getter
could call Array.prototype.filter()
on the input based on the given filters, one by one. getter
可以根据给定的过滤器一一调用输入上的Array.prototype.filter()
。 The following example assumes filterByStatus
, filterByCountry
and filterByState
each contain the search term.以下示例假设filterByStatus
、 filterByCountry
和filterByState
都包含搜索词。
const getters = {
filteredData: state => {
let bldgs = state.bldgList
if (state.filterTypes.filterByStatus) {
bldgs = bldgs.filter(b => b.status.includes(state.filterTypes.filterByStatus))
}
if (state.filterTypes.filterByCountry) {
bldgs = bldgs.filter(b => b.country === state.filterTypes.filterByCountry)
}
if (state.filterTypes.filterByState) {
bldgs = bldgs.filter(b => b.state === state.filterTypes.filterByState)
}
return bldgs
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.