0

I have a data representing table in React. Their last 2 columns separate for edit button and delete button.

What I want is when I press the delete button I want to get the index of the row and according to that index, I want to get some data to perform the Axios call. Getting index I used react bootstrap table rowEvents as follows. please see the following code snippet and correct the way it should be.

class ViewEmp extends React.Component{

    constructor(props) {
        super(props);
    }

    rowEvents = {
        onClick: (e, row, rowIndex) => {
           
        }
    };

    handleDelete = (e) =>{
       console.log("delete")
    }

    delete = () =>{
        return <Button color="danger" onClick={
        this.handleDelet}> Delete </Button>;
    }

   columns = [{
        dataField: 'emp_id',
        text: 'Employee ID'
    }, {
        dataField: 'firstName',
        text: 'First Name'
    },{
       text: 'Delete',
       formatter: this.delete
   }];

    render() {
       
        return (
            <Container style={{paddingTop: '10px', /*background: '#102454'*/}}>

                <Row style={{paddingTop: '10vh'}}>
                    <BootstrapTable
                        striped={true} hover={true}
                        keyField='emp_id'
                        data={ this.state.employeeList }
                        columns={ this.columns }
                        rowEvents={ this.rowEvents }
                        pagination={paginationFactory()}>

                    </BootstrapTable>
                    {component}

            </Row>
            </Container>
        );
    }
}
export default ViewEmp;
1
  • Could you create a working example in CodeSandbox? Commented Sep 11, 2020 at 7:33

2 Answers 2

4

It took several hours for me to find out the solution. Actually there is no direct function or any other thing. Simply writing inside formatter.

    const dealColumns = [
      // Other data columns
    
      {
      
        formatter: (cellContent, row) => {
          return (
            <button
              className="btn btn-danger btn-xs"
              onClick={() => this.handleDelete("name of a data field and this will return 
              the value of that cell")}
            >
              Delete
            </button>
          );
        },
      },
    ];
    
   handleDelete = (rowId) => {
      console.log(rowId);
    };

Here is the reference. https://github.com/react-bootstrap-table/react-bootstrap-table2/issues/544

Thanks.

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

Comments

1

Thanks to @dasun_001, for quick implementation sharing the code. Heads up: removing cellContent will not work. I did not do much research further. Hope someone finds it helpful.

const dealColumns = [
  {
    dataField: "id",
    text: "No.",
  },
  {
    dataField: "name",
    text: "First Name",
  },
// columns follow dataField and text structure
  {
    dataField: "remove",
    text: "Delete",
    formatter: (cellContent, row) => {
      return (
        <button
          className="btn btn-danger btn-xs"
          onClick={() => handleDelete(row.id, row.name)}
        >
          Delete
        </button>
      );
    },
  },
];

const handleDelete = (rowId, name) => {
  console.log(rowId, name);
  //1 YourCellName
};

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.