reactjs - 在 react.js 下拉列表中添加自定义选项以及如何填充
问题描述
react.js 的新手,正在做一个项目。
我的 jsx 文件是这样的:
const myFunction= () => {
...
declaring the data in variable called statuses
...
return (
<>
<h4>Show statuses</h4>
<div className="float-left">
Filter
<Dropdown
optionLabel="value"
optionValue="key"
options={statuses}
filterBy="value"
filter
showClear
/>
</div>
</>
);
}
状态中的数据显示如下:
[
{
"key": 1,
"text": "Status 1",
"value": "Status 1"
},
{
"key": 2,
"text": "Status 2",
"value": "Status 2"
},
]
我得到的结果是这样的:
<ul class="p-dropdown-items" role="listbox">
<li class="p-dropdown-item" aria-label="Status 1" role="option" aria-selected="false">Stlatus 1</li>
<li class="p-dropdown-item" aria-label="Status 2" role="option" aria-selected="false">Status 2</li>
</ul>
我的问题是:
- 如何在 statuses 变量之前的下拉列表中插入自定义选项?我想要一个自定义选项,即 value='0' label='All'。
- 为什么生成的 html 代码中没有显示每个下拉项的值?我错过了什么还是我需要不同的语法?
谢谢大家
解决方案
您可以在 jsx 中映射您的数组并使用适当的道具返回 Dropdown 元素,如下所示:
{data.map((obj) => {
return (
<Dropdown
key={obj.key} // should be added
optionLabel={obj.text}
optionValue={obj.value}
options={statuses}
filterBy={obj.value}
filter
showClear
/>
)
})}
推荐阅读
- python - 带有 if 语句的优先级方案(首先传递一个条件,然后将其余元素应用于第二个条件)
- mysql - MySQL根据列中的最大值获取行
- amazon-web-services - CloudFront 与 Lambda@Edge 未触发
- sql-server - SQL Server 存储过程 - int 的输入参数列表
- angular - Angular 向一个组件添加多个 Angular Material Badges
- javascript - $ajax 不工作,但 $post 工作
- azure - Azure CDN - 图像响应 404 到 CURL
- python - 如何用pynput按windows键?
- html - VF:是否可以只显示数据表中的特定行?
- mongodb - Golang中创建mongodb索引的方法