What is the correct way to upadate Grid on delete or add item

1 Answer 334 Views
Grid
Pierre
Top achievements
Rank 2
Iron
Iron
Pierre asked on 26 May 2022, 11:09 PM


<kendo-grid *ngIf="module"
    [kendoGridBinding]="module.permissions"
    kendoGridSelectBy="id"
    [pageSize]="10"
    [pageable]="true"
    [sortable]="true"
    (edit)="editPermissionHandler($event)"
    (cancel)="cancelPermissionHandler($event)"
    (save)="savePermissionHandler($event)"
    (add)="addPermissionHandler($event)"
    (remove)="removePermissionHandler($event)">
    <ng-template kendoGridToolbarTemplate>
        <button kendoGridAddCommand>Add new</button>
    </ng-template>
    <kendo-grid-column field="id" title="id" hidden="true"></kendo-grid-column>
    <kendo-grid-column field="code" title="Code" [width]="180"></kendo-grid-column>
    <kendo-grid-column field="description" title="Description"></kendo-grid-column>
    <kendo-grid-command-column title="" [width]="220">
        <ng-template kendoGridCellTemplate let-isNew="isNew">
            <button kendoGridEditCommand [primary]="true">Edit</button>
            <button kendoGridRemoveCommand>Remove</button>
            <button kendoGridSaveCommand [disabled]="formGroup?.invalid">
                {{ isNew ? "Add" : "Update" }}
            </button>
            <button kendoGridCancelCommand>
                {{ isNew ? "Discard changes" : "Cancel" }}
            </button>
        </ng-template>
    </kendo-grid-command-column>
</kendo-grid>

this is how I delete an item:


let indexToDel = this.module.permissions.findIndex(p => p.id == perm.id);
this.module.permissions.splice(indexToDel, 1)

//way to avoid that?
this.module.permissions = this.module.permissions.slice();

Can we avoid the last line to force the update?

Thanks

Ursus
Top achievements
Rank 1
Iron
commented on 27 May 2022, 07:58 AM

I just had the same problem a couple of weeks ago - I could not find a better way to do this. AFAIK it even states that you need to do this in the documentation - just did a quick search so I could be mistaken! 

1 Answer, 1 is accepted

Sort by
0
Valentin
Telerik team
answered on 31 May 2022, 12:08 PM

Hi Pierre, Ursus,

Thank you for the provided code snippets.

There is no special methods that can be used to reload the Grid. Updating the view of the component relies on Angular's change detection mechanism.

A common pitfall is when a component property isn't updated by reference, just its value is changed (like when using the push method to add a new record to the Grid collection). In such cases, the change detection isn't triggered and the view won't be re-rendered.

Using the slice method is a valid approach to changing the reference of the collection. This is the reason that the last line of code from the provided code snippet is necessary. Another technique is the spread operator [...].

I hope the provided information helps.

Regards,
Valentin
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Grid
Asked by
Pierre
Top achievements
Rank 2
Iron
Iron
Answers by
Valentin
Telerik team
Share this question
or