1

I am trying to add a delete button to every row of my tables rendered with react-bootstrap-table2. I have sorted data by category and placed in my local state

state = {
  data: [
    {
      products: [
        {
          name: "Product 1",
          price: "200"
        },
        {
          name: "Product 2",
          price: "300"
        },
        ...
      ]
    },
    ...
}

and rendered a separate table for each object in this.state.data and filled with the data from the products array from the corresponding object using this.state.data.map((category, index) => ..). I have also set the id of each of the tables to match the index of the this.state.data's object from where they were rendered from.

But where I struggle is, I want to add a delete button to each row; upon searching, I discovered the formatter property for columns that provides us cell, row, rowIndex but how can I access the table's id? So I can potentially use the code in my columns:

columns = () => {
  ...
  {
    isDummyField: true,
    formatter: (cell, row, rowIndex) => {
      return <Button 
             color="primary"
             onClick={() => {
               console.log (`Product ${rowIndex} of Category ${tableId} deleted`);
             }>
             Delete Product
             </Button>
    }
  }
}

I'm lost on how to access tableId. Can anyone help me with this? I have always found react-bootstrap-tables very confusing.

1 Answer 1

2

I solved this problem from the back-end. Basically I added a unique id to each product and then in the button onClick=(), I simply accessed the product's unique id using row.id

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for sharing the solution. That's what I needed :))

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.