英文:
Mirroring angular-primeng saved data on row
问题
我正在使用angular-primeng表格进行添加和更新。我可以记录数据而不会出现任何问题。我从API应用程序中得到一个结果,就像图片中显示的那样。
我将记录的数据放在**"companyList"**中,但它似乎在表格上有标记。如何在不刷新页面的情况下将我刚刚注册的最新数据添加到表格中?
我想将我刚刚记录的数据信息传输到表格中。
我创建了一个临时的id号码,用"x"来指定,也就是一个本地id,以便可以创建一行新数据。
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'm putting the recorded data in **"companyList"**, 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'm creating a temporary id number that I specify with "x", 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) => {
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);
}
}
[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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论