如何在Ng2智能表中创建,编辑和删除数据

时间:2019-10-13 04:10:57

标签: javascript angular typescript ng2-smart-table

我在我的angular 2项目中使用了[ng2 smart table],我需要使用http.post()方法发送一个API请求,但是当我单击按钮以确认数据时在控制台中遇到此错误时出现了问题:< / p>

  

错误TypeError:_co.addClient不是函数。

这是 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.");
    }
  }
)
}

,这也是我的模板

        <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>

相应.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,
    };


那么,如何找到我的错误以及处理创建,编辑和删除操作的最佳方法呢?  预先感谢

1 个答案:

答案 0 :(得分:0)

这是因为addClientservice.ts上的方法,而ng2-smart-table是在该组件上实例化的,因此您不应该在模板上直接调用服务方法。

因此,正确的处理方法是在component.ts上创建一个调用addClient方法的方法。

在您的component.html模板上,我们将editConfirm事件绑定到另一个方法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>

在您的component.ts上,

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

}

此外,在service.ts上,您将从组件传递数据,并从HTTP客户端返回http请求的响应。

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