如何在我的 nextjs/typescript/fetch 应用程序中搜索这个数组?

发布于 2025-01-16 13:12:44 字数 974 浏览 2 评论 0原文

我一直在尝试寻找方法来搜索这个数组(CustomersList),但没有走得太远。非常感谢您的帮助。

获取后(响应完成了什么):

const data = await res.json();
  return {
    props: {
      CustomersList: data,
    }
  };

我如何将数据显示到页面:

<ul className="customers">
          {CustomersList.map((customer) => (
            <form method="POST" action={AppendUrl(customer.Id)}>
              <table className="customer-list">
                <tr>
                  <th>Customer Name:</th>
                  <th>Customer Id:</th>
                </tr>
                <tr>
                  <td>{customer.Name}</td>
                  <td>{customer.Id}</td>
                  <input type="submit" value="Select"/>
                </tr>
              </table>
            </form>
          ))}
        </ul>

我想向其中添加一个搜索栏,以便能够按客户的姓名(customer.Name)搜索这些客户

I've been trying to find ways to search through this array (CustomersList), but didn't get very far. Help would be much appreciated.

after the fetch(whats done with the response):

const data = await res.json();
  return {
    props: {
      CustomersList: data,
    }
  };

How im displaying the data to the page:

<ul className="customers">
          {CustomersList.map((customer) => (
            <form method="POST" action={AppendUrl(customer.Id)}>
              <table className="customer-list">
                <tr>
                  <th>Customer Name:</th>
                  <th>Customer Id:</th>
                </tr>
                <tr>
                  <td>{customer.Name}</td>
                  <td>{customer.Id}</td>
                  <input type="submit" value="Select"/>
                </tr>
              </table>
            </form>
          ))}
        </ul>

I would like to add a searchbar to this to be able to search through these customers by their name (customer.Name)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻拂→两袖风尘 2025-01-23 13:12:44

我实际上发现了一些非常有用的代码。

搜索码:

//Search Customer
  const [name, setName] = useState("");
  const [foundCustomers, setFoundCustomers] = useState(CustomersList);

  const filter = (e: { target: { value: any } }) => {
    const keyword = e.target.value;

    if (keyword !== "") {
      const results = CustomersList.filter((customer) => {
        return customer.Name.toLowerCase().startsWith(keyword.toLowerCase());
        // Use the toLowerCase() method to make it case-insensitive
      });
      setFoundCustomers(results);
    } else {
      setFoundCustomers(CustomersList);
      // If the text field is empty, show all users
    }

    setName(keyword);
  };

搜索框输入码:

<ul className="customers">
        <input
          type="search"
          value={name}
          onChange={filter}
          className="input"
          placeholder="Customer Name"
        />
        {foundCustomers && foundCustomers.length > 0 ? (
          foundCustomers.map((customer) => (
            <form method="POST" action={AppendUrl(customer.Id)}>
              <table className="customer-list">
                <>
                  <tr>
                    <th>Customer Name:</th>
                    <th>Customer Id:</th>
                  </tr>
                  <tr>
                    <td>{customer.Name}</td>
                    <td>{customer.Id}</td>
                    <input type="submit" value="Select" />
                  </tr>
                </>
              </table>
            </form>
          ))
        ) : (
          <h1>No results found!</h1>
        )}
      </ul>

I actually found some code that was super helpful.

search code:

//Search Customer
  const [name, setName] = useState("");
  const [foundCustomers, setFoundCustomers] = useState(CustomersList);

  const filter = (e: { target: { value: any } }) => {
    const keyword = e.target.value;

    if (keyword !== "") {
      const results = CustomersList.filter((customer) => {
        return customer.Name.toLowerCase().startsWith(keyword.toLowerCase());
        // Use the toLowerCase() method to make it case-insensitive
      });
      setFoundCustomers(results);
    } else {
      setFoundCustomers(CustomersList);
      // If the text field is empty, show all users
    }

    setName(keyword);
  };

searchbox input code:

<ul className="customers">
        <input
          type="search"
          value={name}
          onChange={filter}
          className="input"
          placeholder="Customer Name"
        />
        {foundCustomers && foundCustomers.length > 0 ? (
          foundCustomers.map((customer) => (
            <form method="POST" action={AppendUrl(customer.Id)}>
              <table className="customer-list">
                <>
                  <tr>
                    <th>Customer Name:</th>
                    <th>Customer Id:</th>
                  </tr>
                  <tr>
                    <td>{customer.Name}</td>
                    <td>{customer.Id}</td>
                    <input type="submit" value="Select" />
                  </tr>
                </>
              </table>
            </form>
          ))
        ) : (
          <h1>No results found!</h1>
        )}
      </ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文