【问题标题】:How to create a filtered search table using reactJS. Trying to get my search bar to filter the info in the table如何使用 reactJS 创建过滤后的搜索表。试图让我的搜索栏过滤表格中的信息
【发布时间】:2018-12-14 02:35:09
【问题描述】:

我正在尝试让我的搜索栏实际过滤表中的信息。我创建了一个表格和一个搜索栏,但搜索栏目前没有任何功能。现在我可以在搜索框中输入,但没有任何反应。我认为这可能与我使用 react 而不仅仅是 JS 的事实有关。我只是希望能够按名称搜索(所以第一列中的信息)。我对创建组件不是很熟悉,并且查看了每个教程并观看了无尽的视频,但无法使任何工作。任何事情都会有所帮助,谢谢!

这是我的代码:

class App extends React.Component {
  render() {
    return (
      <BrowserRouter>
        <div>
          <Route
            path="/list"
            exact
            strict
            render={() => {
              function myFunction() {
                var input = document.getElementById("myInput");
                var filter = input.value.toUpperCase();
                var table = document.getElementById("myTable");
                var tr = table.getElementsByTagName("tr");
                var td, i;

                for (i = 0; i < tr.length; i++) {
                  td = tr[i].getElementsByTagName("td")[0];
                  if (td) {
                    if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
                      tr[i].style.display = "";
                    } else {
                      tr[i].style.display = "none";
                    }
                  }
                }
              }

              return (
                <div>
                  <div className="spacing overflow">
                    <input
                      type="search"
                      id="myInput"
                      onsearch="myFunction()"
                      onkeyup="myFunction()"
                      placeholder="Search Here"
                    />
                    <table className="table table-bordered" id="myTable">
                      <tr>
                        <th> Name </th>
                        <th> API </th>
                        <th> Product </th>
                      </tr>

                      <tr>
                        <td>
                          <a href="http://localhost:3000/expert"> Expert</a>{" "}
                        </td>
                        <td>
                          <a href="http://localhost:3000/list-expert">
                            Credit{" "}
                          </a>
                        </td>
                        <td> ADD NAME </td>
                      </tr>

                      <tr>
                        <td>
                          <a href="http://localhost:3000/Lex"> Lex </a>
                        </td>
                        <td>
                          <a href="http://localhost:3000/list-lex"> Phone</a>
                        </td>
                        <td> ADD NAME </td>
                      </tr>

                      <tr>
                        <td>
                          <a href="http://localhost:3000/Lex"> Lex </a>
                        </td>
                        <td>
                          <a href="http://localhost:3000/list-lex"> IM</a>
                        </td>
                        <td> ADD NAME </td>
                      </tr>

                      <tr>
                        <td>
                          <a href="http://localhost:3000/star"> star</a>
                        </td>
                        <td>
                          <a href="http://localhost:3000/list-star"> Verify</a>
                        </td>
                        <td> ADD NAME </td>
                      </tr>
                    </table>
                  </div>
                </div>
              );
            }}
          />
        </div>
      </BrowserRouter>
    );
  }
}

【问题讨论】:

  • 它是 React,你不需要更改(你也不应该)生成的 DOM。从 js 数据生成表格,概念证明:data.filter(yourFilterFunction).map((item, index) =&gt; &lt;tr key={index}&gt;&lt;td&gt;{item.name}&lt;/td&gt;&lt;/tr&gt;)。显然,过滤器文本必须在state 中的某个位置(但数据可能来自stateprops 甚至在您的JS/导入模块中硬编码)。我知道这还不够清楚:您应该真正从有关 React 概念的教程开始。而且...嗯...还有render 用于路由器(而不是简单的component 是不常见的...暂时离开路由

标签: javascript reactjs search filter


【解决方案1】:

我只是在 myFunction 周围缺少 { } 而不是“” 所以它应该是这样的:

onKeyUp={myFunction}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    相关资源
    最近更新 更多