0

I was working with tables and I came across this issue: I want to access the data-row-key attribute (shown in the image below) in the table header row at a child row and I'm stuck. Code:

class App extends React.Component {
  render() {
    const columns = [
      // sample of how the JSON API is read
      {
        title: "Title", dataIndex: "title", key: "title",
      },
      // the one that actually matters. becomes the actions column eventually
      {
        title: "Action", dataIndex: "", key: "x", width: "12%",
        render: () => (
          <Popconfirm
            placement="topRight"
            title="Are you sure to delete this task?"
            // retrieve the data here as a parameter into the confirm(n) call
            onConfirm={() => confirm(43)} okText="Yes" cancelText="No"
          >
            <a>delete</a>
          </Popconfirm>
        )
      }
    ];
    return (
      <Table columns={columns} dataSource={this.state.data}/>
    );
  }
}

Right now I have the actual number (43) in there, but I want it to be dynamic as to be able to retrieve the data from the <tr data-row-key=...> tag, shown in the image below.

As a note, there is not a leading id column at the start of the table. The keys are provided through Django's rest framework -- which is in JSON format, in the very last image. Rendered results: enter image description here

JSON format:

enter image description here

Can anyone please help me? Thanks in advance.

2 Answers 2

1

You can use the querySelector for it.

let value = document.querySelector('data-row-key')
Sign up to request clarification or add additional context in comments.

3 Comments

Where should I put it?
You can put in anywhere you need. But that method or function have to called only after the initial rendering.
I used onConfirm={() => confirm(document.querySelector('data-row-key'))} but it turned out to be null, so is document.querySelector('data-row-key')
0

To get the data-row-key value call the onclick method with this function: e.currentTagart.getAttribute("data-row-key") basically you will have some thing similar to this:

<td><i className="fas fa-edit" data-row-key={order.id} onClick={(e)=>getItemKey(e.currentTarget.getAttribute('data-row-key'))} ></i></td>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.