[英]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在您的情况下,由于变量名称相同,因此来自render
的cartItem
被来自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.