简体   繁体   English

使用输入搜索字段 ReactJS 添加搜索功能 state

[英]Add Search functionality using a input search field state in ReactJS

I have made a ReactJS application to display the items from a restaurant menu by making use of an API.我制作了一个 ReactJS 应用程序,通过使用 API 来显示餐厅菜单中的项目。

My API data is in this way:我的API数据是这样的:

[
  {
   "Name": "Chicken pizza",
   "Category": "Pizza",
   "Type": "non-veg",
   "Price": 376,
   "id": "1"
  },
  {
   "Name": "Paneer Cheese Pizza",
   "Category": "Pizza",
   "Type": "veg",
   "Price": 350,
   "id": "2"
  }
]

So to display the above data from my API I have made use of map function in this way因此,为了显示我的 API 中的上述数据,我以这种方式使用了 map function

{filteredData.map((item) => (
      <div>
        <div className="items" key={item.id}>
          <ul>
            <li className={item.Type === "veg" ? "veg" : "non-veg"}></li>
            <li>{item.Name}</li>
            <li>₹ {item.Price}</li>
            <img src="/images/pizza1.jpg" />
            <div className="hr"></div>
          </ul>
        </div>
      </div>
    ))}

Now I have tried adding an input search box with a state like this to filter the list of items on the page现在我尝试添加一个带有 state 的输入搜索框来过滤页面上的项目列表

state: state:

const [search, setSearch] = useState();

search input:搜索输入:


<input type="text" className="search-input" placeholder="Search for dishes"  value={search} onChange={(e)=>setSearch(e.target.value)}/>

Later I have changed my map function to filter data from search in this way below后来我改变了我的 map function 以下面这种方式从搜索中过滤数据

{filteredData.filter(item=>item.Name.toLowerCase().includes(search.toLowerCase())).map(item=>
   <div>
    <div className='items' key={item.id}>
      <ul>
        <li className={item.Type === 'veg' ? 'veg' : 'non-veg'}></li>
        <li>{item.Name}</li>
        <li>₹ {item.Price}</li>
        <img src='/images/pizza1.jpg'/>
        <div className='hr'></div>
      </ul>
     </div>
    </div>
)}

I think I've been making some mistake around the map function but couldn't figure out what to change and make my code work.我想我一直在围绕 map function 犯一些错误,但无法弄清楚要更改什么并使我的代码正常工作。

Guide me of how I can make changes in my code to achieve the search functionality on my react application.指导我如何更改我的代码以在我的 React 应用程序上实现搜索功能。

Share me whatever the idea you feel like would be helpful for me to correct the code and add the search functionality to filter the items.与我分享您认为有助于我更正代码并添加搜索功能以过滤项目的任何想法。

I'll give my sandbox link down below for further information if you need any regarding the code.如果您需要有关代码的任何信息,我将在下面提供我的沙箱链接以获取更多信息。

https://codesandbox.io/s/upbeat-bartik-8jpc2r?file=/src/App.js https://codesandbox.io/s/upbeat-bartik-8jpc2r?file=/src/App.js

I tried adding a state to input search field and made changes on the map function to reflect the search field input on the data given by the user.我尝试将 state 添加到输入搜索字段,并对 map function 进行更改,以反映用户提供的数据的搜索字段输入。

this is my state这是我的 state

const [search, setSearch] = useState();

this is my search field input这是我的搜索字段输入

<input type="text" className="search-input" placeholder="Search for dishes"  value={search} onChange={(e)=>setSearch(e.target.value)}/>

and this is the change I made on the map function这是我在 map function 上所做的更改

.filter(item=>item.Name.toLowerCase().includes(search.toLowerCase()))

I think the issue is that you're filtering the array even if search is undefined or an empty string.我认为问题是即使search未定义或为空字符串,您也在过滤数组。

I believe something like this should achieve the desired result:我相信这样的事情应该达到预期的结果:

// Check if search is "truthy"
(search ?
    // Filter the filteredData array by the search value
    filteredData.filter((item) =>
        item.Name.toLowerCase().includes(search.toLowerCase())
    ) :
    // Otherwise use the unmodified filteredData array
    filteredData
).map((item) => // Continue the map function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM