Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 - Add new row in AG Grid

Tags:

ag-grid

I want to add a new element in AG Grid. I have a following model:

export class PersonModel {
  cars: CarModel[];
}

The AG Grid has as rowData the array Cars of my model. But this array is not Observable. Now I want to add a new car when I click a button:

<button type="button" (click)="onClickCreateCar()">

And in my viewmodel:

  onClickCreateCar() {
    var emptyCar = new CarModel();
    this.person.cars.push(emptyCar);
  }

I can not see the new row in the grid because the array Cars is not observable. It is ok because the property of a model should not be observable. How do you fix the problem?

My AG-Grid definition:

<ag-grid-angular class="ag-theme-fresh" *ngIf="person" style="height: 100%; width: 100%;" [gridOptions]="gridOptions" [rowData]="person.cars" [columnDefs]="columnDefs">

like image 619
user9923760 Avatar asked Oct 04 '18 12:10

user9923760


People also ask

How do I add a new row to AG Grid?

Configuring the pinned rowvar inputRow = {}; var gridOptions = { pinnedTopRowData: [inputRow], ... } We start by providing an empty object to store the data for the new row to be added. As the user enters cell values for each column in the pinned row, AG Grid updates the pinnedTopRowData with the latest inputs.

How do I add a row to AG Grid on click button?

Adding a row to the grid is very simple. Use the gridApi that we get after gridReady to add a row. This process is called applying transaction. Create a button that takes a onClick funtion, when we click the button add a callBack funtion that does the following operation.

How do I make a row editable on AG Grid?

To enable full row editing, set the grid option editType = 'fullRow' . If using custom cell editors, the cell editors will work in the exact same way with the following additions: focusIn : If your cell editor has a focusIn() method, it will get called when the user tabs into the cell.


Video Answer


1 Answers

For insert a new row into ag-grid you shouldn't use the rowData directly it will create\override existing object and all states would be reset, and anyway, there is a method for it setRowData(rows)

But I'd recommend to use updateRowData(transaction):

updateRowData(transaction) Update row data into the grid. Pass a transaction object with lists for add, remove and update.

As example:

gridApi.updateRowData({add: newRows});
like image 179
un.spike Avatar answered Sep 28 '22 19:09

un.spike