How can I use post method in ng2-smart-table whenever a new row is added?

1.8k Views Asked by At

I used ng2 smart table in my angular 2 project, I need to send an API request using http.post() method when a new row is added to table. How can I get the value of each cell in the newly added row?

1

There are 1 best solutions below

0
On

HTML

 <ng2-smart-table
[settings]="settings"
[source]="data"
(createConfirm)="onPostCall($event)"
(editConfirm)="onPostCall($event)">
</ng2-smart-table>

For calling post method on both editing and creating new row

settings [TS]:

settings = {
     add: {
  confirmCreate: true,
     },
    edit: {
      confirmSave: true,
    },
    columns:{
      // your fields and titles here
     }
   }

On update[TS]

     onPostCall(event){
       event.confirm.resolve(event.newData);
              // console.log(event.newData); //this contains the new edited data
           //your post request goes here
          //    example
       const req = http.post('/api/items/add', body);
        // 0 requests made - .subscribe() not called.
       req.subscribe();
      // 1 request made.
    }

Ref : https://akveo.github.io/ng2-smart-table/#/examples/various