简体   繁体   English

POST请求后如何实时刷新数据?

[英]How to refresh data in realtime after POST request?

I have a table which will show data.我有一个显示数据的表格。 Everything is working fine for POST & GET request but I can see the values after inserting only if I refresh the page. POST & GET请求一切正常,但只有在刷新页面后才能看到插入后的值。

abc.service.ts abc.service.ts

createExamCategory(options) {
    return this.http.post<{ message: string }>(this.url + '/createExamCategory', options);
}

getExamCategory():Observable<any> {
    return this.http.get<any>(this.url + '/getAllExamCategory');
}

abc.component.ts abc.component.ts

onSubmit() {
    if(this.formdata.invalid) {
      return;
    }
    this.adminCategory.createExamCategory(this.formdata.value).subscribe(reponse => {
      console.log(reponse.message);
    }); 
}

ngOnInit() {
  this.columnsToDisplay = ['id', 'examCategoryName', 'isActive', 'Action'];
  this.adminCategory.getExamCategory().subscribe((reponse: any) => {
      this.ExamCategoryData = reponse.examCategoryList;
      this.dataSource = new MatTableDataSource(this.ExamCategoryData);
      this.dataSource.paginator = this.paginator;
  }, error => {
       console.log(error);
     }
  );
}

This is my code.这是我的代码。 What necessary changes do I need to make?我需要进行哪些必要的更改?

You have two options here:您在这里有两个选择:

  1. Add the data that you sent to the POST request directly to the table.将您发送到 POST 请求的数据直接添加到表中。

  2. GET the entire data set after you POST to refresh the table. POST 刷新表后获取整个数据集。

Put the data retrieval code into a separate method and reuse it in the POST subscription.将数据检索代码放入单独的方法中,并在 POST 订阅中重复使用。

onSubmit() {
    if (this.formdata.invalid) {
        return;
    }
    this.adminCategory.createExamCategory(this.formdata.value).subscribe(reponse => {
        console.log(reponse.message);
        this.getData();
    });
}

ngOnInit() {
    this.columnsToDisplay = ['id', 'examCategoryName', 'isActive', 'Action'];
   this.getData();
}

private getData() {
    this.adminCategory.getExamCategory().subscribe(
        (reponse: any) => {
            this.ExamCategoryData = reponse.examCategoryList;
            this.dataSource = new MatTableDataSource(this.ExamCategoryData);
            this.dataSource.paginator = this.paginator;
        },
        error => {
            console.log(error);
        }
    );
}

There is an online example of how to Refresh the View of the Mat-Table.有一个如何刷新 Mat-Table 视图的在线示例。 Mat Tables have no idea on their own if the data-source have changed, so you basically have to trigger the change detection. Mat Tables 自己不知道数据源是否已更改,因此您基本上必须触发更改检测。 The following example is straight out of the example project....以下示例直接来自示例项目....

export interface Element {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: Element[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'}
];

Just a properly defined interface and example Dataset只是一个正确定义的接口和示例数据集

// Displayed Columns, not important for the example
displayedColumns = ['position', 'name', 'weight', 'symbol'];
// We define the initial datasource
dataSource = new MatTableDataSource(ELEMENT_DATA);

Then what they do in the example, is basically refreshing the reference of the Array...然后他们在示例中所做的,基本上是刷新 Array 的引用...

addElement() {
// With the array function push() we make it possible for the Mat-Tables to refresh the view
    ELEMENT_DATA.push({position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'})
    this.dataSource = new MatTableDataSource(ELEMENT_DATA);
  }

Source is from this project来源来自这个项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM