过滤数据-React JS(搜索栏)

托尔·克里斯滕森

我制作了一个小项目进行练习,将一张书的清单提取到一个表中,我希望能够根据标题在该表中搜索书。

我认为我必须使用过滤器方法,但是我不确定如何以及在何处使用它。

我的数据被提取到这样的表中:

const [dataFromServer, setDataFromServer] = useState([]);
  const [q, setQ] = useState("")

  useEffect(() => {
    apiFacade.getBooks().then((data) => setDataFromServer(data.all));
  }, []);

  return (
    <div className="container-fluid padding">
      <div className="row">
        <div className="col-3"></div>
        <div className="col-6 text-center">
          <h2 className="text-center mt-5 mb-2">Books</h2>
          <p className="text-muted">Search for a book by title</p>

          <div class="input-group rounded mb-5 mt-2">
            <input
              type="search"
              class="form-control rounded"
              placeholder="Search"
              value={q}
              onChange={(e) => setQ(e.target.value)}
            />
            <button type="button" class="btn btn-primary">
              <FaSistrix />
            </button>

          </div>
          <table className="table">
            <thead>
              <tr>
                <th scope="col">ISBN</th>
                <th scope="col">Title</th>
                <th scope="col">Author</th>
                <th scope="col">Publisher</th>
                <th scope="col">Publish year</th>
              </tr>
            </thead>
            <tbody>
              {dataFromServer && dataFromServer.length > 0 ? (
                dataFromServer.map((m) => (
                  <tr key={m.isbn}>
                    <td>{m.isbn}</td>
                    <td>{m.title}</td>
                    <td>{m.author}</td>
                    <td>{m.publisher}</td>
                    <td>{m.publishYear}</td>
                  </tr>
                ))
              ) : (
                <Spinner animation="border" />
              )}
            </tbody>
          </table>
          <div className="col-3"></div>
        </div>
      </div>
    </div>

我将搜索栏中的输入输入到名为Q(错误的命名)的状态

但是,如何过滤数据?我对此非常了解,如果您有答案,请解释如何以及为什么,这样我才能真正学到一些东西。

Samehanwar

遵循这些步骤。

  • 为过滤后的数据添加新状态,并保持原始数据完整以回退到该状态。

  • 呈现过滤的数据。

  • 实现从源数据过滤数据的功能。

    const [dataFromServer, setDataFromServer] = useState([]);
    const [filteredData, setFilteredData] = useState([]);
    const [q, setQ] = useState("");
    
    useEffect(() => {
      apiFacade.getBooks().then((data) => {
         setDataFromServer(data.all);
         setFilteredData(data.all);
      })
    }, []);
    
    const filtered = (e) => {
      const filtered =
        dataFromServer &&
        dataFromServer.filter((item) => {
          return item.title.toLowerCase().startsWith(e.toLowerCase());
        });
      setFilteredData(filtered);
    };
    
    return (
      <div className="container-fluid padding">
        <div className="row">
          <div className="col-3"></div>
          <div className="col-6 text-center">
            <h2 className="text-center mt-5 mb-2">Books</h2>
            <p className="text-muted">Search for a book by title</p>
    
            <div class="input-group rounded mb-5 mt-2">
              <input
                type="search"
                className="form-control rounded"
                placeholder="Search"
                value={q}
                onChange={(e) => {
                  setQ(e.target.value);
                  filtered(e.target.value);
                }}
              />
              <button type="button" class="btn btn-primary">
                {/* <FaSistrix /> */}
              </button>
            </div>
            <table className="table">
              <thead>
                <tr>
                  <th scope="col">ISBN</th>
                  <th scope="col">Title</th>
                  <th scope="col">Author</th>
                  <th scope="col">Publisher</th>
                  <th scope="col">Publish year</th>
                </tr>
              </thead>
              <tbody>
                {filteredData && filteredData.length > 0
                  ? filteredData.map((m) => (
                      <tr key={m.id}>
                        <td>{m.title}</td>
                      </tr>
                    ))
                  : null}
              </tbody>
            </table>
            <div className="col-3"></div>
          </div>
        </div>
      </div>
    );
    

尝试类似的工作演示

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

react-native过滤器和JSON搜索数据

来自分类Dev

如何过滤每行的数据?使用搜索输入法,根据用户在 react.js 中输入的内容即时更新行

来自分类Dev

TreeView中的React.js搜索过滤器

来自分类Dev

React - 搜索组件以过滤数组

来自分类Dev

数据网格的搜索栏

来自分类Dev

搜索栏未正确过滤

来自分类Dev

在使用useContext在React.js中进行过滤后如何重新获取数据

来自分类Dev

弹性搜索中的过滤数据

来自分类Dev

如何使用搜索栏从数据库数组中过滤数据

来自分类Dev

如何在 React 中过滤 api 数据

来自分类Dev

在React.js中过滤

来自分类Dev

使用React Native搜索和过滤

来自分类Dev

搜索查询以在React中过滤结果

来自分类Dev

使用react和Typescript进行搜索过滤

来自分类Dev

使用React Native搜索和过滤

来自分类Dev

React - 搜索输入未正确过滤

来自分类Dev

带有过滤器的搜索栏,以及使用Ionic 2的JSON数据

来自分类Dev

如何在React JS中创建动态搜索过滤器?

来自分类Dev

Angular JS搜索过滤器未使用Firebase过滤数据

来自分类Dev

React redux 搜索过滤器不显示过滤的内容?

来自分类Dev

React 中书籍 API 的搜索栏功能

来自分类Dev

实施搜索栏以过滤列表项

来自分类Dev

在Angular中从搜索栏调用数据到chart.js图

来自分类Dev

标记化NSString以过滤数据(搜索)

来自分类Dev

最佳实践:搜索 API 和过滤数据

来自分类Dev

React JS中的进度栏

来自分类Dev

数据过滤器覆盖数据搜索属性

来自分类Dev

弹性搜索 - 过滤匹配值和数据类型的数据

来自分类Dev

在React JS中过滤嵌套数组