在ReactJS中使用复选框来筛选蔬菜和非蔬菜数据。

huangapple go评论69阅读模式
英文:

Filter Data between veg and non-veg using a checkbox in ReactJS

问题

我正在开发一个React项目,使用API来显示餐厅菜单的数据。

我的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"
  }
]

在我的项目中,我使用map函数来显示这些数据,如下所示:

{data.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>
)}

现在我想要使用一个复选框来显示只有veg类型的项目,并在禁用复选框时显示所有项目。

我想要使用如下的状态:

const [veg, setVeg] = useState(false);

const vegOnly = () =>{
    setVeg(false);
}

我的复选框的div如下所示:

<label>
   <input type="checkbox" value={veg} onChange={vegOnly}/>
   Veg Only
</label>

请问如何更改我的代码,以便在不干扰现有代码的情况下使用复选框状态来筛选veg和non-veg数据,并实现我想要在项目中添加的功能。

请使用您拥有的任何想法来指导我解决上述问题。如果您需要更多关于我的疑问的澄清信息,这是我的沙盒链接。

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

我想使用复选框来在veg和non-veg之间进行筛选,使用一个状态。但是不知道如何编写复选框状态的函数来筛选数据。

英文:

I'm working on a React project to display the data of a restaurant menu using an API.

My data in the API is in this way.

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

In my project I'm displaying this data using a map function as below:

{data.map(item =&gt;
  &lt;div&gt;
   &lt;div className=&#39;items&#39; key={item.id}&gt;
     &lt;ul&gt;
      &lt;li className={item.Type === &#39;veg&#39; ? &#39;veg&#39; : &#39;non-veg&#39;}&gt;&lt;/li&gt;
      &lt;li&gt;{item.Name}&lt;/li&gt;
      &lt;li&gt;₹ {item.Price}&lt;/li&gt;
      &lt;img src=&#39;/images/pizza1.jpg&#39;/&gt;
      &lt;div className=&#39;hr&#39;&gt;&lt;/div&gt;
     &lt;/ul&gt;
    &lt;/div&gt;
   &lt;/div&gt;
)}

Now I want to show only the items with the type veg in my API using a checkbox and show all the items when the checkbox is disabled.

I want to use a state like this

const [veg, setVeg] = useState(false);

const vegOnly = () =&gt;{
        setVeg(false);
    }

and my div for checkbox is

&lt;label&gt;
   &lt;input type=&quot;checkbox&quot; value={veg} onChange={vegOnly}/&gt;
   Veg Only
&lt;/label&gt;

How can I change my code in such a way that I can filter the data from veg and non-veg using a checkbox state without disturbing my code and achieve the functionality I want to add in my project.

Guide me with whatever the ideas you have that can help me in the above code.

Here is my sandbox link for further information if you need any more clarification about my doubt.

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

I want to use the checkbox to filter between the types veg and non-veg using a state. But don't know how to write the function for the checkbox state to filter the data.

答案1

得分: 0

你可以通过过滤数据来实现这个功能。我们检查 vegOnly 是否为假,如果是,则返回 data,如果为真,则返回按 Type 过滤后的数据。

const [data, setData] = useState([]); // 初始值为 []

const [vegOnly, setVegOnly] = useState(false);

const handleVegInputChange = (e) => {
  const isChecked = e.target.checked;
  setVegOnly(isChecked);
};

const filteredData =
  vegOnly === false ? data : data.filter((item) => item.Type === "veg");

使用新的处理程序进行输入

<label>
   <input type="checkbox" value={veg} onChange={handleVegInputChange}/>
   仅蔬菜
</label>

filteredData 上进行映射而不是 data

return (
  <>
    {filteredData.map((item) => (
      <div>
        <div className={item.Type === "veg" ? "veg" : "non-veg"} 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>
    ))}
  </>
);
英文:

You can do this by filtering the data. We check if vegOnly is false if so just return the data, if true return the data filtered by Type

const [data, setData] = useState([]); // initial value of []

const [vegOnly, setVegOnly] = useState(false);

const handleVegInputChange = (e) =&gt; {
  const isChecked = e.target.checked;
  setVegOnly(isChecked);
};

const filteredData =
  vegOnly === false ? data : data.filter((item) =&gt; item.Type === &quot;veg&quot;);

Input with the new handler

&lt;label&gt;
   &lt;input type=&quot;checkbox&quot; value={veg} onChange={handleVegInputChange}/&gt;
   Veg Only
&lt;/label&gt;

Map over the filteredData instead the data

return (
  &lt;&gt;
    {filteredData.map((item) =&gt; (
      &lt;div&gt;
        &lt;div className=&quot;items&quot; key={item.id}&gt;
          &lt;ul&gt;
            &lt;li className={item.Type === &quot;veg&quot; ? &quot;veg&quot; : &quot;non-veg&quot;}&gt;&lt;/li&gt;
            &lt;li&gt;{item.Name}&lt;/li&gt;
            &lt;li&gt; {item.Price}&lt;/li&gt;
            &lt;img src=&quot;/images/pizza1.jpg&quot; /&gt;
            &lt;div className=&quot;hr&quot;&gt;&lt;/div&gt;
          &lt;/ul&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    ))}
  &lt;/&gt;
);

huangapple
  • 本文由 发表于 2023年1月8日 23:19:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定