Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ag-Grid: How to save and reload column order

Using Ag-Grid, users can drag columns to order them the way they like. I need to allow the user to save their column order (to an SQL backend) so that it becomes the default column order for them. I was trying to get the column names like this:

var cols = schedGridOptions.columnApi.getAllColumns();
for (col in cols) {
    var colDef = col.getColDef();
    console.log(colDef.headerName);
}

This was an example I found for setting the header name, so I tried to adapt it to getting the header name. But I get this error:

JavaScript runtime error: Object doesn't support property or method 'getColDef'

Perhaps I'm not doing this correctly? I'm fairly new at using Ag-Grid. Looking for suggestions.

like image 362
tolsen64 Avatar asked Jul 23 '18 22:07

tolsen64


People also ask

How do you maintain column order in ag-Grid?

In order for the Column Order to be maintained, the grid needs to match the Columns. This can be done by ensuring each Column has a field or colId defined.

How do you refresh a column on ag-Grid?

Refresh Cells: api. refreshCells(cellRefreshParams) - Gets the grid to refresh all cells. Change detection will be used to refresh only cells whose display cell values are out of sync with the actual value. If using a cellRenderer with a refresh method, the refresh method will get called.

How do you refresh ag-Grid row data?

Update the Row Data inside the grid by updating the rowData grid property or by calling the grid API setRowData() .

How do I set the default sorting on ag-Grid?

To change the default action to use the Ctrl key (or Command key on Apple) instead set the property multiSortKey='ctrl' . The example below demonstrates the following: The grid sorts by Country then Athlete by default.


1 Answers

onColumnMoved will fire every time the column is moving but the drag didn't stopped.

Using onColumnMoved is not performant at all.

If you care about performance you should use onDragStopped

gridOptions.onDragStopped = function (params) {
  const colIds = params.columnApi.getAllDisplayedColumns().map(col => col.colId)
  console.log(colIds) // all visible colIds with the visible order
}
like image 140
Roland Avatar answered Sep 28 '22 15:09

Roland