Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add button in a react-bootstrap-table

I'm using react-bootstrap-table (var ReactBsTable = require("react-bootstrap-table")).

I want to add a column that just contain a button in a bootstraptable .

Any idea thanks.

<BootstrapTable data={this.state.activities} pagination={true} hover={true} search={true} selectRow={selectRowProp} options={options}>
  <TableHeaderColumn isKey={true} dataField="id" hidden {true}>ID</TableHeaderColumn>
  <TableHeaderColumn dataField="title" dataSort={true}>Title</TableHeaderColumn>
  <TableHeaderColumn dataField="adress" dataSort{true}>Adress</TableHeaderColumn>
  ///I dont know if here or there is option to use
 </BootstrapTable>

I tried to add a button which display the id or the row data correspondent to this button. I used jquery but I give an id to the button but all these buttons would have the same id so just the first one work and the rest no.

<BootstrapTable data={this.state.activities} pagination={true} hover={true} search={true} selectRow={selectRowProp} options={options}>
  <TableHeaderColumn isKey={true} dataField="id" hidden {true}>ID</TableHeaderColumn>
  <TableHeaderColumn dataField="title" dataSort={true}>Title</TableHeaderColumn>
  <TableHeaderColumn dataField="adress" dataSort{true}>Adress</TableHeaderColumn>
  <TableHeaderColumn dataField="button" dataFormat={this.buttonFunction}></TableHeaderColumn>

 </BootstrapTable>

buttonFunction: function (cell, row) {
        var today = new Date().toISOString();
        if (row.status === "En cours") {
            if (row.dateEnd > today) {
                return <Alert_Validate></Alert_Validate>;
            } else {
                return <label>
                    <button type="button" id="validatebutton" onClick={this._validateFunction} className="bbtn btn-primary btn-sm"><i className="fa fa-check fa-1x" aria-hidden="true"></i>
                    </button>
                </label>

            }
        }
    },

_validateFunction: function () {
        var userid=this.props.params.id;
        $("#validatebutton").click(function() {
            var $row = $(this).closest("tr");
             var activityid = $row.find("td:nth-child(2)");
            console.log("activity id :"+activityid.text());
        });
like image 858
ShMswi Avatar asked Jun 06 '16 12:06

ShMswi


People also ask

How do I add a button to bootstrap react?

Installation. To make sure your buttons are utilizing the Bootstrap CSS, you need to import that into your application, along with the button component. 1import React from 'react'; 2import Button from 'react-bootstrap/Button'; 3import 'bootstrap/dist/css/bootstrap. min.

How do you link a button in react JS?

To use a button as a link in React, wrap the button in an <a> tag or a Link component if using react router. The button will get rendered instead of the link and clicking on it will cause the browser to navigate to the specified page. Copied!

What are the different styles of tables in react bootstrap?

Tables in react-bootstrap come with predefined style classes which are both responsive and reliable. bordered: Adds borders on all sides of the tables and cells. borderless: Removes borders on all sides including table header. variant: It is used to invert the colors of the table from dark to light and vice versa.

What is react-bootstrap button component?

React-Bootstrap Button Component Last Updated : 17 Jun, 2021 Introduction: React-Bootstrap is a front-end framework that was designed keeping react in mind. Bootstrap was re-built and revamped for React, hence it is known as React-Bootstrap.

How do I make a button larger in react bootstrap?

However you can render whatever you'd like, adding a href prop will automatically render an <a /> element. You can use the as prop to render whatever your heart desires. React Bootstrap will take care of the proper ARIA roles for you. Fancy larger or smaller buttons? Add size="lg" , size="sm" for additional sizes.

How to add & delete table rows in react app?

Now, you can add & delete Table rows yourself by running the React App with the following command After that, you can see the Add Delete Table rows UI by opening the following URL in your web browser Hey there, Welcome to CodingStatus. My Name is Md Nurullah from Bihar, India.


2 Answers

You can make a new function buttonFormatter to add button to every cell of the column

// products will be presented by react-bootstrap-table
var products = [{
  id: 1,
  name: "Item name 1",
  price: 100
},{
  id: 2,
  name: "Item name 2",
  price: 100
},........];

// It's a data format example.
function priceFormatter(cell, row){
  return '<i class="glyphicon glyphicon-usd"></i> ' + cell;
},
function buttonFormatter(cell, row){
  return '<BootstrapButton type="submit"></BootstrapButton>';
}

React.render(
  <BootstrapTable data={products} striped={true} hover={true}>
      <TableHeaderColumn dataField="id" isKey={true} dataAlign="center" dataSort={true}>Product ID</TableHeaderColumn>
      <TableHeaderColumn dataField="name" dataSort={true}>Product Name</TableHeaderColumn>
      <TableHeaderColumn dataField="price" dataFormat={priceFormatter}>Product Price</TableHeaderColumn>
      <TableHeaderColumn dataField="button" dataFormat={buttonFormatter}>Buttons</TableHeaderColumn>
  </BootstrapTable>,
    document.getElementById("app")
);
like image 181
iamsaksham Avatar answered Oct 19 '22 14:10

iamsaksham


I had a similar situation, I was implemented this code, I hope it helps you.

https://jsfiddle.net/69z2wepo/63821/

var dataFake = [
{
  name: 'Product 1',
  priceUnit: 23.50
},
{
  name: 'Product 2',
  priceUnit: 24.50
},
{
  name: 'Product 3',
  priceUnit: 33.20
}];


class Products extends React.Component {
  onClickProductSelected(cell, row, rowIndex){
   console.log('Product #', rowIndex);
  }

  cellButton(cell, row, enumObject, rowIndex) {
    return (
       <button 
          type="button" 
          onClick={() => 
          this.onClickProductSelected(cell, row, rowIndex)}
       >
       Click me { rowIndex }
       </button>
    )
 }

 render() {
   return (
    <BootstrapTable data={this.props.data}>
     <TableHeaderColumn dataField='name' isKey>
        Name
      </TableHeaderColumn>
      <TableHeaderColumn dataField='priceUnit'>
        Price
      </TableHeaderColumn>
      <TableHeaderColumn
        dataField='button'
        dataFormat={this.cellButton.bind(this)}
      />
   </BootstrapTable>
  )
 }
}



ReactDOM.render(
   <Products data={dataFake} />,
   document.getElementById('container')
);
like image 35
Juan Felipe Avatar answered Oct 19 '22 15:10

Juan Felipe