当前位置: 首页 > 知识库问答 >
问题:

reactjs中搜索功能的实现

李谦
2023-03-14

我正在尝试在ReactJS中实现搜索功能。我不知道该怎么做。下面我给出了我尝试过的代码。
我需要在serach之后在表中显示结果。

                        render() { 
                            return (
                                <div>
                                    <input onChange={this.handleSearchChange} placeholder="Search"/>
                                </div>  
                                )
                        }

//下面是我的功能

            handleSearchChange = e => {
                const { value } = e.target;
                var self = this 
            axios.post("http://localhost:4000/get",  { name:   value })
                .then(function(res){
                    console.log("detail",res.data)
                })
                .catch(function(err){
                    console.log('Error',err)
                })  
            };

//下面是我的api响应

              [
            {color: "green",name: "test",age: "22"},
            {color: "red",name: "test2",age: "23"}
        ]

共有1个答案

施自珍
2023-03-14

一旦有了数据,就需要将其添加到状态,以便在状态更改时可以迭代数据并重新生成视图。我在这个例子中使用了React钩子。希望能帮上点忙。

null

table { 
  background-color: white;
  border-collapse: collapse;
}

tr:nth-child(even) {
  background-color: #efefef;
}

td {
  padding: 1em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone@7/babel.min.js"></script>
<script type="text/babel">

  // Grab the hooks you need
  const { useState, useEffect } = React;

  // The main function that will loop over the data
  // and create the rows for the table
  function createRows(data) {

    // Iterate over the data and for each object
    // return a new HTML row
    return data.map((row, i) => {
      const { color, name, age } = row;
      return (
        <tr key={i}>
          <td>{color}</td>
          <td>{name}</td>
          <td>{age}</td>
        </tr>
      )
    });
  }

  // Mock API call which returns data after a 2 second delay
  function fakeAPICall() {
    return new Promise((res, rej) => {
      setTimeout(() => {
        res('[{"color": "green","name": "test","age": "22"},{"color": "red","name": "test2","age": "23"}]');
      }, 2000);
    });
  }

  function Example () {

    // Set up the state in the componenent
    const [data, setData] = useState([]);

    // When the component renders load in the data
    // and set that as your state.
    useEffect(() => {
      async function getData() {
        const response = await fakeAPICall();
        const data = JSON.parse(response);
        setData(data);
      }
      getData();
    }, []);

    // If there's no data in state display nothing...
    if (!data.length) return <div>No data</div>

    // ...otherwise pass the data into the createRows function
    // and return them the row data
    return (
      <table>
        <thead>
          <th>Color</th>
          <th>Name</th>
          <th>Age</th>
        </thead>
        <tbody>
          {createRows(data)}
        </tbody>
      </table>
    )
  };

  // Render it
  ReactDOM.render(
    <Example />,
    document.getElementById("react")
  );

</script>
<div id="react"></div>  
 类似资料:
  • 我正在尝试实现一个二叉查找树,但是“搜索”函数对于除了根之外的每个条目都返回了错误的值。 该函数应返回其值与键参数匹配的节点的地址,如果节点不存在,则返回 NULL。 当我运行代码时,我得到以下内容: 我知道每个节点的“左”和“右”指针链接正确,因为“delAll”函数成功删除了所有节点。 将“cout”语句添加到“search”函数表明该函数似乎返回了正确的地址。为什么从主主调用时会打印错误的地

  • 本文向大家介绍javascript实现简单搜索功能,包括了javascript实现简单搜索功能的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了javascript实现简单搜索功能的具体代码,供大家参考,具体内容如下 注意事项: A.search(B)可以在A中搜索B的位置,返回B出现的位置 A.split(B)将A以B划分为几部分,并返回数组,相当于分词操作 运行结果如下: 更多搜索

  • 本文向大家介绍Android实现搜索历史功能,包括了Android实现搜索历史功能的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Android实现搜索历史的具体代码,供大家参考,具体内容如下 SharedPreferences实现本地搜索历史功能,覆盖搜索重复的文本,可清空  1. 判断搜索内容是否含表情,不需要可以不判断 2.软键盘工具类弹出、关闭,不需要可以不判断 3.存储工具

  • 本文向大家介绍JS 实现百度搜索功能,包括了JS 实现百度搜索功能的使用技巧和注意事项,需要的朋友参考一下 今天我们来用JS实现百度搜索功能,下面上代码:     HTML部分: CSS层叠样式部分: JS部分:   搜索功能的实现源于百度的 https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd="+otext.value+"&cb=hou

  • 本文向大家介绍jquery中用jsonp实现搜索框功能,包括了jquery中用jsonp实现搜索框功能的使用技巧和注意事项,需要的朋友参考一下 前面的话:     在上周本来想发一篇模仿必应搜索的界面。但是在准备写文章之前突然想到前面学习了ajax技术,在这里我也让我的页面有一种不需要手动刷新就能获取到数据。但是发现用前面的方法并不能获取到我想要的效果。无奈前几天电脑换系统,把之前的源码丢了(前面

  • 本文向大家介绍自定义搜索功能Android实现,包括了自定义搜索功能Android实现的使用技巧和注意事项,需要的朋友参考一下 先看看效果图: 源码下载:自定义搜索功能 代码: SearchActivity.java SearchBean.java SearchAdapter.java CommonAdapter.java ViewHolder.java SearchView.java 布局文件: