我正在使用反应表和服务器端过滤。 问题是,当进行过滤时,过滤器不会一起应用。例如,如果我按名称和按类别进行过滤,则名称过滤器将重置。
网址看起来像这样:
http://localhost:3001/api/v1/products?pages=0&pageSize=20&filtered[]=%7B%22id%22:%22name%22,%22value%22:%22j%22%7D&filtered[]=%7B%22id%22:%22comment%22,%22value%22:%22f%22%7D".
我不知道每次尝试按不同的列进行过滤时都调用filter[]=
是否正常,或者服务器端是否存在问题。
这是控制器:
exports.index = function (req,res) {
let filtered = {}
let filters = {}
if (req.query.filtered) {
req.query.filtered.map(result => {
filtered = JSON.parse(result)
})
let id = filtered.id
let value = filtered.value
if (filtered['id'] === 'name' || filtered['id'] === 'comment') {
filters[id] = { '$regex': value,'$options': 'i' }
} else {
filters[id] = value
}
}
Product.listWithCategory(filters).then(response => {
res.json({ result: response })
})
}
对onFetchData进行反应:
onFetchData = (state,instance) => {
const jwt = getJwt()
if (!jwt) {
this.props.history.push('/login')
}
console.log(state.filtered)
let config = {
headers: { 'Authorization': `Bearer ${jwt}` },params: {
pages: state.page,pageSize: state.pageSize,sorted: state.sorted,filtered: state.filtered
}
}
this.setState({ loading: true })
axios.get('http://localhost:3001/api/v1/products',config)
.then(response => {
this.setState({
data: response.data.result,pages: response.data.pages,loading: false
})
})
}