Reputation: 119
I am using Bootstrap table with columns to populate data. I have set up two action buttons, edit and delete. But I am not sure how to get those buttons to work. The code is:
import React, { Component } from "react";
import { Row, Col, Card, CardBody, CardTitle } from "reactstrap";
import { Link } from "react-router-dom";
// datatable related plugins
import BootstrapTable from "react-bootstrap-table-next";
import paginationFactory, {
PaginationProvider,
PaginationListStandalone,
SizePerPageDropdownStandalone,
} from "react-bootstrap-table2-paginator";
import ToolkitProvider, { Search } from "react-bootstrap-table2-toolkit";
//Import Breadcrumb
import Breadcrumbs from "../../components/Common/Breadcrumb";
import "./datatables.scss";
// Table data
const products = [
{
id: 1,
name: "Airi Satou",
position: "Accountant",
office: "Tokyo",
age: "33",
startdate: "2008/11/28",
salary: "$162,700",
},
{
id: 2,
name: "Angelica Ramos",
position: "Chief Executive Officer (CEO)",
office: "London",
age: "47",
startdate: "2009/10/09",
salary: "$1,200,000",
},
];
class DatatableTables extends Component {
constructor(props) {
super(props);
this.state = {
breadcrumbItems: [
{ title: "Tables", link: "#" },
{ title: "Data Table", link: "#" },
],
page: 1,
sizePerPage: 10,
productData: products,
};
}
render() {
const columns = [
{
dataField: "id",
text: "Id",
sort: true,
},
{
dataField: "name",
text: "Name",
sort: true,
},
{
dataField: "position",
text: "Position",
sort: true,
},
{
dataField: "office",
text: "Office",
sort: true,
},
{
dataField: "age",
text: "Age",
sort: true,
},
{
dataField: "startdate",
text: "Start Date",
sort: true,
},
{
dataField: "salary",
text: "Salary",
sort: true,
},
{
dataField: "menu",
isDummyField: true,
text: "Action",
formatter: () => (
<>
<Link to="#" className="me-3 text-primary">
<i className="mdi mdi-pencil font-size-18"></i>
</Link>
<Link to="#" className="text-danger">
<i className="mdi mdi-trash-can font-size-18"></i>
</Link>
</>
),
},
];
const defaultSorted = [
{
dataField: "id",
order: "asc",
},
];
const pageOptions = {
sizePerPage: 10,
totalSize: products.length, // replace later with size(customers),
custom: true,
};
// Select All Button operation
const selectRow = {
mode: "checkbox",
};
const { SearchBar } = Search;
return (
<React.Fragment>
<div className="page-content">
<div className="container-fluid">
<Breadcrumbs
title="Tables"
breadcrumbItems={this.state.breadcrumbItems}
/>
<Row>
<Col className="col-12">
<Card>
<CardBody>
<CardTitle className="h4">Default Datatable </CardTitle>
<PaginationProvider
pagination={paginationFactory(pageOptions)}
keyField="id"
columns={columns}
data={this.state.productData}
>
{({ paginationProps, paginationTableProps }) => (
<ToolkitProvider
keyField="id"
columns={columns}
data={this.state.productData}
search
>
{(toolkitProps) => (
<React.Fragment>
<Row className="mb-2">
<Col md="4">
<div className="search-box me-2 mb-2 d-inline-block">
<div className="position-relative">
<SearchBar
{...toolkitProps.searchProps}
/>
<i className="search-box chat-search-box" />
</div>
</div>
</Col>
</Row>
<Row>
<Col xl="12">
<div className="table-responsive">
<BootstrapTable
keyField={"id"}
responsive
bordered={false}
striped={false}
defaultSorted={defaultSorted}
selectRow={selectRow}
classes={
"table align-middle table-nowrap"
}
headerWrapperClasses={"thead-light"}
{...toolkitProps.baseProps}
{...paginationTableProps}
/>
</div>
</Col>
</Row>
<Row className="align-items-md-center mt-30">
<Col className="inner-custom-pagination d-flex">
<div className="d-inline">
<SizePerPageDropdownStandalone
{...paginationProps}
/>
</div>
<div className="text-md-right ms-auto">
<PaginationListStandalone
{...paginationProps}
/>
</div>
</Col>
</Row>
</React.Fragment>
)}
</ToolkitProvider>
)}
</PaginationProvider>
</CardBody>
</Card>
</Col>
</Row>
</div>
</div>
</React.Fragment>
);
}
}
export default DatatableTables;
I need to make the edit and delete buttons functional. Delete should remove the row and edit should let me make changes. The table is from a template hence I can not figure out how to make these changes. Any help will be appreciated
Upvotes: 2
Views: 4452
Reputation: 17
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
};
Similar question asked here: how to do delete function with a delete button in react bootstrap row event?
Upvotes: 1