Execute [kendoGridAddCommand] on component init

1k views Asked by At

Kendo Default Behaviour

The add event fires when the kendoGridAddCommand under kendoGridToolbarTemplate is clicked.

HTML :

<kendo-grid 
(add)="addHandler($event)"
>
    <ng-template kendoGridToolbarTemplate>
        <button kendoGridAddCommand>Add new</button>
    </ng-template>
    <kendo-grid-column field="id" title="ID" width="120"></kendo-grid-column>
    <kendo-grid-column field="name" title="name" width="120"></kendo-grid-column>
</kendo-grid>

TS :

protected addHandler({sender}) {
    // define all editable fields validators and default values
    const group = new FormGroup({
        'id': new FormControl(),
        'name': new FormControl()
    });

    // show the new row editor, with the `FormGroup` build above
    sender.addRow(group);
}

Requirement

Trigger the addrow event on component init or outside the Grid. By default one row should display with the form controls without click on Add new button.

I tried the below solution provided in SO but did not get success.

KendoUI Angular Grid external command

1

There are 1 answers

0
topalkata On

You can handle the AfterViewInit event, and call the Grid addRow method, e.g.:

ngAfterViewInit() {
  this.formGroup = new FormGroup({
        'ProductID': new FormControl(),
        'ProductName': new FormControl('', Validators.required),
        'UnitPrice': new FormControl(0),
        'UnitsInStock': new FormControl('', Validators.compose([Validators.required, Validators.pattern('^[0-9]{1,3}')])),
        'Discontinued': new FormControl(false)
  });

  this.grid.addRow(this.formGroup);
}

EXAMPLE