【问题标题】:Add custom option in react.js drop down and how to populate在 react.js 下拉列表中添加自定义选项以及如何填充
【发布时间】:2021-01-22 14:53:56
【问题描述】:

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>

我的问题是:

  1. 如何在 statuses 变量之前的下拉列表中插入自定义选项?我想要一个自定义选项,即 value='0' label='All'。
  2. 为什么生成的 html 代码中没有显示每个下拉项的值?我是遗漏了什么还是需要不同的语法?

提前谢谢大家

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    您可以映射您的数组并使用适当的道具返回下拉元素 在你的 jsx 中像这样:

    {data.map((obj) => {
       return (
          <Dropdown
              key={obj.key} // should be added
              optionLabel={obj.text}
              optionValue={obj.value}
              options={statuses}
              filterBy={obj.value}
              filter
              showClear
            />
       )
    })}
    

    【讨论】:

    • 如何在这个例子中添加自定义选项?我需要有一个自定义选项作为第一个选项,然后是要遵循的数据选项
    • @Spiris 我想这个想法是让一个下拉元素等待一个带有选项的数组,正如您之前将其定义为Data。因此,将另一个对象添加到此数组将修改您的选项列表。或者,您可以使用现成的解决方案,例如react-select.com/home
    猜你喜欢
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    相关资源
    最近更新 更多