How To Refresh Ag Grid In Angular
Any web application needs to perform CRUD operations on data and refresh its state after that. Ag-Grid allows your users to hands manipulate data and immediately display any changes in it coming in from API or a server-side update.
In this blogpost, you'll get live samples showing you how to refresh ag-Grid after a data change when all data is stored on the client using the client-side row model. If yous have fewer than xx,000 rows, nosotros recommend using the client-side row model due to many born features information technology offers. ag-Grid likewise supports binding to server-side information for larger data sources - see the ag-Grid information source comparison here.
This sample using customer-side data is implemented in all the popular web frameworks - React, Angular, VueJS. Delight see the sample in activeness beneath:
Alive Demos
See the live examples with code here:
- React
- Athwart
- Vue
- Plain JavaScript
Nosotros volition cover 2 ways to implement this, ane being a full general approach which tin can be applied regardless of the "flavour" of the filigree beingness used. The other involves changing a bound row information holding, which may sound fancy but is super easy, as nosotros'll presently run across...
Contents
- Immutable Data
- Binding Row Data in React
- Bounden Row Data in Angular
- Binding Row Information in Vue
- Binding Row Data in Patently JavaScript
- What's Next
Immutable Data
When you demand to frequently update row information in ag-Grid, using the immutable information grid option is especially valuable. When immutable data is prepare, the grid tin automatically work out which rows need to be updated, replaced, deleted or added when new row data is provided, rather than but replacing all of the row information and redrawing the filigree completely.
This is particularly handy when the default state of the grid changes oftentimes, for example every bit a effect of grouping or filtering. If nosotros were to update row data without using the immutable data filigree option, the filigree would add together the data but revert to its default country, with no filtering practical or expanded group nodes.
For this approach to work, we need to set unique IDs for all of our rows. This is done using the getRowNodeId filigree callback, which is called for each row and lets united states return a unique ID value for that row.
This is all that'south needed to utilize immutable information to simplify the row data update. For boosted details on how to utilise immutable data, delight see our documentation.
Binding Row Data in React
In this department we'll be walking through an instance showing how to bind the grid row data when using React.
Nosotros update the row information in the grid using the immutable data grid option and by making changes to our rowData land variable in our methods.
The lawmaking snippet below shows how to add together a new row and update the grid to display information technology. Please meet the relevant code in the addRowData method, the rowData state variable and the getRowNodeId callback below:
this.state = { rowData: [], ...remainder of the state }; } getRowNodeId = data => { render data.id; }; addRowData = () => { permit newRowData = this.state.rowData.slice(); let newId = this.state.rowData.length === 0 ? 0 : this.land.rowData[this.state.rowData.length - 1].id + 1; allow newRow = { athlete: 'new athlete', id: newId }; newRowData.push(newRow); this.setState({ rowData: newRowData }); };
The addRowData method is the cardinal hither. We first create a re-create of our existing rowData variable, then build our new row. We then add the new row to our rowData copy and finally, we set up the rowData country variable to reference the updated row information object.
Please see a GIF showing adding a new row below:
We can abstract this blueprint and use it to apply the delete and update methods likewise. The design is equally follows: get a re-create of the current row data, make the changes to the copy and demark the copy to the grid.
The full React example tin be found embedded below - delight feel free to explore and play with information technology:
Source: https://blog.ag-grid.com/refresh-grid-after-data-change/
0 Response to "How To Refresh Ag Grid In Angular"
Post a Comment