Javascript:-如何将值从表传递到 onClick 处理程序以实现删除功能

标签 javascript twitter-bootstrap reactjs ecmascript-6

在 onClick 上,我尝试调用删除函数。我需要的是 tableItem id 的值,我可以在 UI 中看到该值,但无法访问它以删除特定行。场景是:单击->删除 fn->使用 id->删除该行。 我只需要 id 即可实现这一点。但我无法引入删除功能。

import React from 'react';





export default class TradeTable extends React.Component {

	delete = (event) => {
		event.preventDefault();
		
		console.log(event.target.value+"wdccerfec")
	
		//I just need the value of table item id 


	}
	render() {


		var tableData = this.props.store.arr;



		return <div className="panel panel-default">
	
			<div className="panel-body tradeComponent div-background table-responsive">
				<table className="table table-striped tb div-lightbackground">

					<thead className="thead-dark ">
						<tr>
							<th>Commodity</th>
							<th>Date</th>
							<th>Side</th>
							<th>Qty (MT)</th>
							<th>Price (MT)</th>
							<th>CounterParty</th>
							<th>Location</th>
							<th></th>
						</tr>
					</thead>
					<tbody>
                 //Trade data is an array of objects
						{
							tableData.map(tableItem => {
								return (
									<tr>
										<td>{tableItem.tradeDate}</td>
										<td>{tableItem.commodity}</td>
										<td>{tableItem.side}</td>
										<td>{tableItem.quantity}</td>
										<td>{tableItem.price}</td>
										<td>{tableItem.counterparty}</td>
										<td>{tableItem.location}</td>
										<td>{tableItem.id}</td>
										<td>

											

<button type='submit' className="btn btn-primary table-style" value={tableItem.id} onClick={this.delete}  >
    												<span className="glyphicon glyphicon-trash" aria-hidden="true"></span>
											</button>

										</td>
									</tr>)
							})
						}

					</tbody>
				</table>
			</div>
		</div>
	}
}

最佳答案

您需要通过 onClick 按钮传递 id

onClick={this.delete.bind(this, tableItem.id)}

然后

delete = (id) => {

    console.log('Clicked item', id)
}

关于Javascript:-如何将值从表传递到 onClick 处理程序以实现删除功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49445015/

相关文章:

javascript - Express 主体解析器在 req 对象中返回空主体

html - 下拉菜单不与父 div 重叠(bootstrap2)

javascript - 绑定(bind)监听器以动态生成 HTML 中的内容

html - 如何在 bootstrap 3 上获得带有标题的缩略图,该缩略图在右侧垂直对齐到中心

javascript - 尝试在 react 中添加多张图片,但它只显示一张图片

javascript - Reactjs SPA 应用程序 - 一切作为组件(性能和实践)

javascript - Android WebView 在单击按钮时更改 html 的背景和文本颜色

javascript - 避免 jQuery 中的重复代码

javascript - Angular 2窗口全局对象未定义

javascript - 使用 jquery 验证动态输入字段