Skip to content

Instantly share code, notes, and snippets.

@revskill10
Created January 22, 2014 09:25
Show Gist options
  • Save revskill10/8555856 to your computer and use it in GitHub Desktop.
Save revskill10/8555856 to your computer and use it in GitHub Desktop.
Integrate Datatable with React.js
/** @jsx React.DOM */
var LopMonHoc = React.createClass({
getInitialState: function(){
return {data: []}
},
loadData: function(){
$.ajax({
url: '/daotao/lops',
success: function(data){
this.setState({data: data.lops});
}.bind(this)
})
},
componentWillMount: function(){
this.loadData();
},
componentDidMount: function(){
var self = this;
$('#mytable').dataTable({
"sPaginationType": "bootstrap",
"bAutoWidth": false,
"bDestroy": true,
"fnDrawCallback": function() {
self.forceUpdate();
},
});
},
componentDidUpdate: function(){
$('#mytable').dataTable({
"sPaginationType": "bootstrap",
"bAutoWidth": false,
"bDestroy": true,
});
},
render: function(){
var x = this.state.data.map(function(d, index){
return <tr><td>{index+1}</td><td>{d.ma_lop}</td><td>{d.ten_mon_hoc}</td></tr>
});
return (
<div class="table-responsive">
<h4>Hello</h4>
<table class="table table-bordered" id="mytable">
<thead>
<tr class="success">
<td>Stt</td>
<td>Mã lớp</td>
<td>Tên môn học</td>
</tr>
</thead>
<tbody>
{x}
</tbody>
</table>
</div>
)
}
});
@richard-dev-mx
Copy link

didnt work for me, React Js not render the rows created with ajax result. If eliminate the contents of thead and tfooter, the rows are displayed, but other elements disappear (like pagination controls and search control)

@parkerproject
Copy link

parkerproject commented Dec 9, 2016

Why not use react-data-grid instead? React jQuery DataTables is bit heavy to me

@mnhlt
Copy link

mnhlt commented Jan 2, 2017

if you dont worry about delay, you can set time out for $(..) .datatable({..}) about 25ms-50ms in componentDidUpdate(). It's worked for me, easily.

@flexicious
Copy link

React jQuery DataTables is a jquery component, so you will loose react context, and have issues using React components within the cells (you might need to call ReactDOM.render) - a better alternative would be to use a pure React DataTable - here is an example: http://www.reactdatagrid.com/

@Gertiozuni
Copy link

serverSide: true, is not working with this way... how can i do it ?

@gotyet9
Copy link

gotyet9 commented Mar 16, 2017

Data is loading in its place but problem is that is still get no data available at the bottom and searching sorting pagination does not work at all

@J3K
Copy link

J3K commented Apr 26, 2017

If anyone is having a problem, You should implement render() + shouldComponentUpdate() + componentDidUpdate() + componentWillMount()
componentWillMount() : for Network/API stuff
componentDidUpdate() : You init the DataTable $('#YourTable').DataTable();
shouldComponentUpdate() : return True; (Default)
render() : Render your elements like done above.

Here is a Lifecycle when you execute the App.JS :

WillMount
Render
DidMount
Render
DidUpate

@vijaykotha
Copy link

Hi All

I am using jquery DataTable in React Js and I have written specific code for the rendering of a cell. My code is

var handleView = this.handleClick;
x.mRender = function(obj, data, type, row) {
var i1 = $("");
var i2 = $("");
i1.attr("onclick","handleView();");
i2.attr("onclick","handleView();");
var cellContent = $("

").append(i1).append(i2);

			    	return cellContent.prop('outerHTML');
		 	 
			};

I am not able to trigger onClick on i1 / i2.
It is showing that handleView() is not defined.

Could anyone help me out. I have tried many ways but its not finding that funtion

@LIBINLUVIS
Copy link

What about Functional Components

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment