【问题标题】:2 filters in one on reactjsreactjs上的2个过滤器合二为一
【发布时间】:2018-08-22 15:54:26
【问题描述】:

我遇到了这个麻烦,我做了 2 个不同的过滤器 一种过滤选定类别的过滤器,另一种过滤日期选择器上的日期 但我需要他们在这种情况下合二为一 1.-您只想按所选类别进行过滤 2.-您只想按日期过滤 3.- 您想按类别和日期进行筛选

我按类别过滤的方式是这个,它有效

filterData = (data) =>{
return data.filter((value,index)=>{
  if (this.state.filterSelected.indexOf(value.category)>-1) {
        return(value)
      }
})

}

烤盘看起来像这样

<Griddle
data={this.state.filterSelected.length === 0  ? data : this.filterData(data)}
pageProperties={{
  pageSize: 100,
}}
plugins={[plugins.LocalPlugin]}
components={this.state.filterOptions}

/>

我按日期过滤的方式看起来像这样,它也可以工作

 filterData = (data) =>{
  return data.filter((value,index)=>{
   if (this.state.dateSelected === value.time) {
      return(value)
    }
  })
}

烤盘看起来像这样

    <Griddle
    data={this.state.dateSelected ==='' ? data : this.filterData(data)}
    pageProperties={{
      pageSize: 100,
    }}
    plugins={[plugins.LocalPlugin]}
    components={this.state.filterOptions}
  />

它们都是分开工作的,我知道它更多的是一个逻辑问题,而不是我为过滤数据找到这个解决方案 但我不知道如何在烤盘上传递勇气,我会感谢一些帮助

  filterData = (data) =>{
    return data.filter((value,index)=>{
     if (
          (this.state.filterSelected.length>0 && 
          this.state.filterSelected.indexOf(value.category)>-1 && 
          this.state.dateSelected !='' && this.state.dateSelected(value.time)>-1)||
          (this.state.filterSelected.length==0 && 
          this.state.dateSelected !='' && this.state.dateSelected(value.time)) ||
          (this.state.filterSelected.length>0 && 
          this.state.filterSelected.indexOf(value.category) && 
          this.state.dateSelected =='')
         ){
        return(value)
      }
    })
  }

【问题讨论】:

    标签: javascript reactjs typescript react-native


    【解决方案1】:

    在您的渲染中设置一个等于所有数据的变量,然后添加两个过滤器。例如:

    render() {
      let filteredData = data;
      if (filterByDate) filteredData = this.filterByCategory(filteredData);
      if (filterByCategory) filteredData = this.filterByCategory(filteredData);
    
      return (
        <Griddle
          data={filteredData.length === 0 ? data : filteredData}
          pageProperties={{
            pageSize: 100,
          }}
          plugins={[plugins.LocalPlugin]}
          components={this.state.filterOptions}
        />
      );
    }
    

    【讨论】:

    • 我认为我不明白,我这样做了,但由于我无法在渲染上制作过滤器,所以我抛出了一个语法错误
    • @user8485597 我的示例主要是 sudo 代码。您应该使用您编写的函数并从中返回过滤后的数据。
    • 创建两个不同的过滤数据函数。一个用于日期的类别一。然后从它们返回过滤后的数据。这有意义吗?
    • 我试过你的例子,但我根本没有得到它,它仍然给我一个错误,并且害羞地一直打扰你,但我已经解决了这个问题,感谢你的时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多