How to get and set ag-grid state?

How can I get and reset the state of the ag-grid table? I mean which columns are displayed in what order, with what sorting and filtering, etc.

Update : getColumnState and setColumnState seem to be close to what I want, but I cannot understand the callbacks in which I have to save and restore state. I tried to restore it to onGridReady, but when the actual rows load, I lose state.

+12
ag-grid
source share
7 answers

I believe you are looking for this Docs page . This describes the api column and what features are available to you. The functions that are most important will be:

  • getAllDisplayedColumns() - used to show which columns can be displayed. Due to virtualization, there may be some columns that are not yet displayed in the DOM, if you only want the columns displayed in the DOM, then use getAllDisplayedVirtualColumns() - both functions display the order as they will be displayed on the web page
    • The Column object that is returned from these functions contains sorting and filtering attributes for each of the columns.

I believe that everything you need will be available to you from this function, which will be called as gridOptions.columnApi.getAllDisplayedColumns()

Other features that may be helpful:

  • Available from gridOptions.columnApi :
    • getColumnState() - returns objects with less granularity than the above functions - has only: aggFunc, colId, hide, pinned, pivotIndex, rowGroupIndex and width
    • setColumnState(columnState) - this allows you to set columns to hidden, visible or pinned, columnState should be what is returned from getColumnState()
  • Available from gridOptions.api :
    • getSortModel() - gets the current sorting model
    • setSortModel(model) - set the grid sorting model , model should be of the same format that is returned from getSortModel()
    • getFilterModel() - gets the current filter model
    • setFilterModel(model) - set the model of the mesh filter, model should be of the same format that is returned from getFilterModel()

There are other functions that are more specific if you only want to bind a column that you can use setColumnPinned , or use setColumnPinned for several columns at once, and additional functions are available on linked AG pages -Grid docs

+4
source share

The gridReady event should do what you need. I suspect that your filter state is reset when updating the grid with data - you can change this behavior by setting filterParams: {newRowsAction: 'keep'}

This can be specified column- defaultColDef or globally with defaultColDef .

Here is an example configuration that should work for you:

 var gridOptions = { columnDefs: columnDefs, enableSorting: true, enableFilter: true, onGridReady: function () { gridOptions.api.setFilterModel(filterState); gridOptions.columnApi.setColumnState(colState); gridOptions.api.setSortModel(sortState); }, defaultColDef: { filterParams: {newRowsAction: 'keep'} } }; 

I created a plunker here that illustrates how this will work (note that I am restoring state from hard code lines, but the same principle applies): https://plnkr.co/edit/YRH8uQapFU1l37NAjJ9B . The rowData parameter is set to timeout 1 second after loading

+4
source share

There is a very specific example on their website containing details of what you are trying to do: javascript-grid-column-definitions

 function saveState() { window.colState = gridOptions.columnApi.getColumnState(); window.groupState = gridOptions.columnApi.getColumnGroupState(); window.sortState = gridOptions.api.getSortModel(); window.filterState = gridOptions.api.getFilterModel(); console.log('column state saved'); } 

and for recovery:

 function restoreState() { gridOptions.columnApi.setColumnState(window.colState); gridOptions.columnApi.setColumnGroupState(window.groupState); gridOptions.api.setSortModel(window.sortState); gridOptions.api.setFilterModel(window.filterState); console.log('column state restored'); } 
+4
source share

You must do the following.

Include div for grid in html

 <div id="myGrid" style="width: 500px; height: 200px;"></div> 

Js side

 //initialize your grid object var gridDiv = document.querySelector('#myGrid'); //Define the columns options and the data that needs to be shown var gridOptions = { columnDefs: [ {headerName: 'Name', field: 'name'}, {headerName: 'Role', field: 'role'} ], rowData: [ {name: 'Niall', role: 'Developer'}, {name: 'Eamon', role: 'Manager'}, {name: 'Brian', role: 'Musician'}, {name: 'Kevin', role: 'Manager'} ] }; //Set these up with your grid new agGrid.Grid(gridDiv, gridOptions); 

Mark this pen for more features. https://codepen.io/mrtony/pen/PPyNaB . This is done using angular

0
source share

Automatic aggregation when a custom grouped grouping stops working after setColumnState

0
source share

To save filter values, you can use filterParams: {newRowsAction: 'keep'}

0
source share

Step 1:

First of all, you need to add dependencies to your project. In package.json add the following code:

 "dependencies": { ... "ag-grid": "6.1.0", "ag-grid-ng2": "6.1.0" } 

Make sure the latest version of ag-Grid is installed.

Step 2:

Update System.config.js to use ag-grid in your project.

 System.config({ packages: { ... 'ag-grid-ng2': { defaultExtension: "js" }, 'ag-grid': { defaultExtension: "js" } }, map: { ... 'ag-grid-ng2': 'node_modules/ag-grid-ng2', 'ag-grid': 'node_modules/ag-grid' } }); 

Step 3:

Import the AgGridModule into the module in which you want to use it.

 import {AgGridModule} from 'ag-grid-ng2/main'; @NgModule({ imports: [ BrowserModule, AgGridModule.forRoot(), ... }) 

forRoot - ensure that AgGrid providers are introduced at the root level.

Step 4:

And the last thing you need to enable CSS for ag-Grid. You can do it right in your index.html

 <link href="node_modules/ag-grid/dist/styles/ag-grid.css" rel="stylesheet" /> <link href="node_modules/ag-grid/dist/styles/theme-material.css" rel="stylesheet" /> 

Here it is. Setup completed.

Let's move on to the implementation

Create the component in which you want to use your grid.

 @Component({ selector: 'ag-grid', template: ` <ag-grid-ng2 style="width: 80%; margin-left: 10%" #agGrid class="ag-material" [gridOptions]="myGridOptions"> </ag-grid-ng2>` }) **export class MyGridComponent{ }** 

In the template we have a tag. This is provided by previously imported AgGridModule. Note the property "gridOptions", we assigned it to the variable "myGridOptions", and therefore we need to initialize this variable in the component. Add the following code to the component:

  private myGridOptions: GridOptions = {}; 

This initializes an empty grid. Now, obviously, we need some data in our grid.

To add a date to ag-Grid, we have two GridOptions properties:

 rowData columnDefs 

we can initialize both of them to ngOninit. Import OnInit and add it to the component and add the following code:

 import { OnInit } from '@angular/core'; export class MyGridComponent implements OnInit{ ngOnInit() { this.myGridOptions.rowData = this.createRowData(); this.myGridOptions.columnDefs = this.createColumnDefs(); } private createColumnDefs() { return [ {headerName: "NAME", field: "name", width: 100}, {headerName: "AGE", field: "age", width: 100}, {headerName: "GENDER", field: "gender", width: 100} ] } 

// data are mapped to the corresponding field header key

 private createRowData() { return [ {name: "Geller", age: 30, gender: "Male"}, {name: "Geller", age: 28, gender: "Female"}, {name: "Tribbiani", age: 29, gender: "Male"}, {name: "Bing", age: 30, gender: "Male"}, {name: "Green", age: 28, gender: "Female"}, {name: "Buffay", age: 29, gender: "Female"} ]; } } 

Here we have two methods that add headers and row data to the grid.

Here's what your grid looks like:

 blog1_1 

We use a material theme, but you can choose the one you like here.

Now, in the general case, you will receive this data from the service. Let's create a service layout for this. But before that, we must create a type for our data.

Create the "friends.ts" file and create the Friends class as follows:

 export class Friends{ name: string; age: number; gender: string; } 

Now for the breadboard service, create the friends.service.ts file and add the following code:

 import {Friends} from "./friends"; export class FriendsService{ getDate() { let friends: Friends[] = [ {name: "Geller", age: 30, gender: "Male"}, {name: "Geller", age: 28, gender: "Female"}, {name: "Tribbiani", age: 29, gender: "Male"}, {name: "Bing", age: 30, gender: "Male"}, {name: "Green", age: 28, gender: "Female"}, {name: "Buffay", age: 29, gender: "Female"}, ]; return friends } } 

To use the service, add it to the vendors section of your ngModule.

Now we need to update our methods in the component. Rows will be directly displayed because the rowData property needs an array of any type, so all we need to do is update the createColumnDefs method.

 constructor(private friendsService: FriendsService){} ngOnInit() { let data: Friends[] = this.friendsService.getDate(); this.myGridOptions.rowData = data; this.myGridOptions.columnDefs = this.createColumnDefs(data[0]); } private createColumnDefs(friends: Friends) { let keyNames = Object.keys(friends); let headers = []; keyNames.map(key => { headers.push({ headerName: key.toUpperCase(), field: key, width: 100 }) }); return headers; } 

We simply extract the keys from the data, add properties such as width and field, and insert them into the array.

-one
source share

All Articles