简体   繁体   English

如何将对象传递给 onchange 函数? (选择选项)

[英]how to pass an object to an onchange function? (Select option)

I have an select dropdown inside a table and when I'm trying to select an option I want to send the whole object to the onchange function.我在表中有一个选择下拉列表,当我尝试选择一个选项时,我想将整个对象发送到 onchange 函数。 is it possible to send the whole object?是否可以发送整个对象? how can I achieve this?我怎样才能做到这一点?

this is what i tried.这是我试过的。

data = [
{id:1,name:"tasha",code:"T!@#"},
{id:2,name:"dia",code:"ew34"},
{id:3,name:"alex",code:"loiu"},
{id:4,name:"rubee",code:"1234"},
]

const selectUser = (value: any, record: any) =>{
    console.log("selected user details ", value); //if i select first option it will print {id:1 name:"tasha",code:"T!@#"}
    console.log("table record", record);
  }

const column = [
{
      title: "id",
      dataIndex: "id",
      key: "id",
    },
{
      title: "user",
      dataIndex: "user",
      key: "user",
      render: (text: any, record: any) => {
        return (
          <Select
            style={{ width: 120 }}
            onChange={(user) => selectUser(user, record)}
          >
            {data.map((item: any) => {
              return <Option value={item}>{item.name}</Option>;
            })}
          </Select>
        );
      },
    },
]


I'm getting following error我收到以下错误

react-dom.development.js:13414 Uncaught Error: Objects are not valid as a React child (found: object with keys {id, name, code}). If you meant to render a collection of children, use an array instead.

but this works fine without giving any errors if I set the value={item.id} but however I want to send the whole object not only the id但是,如果我设置 value={item.id},这可以正常工作而不会出现任何错误,但是我想发送整个对象而不仅仅是 id

You cannot pass an object into value.您不能将对象传递给值。 HTML attributes are written as string/number. HTML 属性写为字符串/数字。

This is your statement with an error:这是您的声明有错误:

    return <Option value={item}>{item.name}</Option>;

You can pass in the index or id and make use of it later to access your array.您可以传入索引或 id 并稍后使用它来访问您的数组。 Also suggest to use a unique key for list elements.还建议对列表元素使用唯一键。


data = [
{id:1,name:"tasha",code:"T!@#"},
{id:2,name:"dia",code:"ew34"},
{id:3,name:"alex",code:"loiu"},
{id:4,name:"rubee",code:"1234"},
]

const selectUser = (value: any, record: any) =>{
    console.log("selected user details ", value[userId]); //if i select first option it will print {id:1 name:"tasha",code:"T!@#"}
    console.log("table record", record);
  }

const column = [
{
      title: "id",
      dataIndex: "id",
      key: "id",
    },
{
      title: "user",
      dataIndex: "user",
      key: "user",
      render: (text: any, record: any) => {
        return (
          <Select
            style={{ width: 120 }}
            onChange={(userId) => selectUser(userId, record)}
          >
            {data.map((item: any) => {
              return <Option value={item.id} key={ite.id}>{item.name}</Option>;
            })}
          </Select>
        );
      },
    },
]

If you really want to always pass an object, you can create your own custom component which takes in an object.如果你真的想总是传递一个对象,你可以创建自己的自定义组件来接收一个对象。 That will obviously be much more complex than doing the above.显然,这比执行上述操作要复杂得多。

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

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