【问题标题】:Search doesn't Filter data搜索不过滤数据
【发布时间】:2020-04-01 12:26:12
【问题描述】:

我有以下代码从列表中搜索数据。我使用了以下代码,但它不过滤记录。这是codepen [链接][1]。

【问题讨论】:

  • 您想在单击按钮时搜索还是在键入时搜索?
  • 嗨@JuniusL。是的。我还想将搜索移动到另一个组件search_page.js > 导致index.js 页面 > 然后Detail.js

标签: twitter-bootstrap


【解决方案1】:

您需要两个数组来进行搜索,第一个保存原始数据,第二个保存过滤后的数据。

constructor(props) {
  super(props);
  this.state = {
    page: 2,
    itemsPerPage: 10,
    data: cardData.data.Table,
    filteredData: cardData.data.Table
  };
  this.items = createItems(100);
}

搜索过滤功能

onSearchTextChange = searchText => {
  const newData = this.state.data.filter(
    item =>
      typeof item.first_name === "string" &&
      item.first_name.toLowerCase().includes(searchText.toLowerCase())
  );

  this.setState({
    filteredData: newData
  });
};

显示来自filteredData的值

<List divided>
  {this.state.filteredData.map(results => (
    <div className="col-sm-3">
      <div className="card our-team" id="employeeInfo">
        <div className="card-body">
          <img
            class="pic"
            src={`data:image/jpeg;base64,${results.Photo}`}
            onerror="this.style.display='none'"
          />
          <h3 className="title">{results.first_name}</h3>
          <div className="row">
            <div className="col-md-3 col-sm-6">
              {" "}
              {results.Department}
            </div>
          </div>
          {/* row for Location cubicle Prof  */}

          <Link
            to={{ pathname: `/cards/${results.id}`, state: results }}
            className={`card-wrapper restore-${results.id}`}
          >
            View Detail
          </Link>
        </div>
      </div>
    </div>
  ))}
</List>

【讨论】:

  • Men Junius 你是最好的,它工作得很好,如果我想把它移到另一个页面,我可以按部门的名称向过滤器过滤器添加更多字段吗?
  • 在不同的页面上,您只需要原始数据并使用传入的值进行过滤。
  • 如果我想按名称添加过滤器,按部门如何更新onSearchTextChange ?使用多个文本框或下拉菜单
  • 我们可以修改函数来处理多重过滤。见stackoverflow.com/questions/56419688/…stackoverflow.com/questions/58888826/…
猜你喜欢
  • 2012-12-04
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
相关资源
最近更新 更多