Search code examples
javascriptangulartypescriptng2-smart-table

How to create,edit and delete data in Ng2 smart table


I used [ng2 smart table] in my angular 2 project, I need to send an API request using http.post() method but the problem happened when I click on the button to confirm data face this error in console:

ERROR TypeError: _co.addClient is not a function.

this is code in service.ts :

import { Injectable } from '@angular/core';
import { Clients } from './clients.model';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable} from 'rxjs';
@Injectable({
  providedIn: 'root'
})
export class ClientsService {

  url="http://localhost:21063/api/clints"
  clients:Clients[];
  client:Clients;

  constructor(private http:HttpClient) { }
getAllClients(): Observable<Clients[]>{
    return this.http.get<Clients[]>(this.url);

}
addClient(event){
this.http.post<Clients>(this.url,this.client)
.subscribe(
  res=>{
    console.log(res);

    event.confirm.resolve(event.Clients);
  },
  (err: HttpErrorResponse) => {
    if (err.error instanceof Error) {
      console.log("Client-side error occurred.");
    } else {
      console.log("Server-side error occurred.");
    }
  }
)
}

and this my template :

        <div class="mainTbl">

            <ng2-smart-table 
            [settings]="settMain" 
            [source]="this.Service.clients"
            (createConfirm)="addClient($event)"
            (editConfirm)="onEditConfirm($event)"
            (deleteConfirm)="onDeleteConfirm($event)"
            ></ng2-smart-table>

        </div>

compontent .ts

settMain = {
    noDataMessage: 'عفوا لا توجد بيانات',

    actions: {
      columnTitle: 'إجراءات',
      position: 'right',
    },
    pager: {
      perPage: 5,
    },
    add: {
      addButtonContent: '  إضافة جديد ',
      createButtonContent: '',
      cancelButtonContent: '',
      confirmCreate: true,
    },
    edit: {
      editButtonContent: '',
      saveButtonContent: '',
      cancelButtonContent: '',
      confirmSave: true,


    },
    delete: {
      deleteButtonContent: '',
      confirmDelete: true,
    },

    columns: {
      id: {
        title: 'كود العميل',
        width: '80px',
      },
      name: {
        title: 'اسم العميل',
        width: '160px'
      },
      phone: {
        title: ' الهاتف'
      },
      address: {
        title: ' العنوان'
      },
      account: {
        title: 'الرصيد '
      },
      notes: {
        title: 'ملاحظات'
      }
    }
  };


  private myForm: FormGroup;

  constructor(private formBuilder: FormBuilder, private Service: ClientsService) { }

  ngOnInit() {

    this.Service.getAllClients().subscribe(data => this.Service.clients = data);
    this.Service.client={
      id:0,
      name:null,
      phone:null,
      address:null,
      type:null,
      account:0,
      nots:null,
      branchId:0,
    };


so how can I find my mistake also what the best way to handle create, edit and delete operations? thanks in advance


Solution

  • That is because addClient is a method on service.ts, whereas the ng2-smart-table is instantiated on that component, and you shouldn't directly call your service method on the template.

    Therefore, the right way of doing things would be to create a method on your component.ts which calls the addClient method.

    On your component.html template, we bind the editConfirm event to another method onAddClient

    <div class="mainTbl">
      <ng2-smart-table 
        [settings]="settMain" 
        [source]="this.Service.clients"
        (createConfirm)="onAddClient($event)"
        (editConfirm)="onEditConfirm($event)"
        (deleteConfirm)="onDeleteConfirm($event)"
      ></ng2-smart-table>
    </div>
    

    On your component.ts,

    onAddClient(event) {
      this.Service.addClient(event).subscribe(
        (res) => {
          // handle success
        }, (error) => {
         // handle error
        });
    
    }
    

    And in addition, on your service.ts, you will pass the data from the compnoent, and return the response from the http request from the HTTP Client.

    addClient(data){
      console.log(data);
      return this.http.post<Clients>(this.url, data);
    }