-
I’m using jQuery DataTable inside react. (I know, not good to use jQuery in react but it’s since long and many feature customize in that plug-in which we did not get in other plug-in)
-
Using columns hook we can add anchor link in one of column (it’s working fine but page is refresh due to not use react router)
let columnConfig = [ ...ManyOtherColumns, { orderable: false, className: 'view-page-cell', "width": "35px", "defaultContent": "", "createdCell": function (td, cellData, rowData, row, col) { //Some custom logic here... let anchor = `<a class="view-page" title="${title}" href="${url}"> <img class="image" src="${viewIcon}" width="16" /> </a>`; ReactDOM.render(anchor, td ) } } ]
-
Not able to add
Link
ofreact-router-dom
(using version "5.2.0") (code example as below), getting error likeUncaught Error: Invariant failed: You should not use <Link> outside a <Router>
let columnConfig = [ ...ManyOtherColumns, { orderable: false, className: 'view-page-cell', "width": "35px", "defaultContent": "", "createdCell": function (td, cellData, rowData, row, col) { // custom logic here.. ReactDOM.render(<Link to={url} isactive="true" className="view-page"><img src="${viewIcon}" /></Link>, td ) } } ]
-
DataTable Initialize as below
wrapper.find('.data-table-grid').DataTable({ ...OtherConfig, columns: columnConfig });
For more check my codepen: https://codepen.io/jsnaghera/pen/vYrwgQz
NOTE: Using click event on cell and useHistory
will not full-fill my requirement because I need feature like user can righ+click and open page in new tab.
2
Answers
You can write
onClick
instead of<a>
or<Link>
then move page to yourURL
by that function.I’m sorry to say it, but you will not be able use
react-router
inside of your jQuery code.When you call
ReactDOM.render
here you are creating a completely separate component tree. ThisLink
will not be inside of theRouter
that’s in yourApp
.The simplest solution is to use a standard
<a>
tag like<a href="/view-page">
. React-router will handle loading the correct content for that path. But clicking this link will cause a full reload of the App rather than navigating inside the existing App instance like you would with a<Link>
.You can use
react-router
so long as you are not using an additionalReactDOM.render
.I came up with a solution where you use the
<Link>
in the code which creates the table markup, rather than where you modify the markup in jQuery.I made a component that returns a
<td>
:And then used that inside the
return
of yourApp
:This eliminates your
columnDef
, so now you just need:Revised CodePen