首页 > 解决方案 > 我想在反应中过滤 html 表格数据。但我无法过滤 React.js(或仅 js)中的所有数据

问题描述

我想在 html 表中过滤所有数据。

这是我的代码

  const handleSearch = e => {
    setSearchTerm(e.target.value);
    const input = document.getElementById("search-input");
    const filter = input.value.toUpperCase();
    const table = document.getElementById("patients");
    const tr = table.getElementsByTagName('tr');
    let 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';
        }
      }
    }
  }

此代码导致 td[0] 结果过滤。我想过滤其他 td[i] 数据(例如,我输入文本 td[1] 或 td[2] 值,我会看到过滤 td[1] 数据或 td[2] 数据。但是,我没有知道如何过滤。请帮助我。

标签: javascripthtmlreactjshtml-tabledatatable

解决方案


推荐阅读