在react-bootstrap表中添加按钮

时间:2016-06-06 12:27:34

标签: reactjs react-bootstrap

我正在使用react-bootstrap-table(var ReactBsTable = require("react-bootstrap-table"))。

我想在可引导程序中添加一个只包含按钮的列。

任何想法都谢谢。

<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>

我尝试添加一个按钮,显示与此按钮对应的id或行数据。 我使用了jquery,但是我给了一个id按钮,但所有这些按钮都有相同的id,所以只有第一个工作,其余的没有。

<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());
        });

2 个答案:

答案 0 :(得分:16)

您可以创建一个新功能buttonFormatter,为列的每个单元格添加按钮

// 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")
);

答案 1 :(得分:14)

我有类似的情况,我实现了这段代码,希望对你有帮助。

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')
);