Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ag grid: How to add a row with sorting order

I have been trying an issue like while adding the row into ag grid, it is always adding in bottom of the grid without sort order.

I would like to add the row with sorting order here the example what is happening when I add the row,

Actual results is

4
5
6
1 => Newly added row without sorting.

Expected result is

1 => Newly added row with sorting.
4
5
6

This is the syntax using from Ag-grid.

const addedRow = this.gridOptions.api.updateRowData({ add: [view.data]});
      addedRow.add[0].setSelected(true);

Any expert advice

No sort(Default: Loading the data as per the array order)

8

4

9

1 => Added new Row

Ascending

1 => Added new Row

4

8

9

Descending

9

8

4

1 => Added new Row

like image 934
klmuralimohan Avatar asked Sep 03 '25 01:09

klmuralimohan


1 Answers

You can set the sort order of your grid using the gridApi.

In the onGridReady callback, set the following sort:

onGridReady(params) {
      this.gridApi = params.api;

      var sort = [
          {
            colId: "id",
            sort: "asc"
          }
        ];
        this.gridApi.setSortModel(sort);
    }

Then when you've added your new row, it will be automatically order by the id field in ascending order.

Take a look at this StackBlitz example.

like image 121
ViqMontana Avatar answered Sep 05 '25 10:09

ViqMontana