S
S
Siv_Nefzer2020-04-27 16:47:36
React
Siv_Nefzer, 2020-04-27 16:47:36

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>
        )  }

I try to display first on an alert to understand if I caught it or not
alert(Айдишник: ' + (event.target.dataset.row, 10) );

Judging by my minimum skill, event.target.dataset.row does not work
Tell me what I'm doing wrong or what needs to be added to the code for performance. Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Siv_Nefzer, 2020-04-28
@Siv_Nefzer

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>
        )  }

and output
handleChange(event) {
    let target = event.currentTarget.dataset.row;
    console.log(target);
  }

The problem was the following.:
1- event was not registered in the event
2 - it was necessary to use .currentTarget

A
abberati, 2020-04-27
@abberati

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>
)

UPD: where did you get the brackets with a comma and a ten from here?
alert(Айдишник: ' + (event.target.dataset.row, 10) );

and why? error here, but take my advice - do not refer to the DOM once again, this is not necessary in react.
5ea6f0fe8c53e532924227.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question