简体   繁体   English

如何使用 react 分别实现/渲染两个过滤器

[英]How to implement / render two filters separately using react

How can i implement these two filter with postRes in table row?如何在表格行中使用postRes实现这两个过滤器? postRes is json api response. postRes 是 json api 响应。 includeKeyword & searchVolume is State hook. includeKeyword & searchVolume 是 State 钩子。 i don't want to implement filters if states are empty.如果状态为空,我不想实现过滤器。 i appreciate your help in advance我提前感谢您的帮助

  let includeKeywordResults = !includeKeyword  ? postRes : postRes.filter(data =>
    data.text.toLowerCase().includes(includeKeyword.toLocaleLowerCase())) 
  

 let searchVolumeResults = !searchVolume ? postRes : postRes.filter(data => 
  data.keyword_idea_metrics.avg_monthly_searches > parseInt(searchVolume) )

Return返回

        <div className="container table-responsive py-5"> 
      <table className="table table-bordered table-hover" id="table-to-xls">
        <thead className="text-white bg-dark">
          <tr>
            <th scope="col">Keyword</th>
            <th scope="col">Search Volume</th>
            <th scope="col">Competition</th>
            <th scope="col">CPC</th>
          </tr>
        </thead>
        <tbody>
    {includeKeywordResults !== undefined ? includeKeywordResults.map((data, index) => (
          <tr key={index}>
            <td>{data.text}</td>
            <td>{data.keyword_idea_metrics.avg_monthly_searches}</td>
            <td>{data.keyword_idea_metrics.competition}</td>
            <td>{data.keyword_idea_metrics.low_top_of_page_bid_micros}</td>
          </tr>
  )) : null}
  
  </tbody>
      </table>
      </div>
      </div>
    }

Create a filter function with your various criteria and pass it to the Array.filter prototype.使用您的各种标准创建一个过滤器 function 并将其传递给 Array.filter 原型。 Function will look like something like that (not tested): Function 看起来像这样(未经测试):

const filterFunction = (a) => {
   if (!includeKeyword && !searchVolume){
      return true
   } else if (includeKeyword && !searchVolume){
      return a.text.toLowerCase().includes(includeKeyword.toLocaleLowerCase())) 
   } else if (!includeKeyword && searchVolume){
      return  a.keyword_idea_metrics.avg_monthly_searches > parseInt(searchVolume)
   } else {
      a.text.toLowerCase().includes(includeKeyword.toLocaleLowerCase())) && a.keyword_idea_metrics.avg_monthly_searches > parseInt(searchVolume) 
   }
}

and pass it before your map:并在您的 map 之前传递它:

includeKeywordResults.filter(filterFunction).map((data, index) => (

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

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