如何使用 React DropdownButton 从所选项目中获取额外属性

发布于 2025-01-18 04:31:57 字数 930 浏览 0 评论 0 原文

我是一个从事React原型的Java开发人员,并试图从选定的下拉列表中检索ID属性,然后我们应该将其发布到我们的API中。下拉选项上显示的用户显示的值是演员的名称,而API需要其ID。 我想我可能会在某个地方遇到这个错误的语法,但我看不到哪里。我尝试在 handleselect 调用中添加额外的参数,创建了另一个新的HandleClick函数,并将其添加到下拉菜单中。项目不是下拉button ,更改了参数和呼叫,但是我只对这些字段不确定,现在已经用完了想法。 任何帮助都非常感谢。

<DropdownButton 
onSelect={handleSelect} 
title={selected?.key || list[0].key}>
{list.map((item, index) => {
    return (
        <Dropdown.Item key={index} eventKey=. 
            {item.key} onSelect={handleClick}>
            {item.key}
            </Dropdown.Item>
           );
        })}
 </DropdownButton>
const handleSelect = (key, event, value) => {
  setSelected({
    key: key,
    value: event.target.value
  })

  setActorId(getSelectedActorId(value))
}

const handleClick = (eventKey, event) => {
  console.log(eventKey)
  console.log(event)
}

非常感谢。

I'm a Java developer working on a react prototype and stuck trying to retrieve the id property from the selected dropdown item, which we then should post to our api. The value shown for the user on the dropdown option is an actor's name whereas the api needs their id.
I think I may be getting the syntax for this wrong somewhere but I can't see where. I've tried adding extra params to the handleSelect call, created another new handleClick function and added it to Dropdown. Item instead of DropdownButton, changed params and calls around, but I only get undefined for these fields and am running out of ideas now.
Any help is much appreciated.

<DropdownButton 
onSelect={handleSelect} 
title={selected?.key || list[0].key}>
{list.map((item, index) => {
    return (
        <Dropdown.Item key={index} eventKey=. 
            {item.key} onSelect={handleClick}>
            {item.key}
            </Dropdown.Item>
           );
        })}
 </DropdownButton>
const handleSelect = (key, event, value) => {
  setSelected({
    key: key,
    value: event.target.value
  })

  setActorId(getSelectedActorId(value))
}

const handleClick = (eventKey, event) => {
  console.log(eventKey)
  console.log(event)
}

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见你 2025-01-25 04:31:57

下拉down.item只有一个onclick属性,但没有一个选择性属性:。当您只需要演员的ID来提交时,我只会在该州安全的ID安全,而别无其他。

我创建了一个带有一些虚拟数据的沙箱: https://codesandbox.io/s/vibrant-s/s/vibrant-s/s/vibrant-s/vibrant-s/c- Blackwell-H7S7OP

  • 为演员ID创建一个状态
  • 设置此状态下dropdownbutton设置
  • 适当的EventKey将eventkey设置为下拉列表。iTEM迭代时,因此您可以将它们从OnSelect Callback

编辑中删除:下拉dropdown是带有覆盖菜单的按钮。您通常不会在按钮中显示所选的Valu。对于这种情况,我建议您使用Form.SELECT组件。我已经对沙箱进行了调整。

EDIT2:我还添加了一个hacky下拉按钮,该按钮根据所选值更改标题。但是我不建议您这样做。

Dropdown.Item just has a onClick property, but not an onSelect property: https://react-bootstrap.netlify.app/components/dropdowns/#dropdown-item-props. When you just need the actor's id for submitting, then I would only safe the id in the state and nothing else.

I have created a sandbox with some dummy data: https://codesandbox.io/s/vibrant-blackwell-h7s7op

  • Create a state for actor id
  • Set this state onSelect of the DropdownButton
  • Set appropriate eventKey to Dropdown.Item when iterating, so you get them out of onSelect callback

Edit: Dropdown is a button with an overlay menu. You usually don't display the selected valu in the button. For this case i would recommend you to use Form.Select component. I have adjusted the sandbox accordinly.

Edit2: I have also added a hacky dropdown button which changes its title according to the selected value. But I wouldn't recommend you doing it this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文