在行上镜像 angular-primeng 保存的数据

huangapple go评论52阅读模式
英文:

Mirroring angular-primeng saved data on row

问题

我正在使用angular-primeng表格进行添加和更新。我可以记录数据而不会出现任何问题。我从API应用程序中得到一个结果,就像图片中显示的那样。

我将记录的数据放在**"companyList"**中,但它似乎在表格上有标记。如何在不刷新页面的情况下将我刚刚注册的最新数据添加到表格中?

我想将我刚刚记录的数据信息传输到表格中。

我创建了一个临时的id号码,用"x"来指定,也就是一个本地id,以便可以创建一行新数据。

在行上镜像 angular-primeng 保存的数据

在行上镜像 angular-primeng 保存的数据

companyList: CompanyModel[] = [];
clonedCompanyList: { [s: string]: CompanyModel } = {};

addCompany(company: CompanyModel) {
  this.companyService.postCompany(company)
    .subscribe({
      next: (res) => {
         this.companyList.push(res);
         this.companyList = [...this.companyList];
         console.log(this.companyList);
      },
      error: (e) => {
        console.log(e);
      },
      complete: () => {
      }
    })
}

//#Edit
onRowEditInit(company: CompanyModel) {
  this.clonedCompanyList[company.id] = { ...company };
}

//#Save
onRowEditSave(company: CompanyModel) {
  if (!company.id.toString().indexOf('x')){
    this.addCompany(company);
    delete this.clonedCompanyList[company.id];
  } else if (company.id.toString().indexOf('x')) {
    this.putCompany(company);
  }
}


<details>
<summary>英文:</summary>

I am adding and updating using angular-primeng table. I can record data without any problem. I get a result from the API application as in the picture.

I&#39;m putting the recorded data in **&quot;companyList&quot;**, but it looks like I marked it on the table. How can I add the last data I registered to the table without refreshing the page?



I want to transfer the information of the data I have just recorded onto the table.


I&#39;m creating a temporary id number that I specify with &quot;x&quot;, that is, a local id so that a new line can be created


[![enter image description here][1]][1]


[![enter image description here][2]][2]

     companyList: CompanyModel[] = [];
     clonedCompanyList: { [s: string]: CompanyModel } = {};


     addCompany(company: CompanyModel) {
      this.companyService.postCompany(company)
        .subscribe({
          next: (res) =&gt; {
             this.companyList.push(res);
             this.companyList = [...this.companyList];
             console.log(this.companyList);
          },
          error: (e) =&gt; {
            console.log(e);
          },
          complete: () =&gt; {
          }
        })
    }

    //#Edit
    onRowEditInit(company: CompanyModel) {
      this.clonedCompanyList[company.id] = { ...company };
    }

    //#Save
    onRowEditSave(company: CompanyModel) {
      if (!company.id.toString().indexOf(&#39;x&#39;)){
        this.addCompany(company);
        delete this.clonedCompanyList[company.id];
      } else if (company.id.toString().indexOf(&#39;x&#39;)) {
        this.putCompany(company);
      }
    }


  [1]: https://i.stack.imgur.com/w00AZ.png
  [2]: https://i.stack.imgur.com/Cfi3A.png

</details>


# 答案1
**得分**: 0

尝试添加公司到您的列表后,执行以下操作:

```javascript
this.companyList.push(newCompany);
this.companyList = [...this.companyList]; // 这样做
英文:

After adding a Company to your list try this.

this.companyList.push(newCompany);
this.companyList = [...this.companyList]; //Do this

huangapple
  • 本文由 发表于 2023年2月6日 14:56:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定