Answer the question
In order to leave comments, you need to log in
How to pull an attribute from a table in REACT?
Good day. I ran into a problem that was incomprehensible to me (as for a lamer in js and React programming). I'm confused about getting the value of a cell from a table formed from a GET request on an array, instead of a value it gives out NaN . Tried to forcibly convert to a number, still the same answer. The console shows that the attribute is assigned data-row
Table code and how I write the attribute
renderContent(item,idx){
return(
<React.Fragment key={idx}>
<tr key = { idx } onClick={this.handleChange} data-row={ item.id } >
<td >{item.id} </td>
<td>{item.number}</td>
<td>{item.data}</td>
<td>{item.idstate}</td>
<td>{item.sstatemc}</td>
<td>{item.sstatehl}</td>
</tr>
</React.Fragment>
) }
alert(Айдишник: ' + (event.target.dataset.row, 10) );
Answer the question
In order to leave comments, you need to log in
how it was done in the end
renderContent(item,idx){
return(
<React.Fragment key={idx}>
<tr key = { idx } onClick={this.handleChange} data-row={item.id} >
<td >{item.id} </td>
<td>{item.number}</td>
<td>{item.data}</td>
<td>{item.idstate}</td>
<td>{item.sstatemc}</td>
<td>{item.sstatehl}</td>
</tr>
</React.Fragment>
) }
handleChange(event) {
let target = event.currentTarget.dataset.row;
console.log(target);
}
And why pull something out of the DOM, if you can not pull it out?
return (
<React.Fragment key={idx}>
<tr
key={idx}
onClick={() => {
alert(item.id)
this.handleClick(item.id)
}}
>
<td>{item.id} </td>
<td>{item.number}</td>
<td>{item.data}</td>
<td>{item.idstate}</td>
<td>{item.sstatemc}</td>
<td>{item.sstatehl}</td>
</tr>
</React.Fragment>
)
alert(Айдишник: ' + (event.target.dataset.row, 10) );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question