简体   繁体   English

如何在 Ant 设计表列渲染中获取单个项目的 ID

[英]How can I get single item's id in Ant Design table column rendering

Here cartItems is the array I passed as a datasource!这里cartItems是我作为数据源传递的数组!

<Table
        style={{ marginTop: '2rem' }}
        columns={columns}
        dataSource={cartItems}
     />

How can I get single item in my cartItems array inside the render() method?如何在render()方法内的cartItems数组中获取单个项目? I just want to extract the id & want to pass it inside deleteFromCart(id).我只想提取 id 并想在 deleteFromCart(id) 中传递它。

  const columns = [
{ title: 'Name', dataIndex: 'name', key: 'name' },
{ title: 'Quantity', dataIndex: 'quantity', key: 'quantity' },
{ title: '$ Unit Price', dataIndex: 'price', key: 'price' },
{ title: '$ Total Price', dataIndex: 'totalPrice', key: 'totalPrice' },
{
  title: 'cartItem',
  dataIndex: 'cartItem',
  key: 'cartItem',
  render: (cartItem) => (
    <a
      style={{ color: 'red' }}
      onClick={(cartItem) => {
        deleteFromCart(cartItem.id)
        console.log('delete')
      }}
    >
      Remove
    </a>
  ),
},

] ]

Im unable to extract the id..Please help!我无法提取 id..请帮忙!

onClick event gives you event object and not the record of table. onClick事件为您提供event object 而不是表的record In your case, since the variable name is same, cartItem from render is being shadowed by cartItem from onClick handler function在您的情况下,由于变量名称相同,因此来自rendercartItem被来自cartItem onClick handler function 的 cartItem 遮蔽

{
  title: 'cartItem',
  dataIndex: 'cartItem',
  key: 'cartItem',
  render: (cartItem) => (
    <a
      style={{ color: 'red' }}
      onClick={(e) => { // remove cartItem from here
        deleteFromCart(cartItem.id)
        console.log('delete')
      }}
    >
      Remove
    </a>
  ),
}

The second argument (record) is the particular record inside the array!第二个参数(记录)是数组中的特定记录!

render: (item,record) => (    //second argument is the item
        <a
          style={{ color: 'red' }}
          onClick={(e) => {
            deleteFromCart(record.id)
          }}
        >
          Remove
        </a>
      ),
    },

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

相关问题 获取ant.design Table列中另一列的值? - Get value of another column in an ant.design Table's column? 如何跟踪“Ant Design-Dynamic Form Item”并将所有值存储在单个对象状态中? - how can i keep track of "Ant Design- Dynamic Form Item" and store all values in a single object state? Object.keys() 返回在 Ant Design 表列渲染中不起作用 - Object.keys() return not working in Ant Design table column rendering Ant Design - 如何在过滤后获得表格行数? - Ant Design - How can i get count of table row after filtering? 如何在服务器端渲染中正确使用带有 ant 设计库的 next js? - How can I use next js with ant design library properly with server side rendering? 无法在 Gatsby 站点的 Ant Design Table 中对列进行排序 - Can't sort Column in Ant Design Table in Gatsby site 如何自定义 Ant 设计的表格? - How can I customised the form of Ant Design? 如何在 Ant.Design 中设置没有表单 object 的 Form.Item 的值? - How can I set the value of a Form.Item without a form object in Ant.Design? 如何在 Ant 设计的 Form.Item 中为 DatePicker 使用 initialValue - How can I use initialValue for DatePicker in Form.Item of Ant Design Ant 设计:如何禁用 hover 上菜单项的边框底部? - Ant Design: How can I disable border-bottom of a menu item on hover?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM