react-bootstrap-table icon indicating copy to clipboard operation
react-bootstrap-table copied to clipboard

Passing react-router <Link> component to the table cell

Open polyakoff opened this issue 10 years ago • 10 comments

Hello,

got a problem trying to create a hyperlink inside a table cell using react-router component.

I wrote the following function:

function tenantLinkFormat(cell, row) {
    const link = <Link to="/users">cell</Link>;
    console.log(cell);
    console.log(link);
    return link;
}

And got this error message in console:

warning.js:36 Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method of TableBody.

The console output with the objects looks as follows: http://i.imgur.com/RyFGBCD.png

So it seems to me that everything is correct, but it isn't apparently. Please point me to what should be fixed here.

polyakoff avatar Nov 16 '16 05:11 polyakoff

@polyakoff, it's a improvement item that I'll fix it in the future. Because I doesn't make react-bootstrap-table fully capable for newest react.

AllenFang avatar Nov 16 '16 13:11 AllenFang

@AllenFang Got it, thanks. Could you give a tip how to workaround this?

polyakoff avatar Nov 16 '16 14:11 polyakoff

@polyakoff, I think it cant be done on in your repo, It need to change the source codes~

AllenFang avatar Nov 16 '16 16:11 AllenFang

I am thinking of a possibility or an option of exposing a function on the table row such that once the row is clicked, function can retrieve the id/unique key of the row which could be further used to redirect to a link built using the id/unique key. This option could be enabled and disabled at will taking into consideration that inline editing is needed at times.

Also, an extra cell could be added on each record that will host a link.

Another possibility is to be able to detect the propTypes of the props passed by data and render them accordingly.

Hope a work around is implemented asap Courage.

bnlambert avatar Nov 16 '16 22:11 bnlambert

@bnlambert, thanks your advice, you can check this example.

Also, an extra cell could be added on each record that will host a link.

I'm not sure which this means.

AllenFang avatar Nov 17 '16 15:11 AllenFang

You can do transitions with React Router v4 without <Link />:

https://github.com/ReactTraining/react-router/issues/4220

That might be a workaround here.

ghost avatar Dec 01 '16 20:12 ghost

also, wouldn't this work:

<TableHeaderColumn dataField='id' dataFormat={ this.colFormatter }>my header</TableHeaderColumn>

...and then:

  colFormatter = (cell, row) => {
    return (
      <Link to='/some/route'>
        {cell}
      </Link>
    )
  }

That appears to work for me.

ghost avatar Dec 02 '16 21:12 ghost

@bmueller-sykes thanks your finding. Open this issue if someone still have the same issue. Thanks

AllenFang avatar Dec 04 '16 07:12 AllenFang

To navigate using a React Router, you can disable default behavior and turn on the onClick

`import React, { Component } from 'react' import { Route } from 'react-router-dom' import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table' .... class LinkTable extends Component {

hrefIdFormatter = (cell, row) => { const href = product/${row.id}; return ( <Route render={({ history }) => ( <a href={href} style={{ cursor: 'pointer' }} onClick={(e) => { // stop default behavior a href e.preventDefault(); // set state selected product id this.set({ ...this.state, selected: row.id }); // go to the detail product page history.push(/product/${row.id}) }}> {row.id} )} />) } render() { return ( <TableHeaderColumn dataSort={true} dataField="id" width="5%" thStyle={{ fontWeight: 500 }} tdStyle={{ fontSize: '0.8rem' }} dataFormat={this.hrefIdFormatter} isKey>ID .... ) } }`

FreeClimb avatar Apr 17 '17 11:04 FreeClimb

also, wouldn't this work:

<TableHeaderColumn dataField='id' dataFormat={ this.colFormatter }>my header</TableHeaderColumn>

...and then:

  colFormatter = (cell, row) => {
    return (
      <Link to='/some/route'>
        {cell}
      </Link>
    )
  }

That appears to work for me.

@bmueller-sykes thanks your finding. Open this issue if someone still have the same issue. Thanks

Can i use this solution in react-bootstrap-table2? What would you recommend between react-bootstrap-table and react-bootstrap-table2? Thank you!

jackiehamchen avatar Dec 07 '18 07:12 jackiehamchen