How to set datasource to server side React AG Grid
In this article, we will show you how to set datasource to server side React AG Grid. Here, we will walk you through the process of setting up an external datasource to the server side AG Grid.
Here, we will add button to set the external datasource to the AG Grid on button click event.
Checkout more articles on React
Demo Application
Package Version
Steps to set datasource to server side React AG Grid
- Add AG Grid in React component
- Create server side AG Grid
- Render the button to set the external datasource
- Output
1. Add AG Grid in React component
Let’s create a react application using the create-react-app
and implement AG Grid in React. Check the following article for more information.
How to implement AG Grid in React
2. Create server side AG Grid
In the next step, we need to implement the REST API to render the AG Grid. Refer to the following article to implement server side AG Grid.
Implement server side pagination in React AG Grid
3. Render the button to set the external datasource
Let’s add the button to set external datasource to React AG Grid on button click. Here, we’ll use the following JSON data to render in Grid. Ensure that the external data source is in the same format as the listed columns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | [{ id: 1, first_name: "Julietta", last_name: "Norker", avatar: "https://robohash.org/eumporroexercitationem.png?size=50x50&set=set1" }, { id: 2, first_name: "Ros", last_name: "Beadel", avatar: "https://robohash.org/quaeratsedet.png?size=50x50&set=set1" }, { id: 3, first_name: "Ker", last_name: "Clemenson", avatar: "https://robohash.org/fugabeataepossimus.png?size=50x50&set=set1" }] |
Now, we have to use the setDatasource()
method to set the data dynamically on button click. Check the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | const changeDataSource = () => { const data = [{ id: 1, first_name: "Julietta", last_name: "Norker", avatar: "https://robohash.org/eumporroexercitationem.png?size=50x50&set=set1" }, { id: 2, first_name: "Ros", last_name: "Beadel", avatar: "https://robohash.org/quaeratsedet.png?size=50x50&set=set1" }, { id: 3, first_name: "Ker", last_name: "Clemenson", avatar: "https://robohash.org/fugabeataepossimus.png?size=50x50&set=set1" }]; const dataSource = { getRows: function (params) { params.successCallback(data, data.length); } }; gridApi.setDatasource(dataSource); } |
Invoke the above function on button click and you will notice that the grid will change.
4. Output
Run the application and check the output in the browser.
I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂