【发布时间】:2023-03-24 00:03:01
【问题描述】:
我正在映射我的数据并创建一个表。在每个表格行上,都有一个单选按钮。我希望能够选择该按钮,这将为我提供该行中的所有数据,但是,我什至无法选择它。它不起作用。我在这里做错了什么?
我尝试添加一个值、一个 onChange 侦听器和选中的属性,但看起来我做错了什么。我知道你需要所有这些,但我不确定我做错了什么。
export default class ResultTable extends Component {
state = {
data: [
{
insightName: 'Name 1',
description: 'name 1 desc',
created: new Date(),
createdBy: 'Person 1',
category: 'insight 1',
status: 'published'
},
{
insightName: 'Name 2',
description: 'name 2 desc',
created: new Date(),
createdBy: 'Person 2',
category: 'insight 2',
status: 'published'
},
{
insightName: 'Name 3',
description: 'name 3 desc',
created: new Date(),
createdBy: 'Person 3',
category: 'insight 3',
status: 'published'
}
],
selectedRow: ''
}
handleRadioButton = (e) => {
e.persist()
console.log('clicked', e)
this.setState({
selectedRow: e.target.value
}, () => console.log(this.state.selectedRow))
}
render() {
return (
<div>
<Table striped bordered hover>
<thead>
<tr>
<th> </th>
<th>Insight Name</th>
<th>Insight Description</th>
<th>Created</th>
<th>Created By</th>
<th>Category</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{
this.state.data.map((obj, idx) => {
return (
<tr onClick={this.selectRow}>
<td>
<div class="Form-group">
<div class="Form-radio Form-radio--inline">
<input id="radio7" name="radioInline" type="radio" value={obj.insightName} checked={this.state.selectedRow === obj.insightName} onChange={this.handleRadioButton} />
<label for="radio7"></label>
</div>
<div />
</div>
</td>
<td>{obj.insightName}</td>
<td>{obj.description}</td>
<td>{obj.created.toString()}</td>
<td>{obj.createdBy}</td>
<td>{obj.category}</td>
<td>{obj.status}</td>
</tr>)
})
}
</tbody>
</Table>
</div>
)
}
}
【问题讨论】:
标签: javascript reactjs