How to add href to react-bootstrap-table column dynamically?

久未见 提交于 2021-02-16 20:23:18

问题


I'm using react-bootstrap-table for datatable in react, Here am getting c=data from the backend as JSON object. How to add ahref to a column in react-bootstrap-table dynamically

const data = [ 
{ 
  id: 0, 
  name: 'John Doe',
  registered: '2018/01/01',
  role: 'Guest', 
  status: 'Pending'
 }, 
 { 
  id: 1, 
  name: 'Samppa Nori', 
  registered: '2018/01/01', 
  role: 'Member', 
  status: 'Active'
 }
]




<BootstrapTable data={this.data} version="4" striped hover pagination search options={this.options}>
              <TableHeaderColumn dataField="name"     dataSort>Name</TableHeaderColumn>
              <TableHeaderColumn isKey dataField="email">Email</TableHeaderColumn>
              <TableHeaderColumn dataField="age" dataSort>Age</TableHeaderColumn>
              <TableHeaderColumn dataField="city" dataSort>City</TableHeaderColumn>
            </BootstrapTable>

when user click on name column it should redirect to user/:id, so I want to add ahref to name column dynamically . can any help in adding href to react-bootstrap-table


回答1:


after 2 hours of my research, just adding dataFormat into your column solved my solution. Here Cell has the column data and Row has total row data.

CellFormatter(cell, row) {
    return (<div><a href={cell+"/"+row.age}>{cell}</a></div>);
  }

 <BootstrapTable data={this.table} version="4" striped hover pagination search options={this.options}>
                  <TableHeaderColumn dataField="name"  dataFormat={this.CellFormatter}   dataSort>Name</TableHeaderColumn>
                  <TableHeaderColumn isKey dataField="email">Email</TableHeaderColumn>
                  <TableHeaderColumn dataField="age" dataSort>Age</TableHeaderColumn>
                  <TableHeaderColumn dataField="city" dataSort>City</TableHeaderColumn>
                </BootstrapTable>


来源:https://stackoverflow.com/questions/53894031/how-to-add-href-to-react-bootstrap-table-column-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!