简体   繁体   English

将获取数据过滤到复选框 react.js

[英]Filter fetch data to checkboxes react.js

Maybe someone has an idea how to filter the data in the checkboxes.也许有人知道如何过滤复选框中的数据。 I already have a tag filter, but I don't know how to transform it to additionally filter my prices.我已经有一个标签过滤器,但我不知道如何将它转换为额外过滤我的价格。
I need to filter by prices, eg I have a price of $ 9.99, $ 24.49 and $ 19.98 $ 20.00 - I need to make a filter:我需要按价格过滤,例如我的价格为 9.99 美元、24.49 美元和 19.98 美元 20.00 美元 - 我需要制作一个过滤器:
"<$ 10", "$ 10- $ 20", "> $ 20" "<$ 10"、"$ 10- $ 20"、"> $ 20"

DEMO here: https://codesandbox.io/s/blissful-sound-b3vm3?file=/src/App.js演示在这里: https : //codesandbox.io/s/blissful-sound-b3vm3? file =/ src/ App.js

data fetch from data.json从 data.json 获取数据


  function search(data) {
    return data.filter(
      (item) =>
        (filterParam === 'All' || item.tags.includes(filterParam)) &&
        (searchParam.length === 0 ||
          (searchParam.every((tag) => item.tags.includes(tag)) &&
            JSON.stringify(item).toLowerCase().indexOf(q.toLowerCase()) > -1))
    )
  }

Add a second filter and state object, and filter by that new param.添加第二个过滤器和状态对象,并通过该新参数进行过滤。

const [priceFilterParam, setPriceFilterParam] = useState("All");

function search(data) {
  return data.filter(
    ...
  ).filter(item => {
    const price = parseFloat(item.price.substring(1));
    return (priceFilterParam === "All") ||
      (priceFilterParam === "Low" && price <= 10) || 
      (priceFilterParam === "Medium" && price > 10 && price <= 20) ||
      (priceFilterParam === "High" && price > 20)
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM