首页 > 解决方案 > 反应表。如何进行服务器端过滤。请求参数

问题描述

我正在使用反应表和服务器端过滤。问题是过滤时过滤器不能一起应用。例如,如果我按名称和按类别过滤,则名称过滤器将重置。

网址如下所示:

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

标签: node.jsreactjsmongoosereact-table

解决方案


推荐阅读