Angular 6 - Add new row in AG Grid

后端 未结 4 1738
面向向阳花
面向向阳花 2021-01-05 00:46

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

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 01:29

    for angular:

    set id for html - selector (#agGrid in this example):

    
    
    

    and then define the viewchild with this id, import AgGridAngular like shown below, then you can use the ag-grid api in Angular

    import {Component, OnInit, ViewChild} from '@angular/core';
    import { AgGridAngular } from 'ag-grid-angular';
    
    @Component({
      selector: 'app-angular-handsometable',
      templateUrl: './angular-handsometable.component.html',
      styleUrls: ['./angular-handsometable.component.scss']
    })
    export class AngularHandsometableComponent implements OnInit {
      @ViewChild('agGrid') agGrid: AgGridAngular;
      columnDefs = [
        {headerName: 'Make', field: 'make', sortable: true, filter: true, editable: true },
        {headerName: 'Model', field: 'model', sortable: true, filter: true, editable: true },
        {headerName: 'Price', field: 'price', sortable: true, filter: true, editable: true }
      ];
    
      rowData = [
        { make: 'Toyota', model: 'Celica', price: 35000 },
        { make: 'Ford', model: 'Mondeo', price: 32000 },
        { make: 'Porsche', model: 'Boxter', price: 72000 },
        { make: 'Toyota', model: 'Celica', price: 35000 },
        { make: 'Ford', model: 'Mondeo', price: 32000 },
        { make: 'Porsche', model: 'Boxter', price: 72000 },
        { make: 'Toyota', model: 'Celica', price: 35000 },
        { make: 'Ford', model: 'Mondeo', price: 32000 },
        { make: 'Porsche', model: 'Boxter', price: 72000 },
        { make: 'Toyota', model: 'Celica', price: 35000 },
        { make: 'Ford', model: 'Mondeo', price: 32000 },
        { make: 'Porsche', model: 'Boxter', price: 72000 }
      ];
      constructor() { }
    
      ngOnInit() {
    
      }
    
      save() {
        console.log( 'Save', this.rowData );
      }
    
      addRow() {
        this.agGrid.api.updateRowData({
          add: [{ make: '', model: '', price: 0 }]
        });
      }
    
    }
    

提交回复
热议问题