angular - 如何在 Ag-grid 中获取更新的行
问题描述
单击“插入新行”按钮后,将触发以下方法,并将一个空的新行附加到当前最后一行。
insertNewRow(){
var newItem = this.createNewRowData();
var res = this.gridOptions.api.updateRowData({add: [newItem]});
}
createNewRowData(){
var newData = [];
//blah blah
return newData;
}
在我丰富了屏幕上的数据后,如何让网格获取新行(可能还有自上次数据库检索以来已更改的所有行)?
请注意,在单击“保存”按钮之前,我可以多次单击“插入新行”以创建多行。
saveChanges(){
var data = "HOW TO GET NEW ROWS AND UPDATED ROWS, AND STORE HERE??";
this.myAppService.saveData(data).subscribe(
//blah blah
);
console.log("Saved successfully to database");
}
编辑 1
尝试将以前的记录存储到变量(“tableData”)中:
tableData: string[] = [];
currentData: string[] = [];
retrieveTableData (){
//blah blah
tableData loaded with an array of json
}
saveChanges(){
this.gridOptions.api.forEachNode((rowNode) => {
this.currentData.push(rowNode.data);
});
console.log(this.currentData);
this.currentData.forEach(item => {
console.log(item);
if (!this.tableData.includes(item)){
console.log("Updated Row: " + item);
}
});
}
我想数据类型等存在一些问题。
控制台记录“tableData”和“currentData”的示例:
[object Object],[object Object],[object Object]
如果我打印出任一变量下的每个项目:
{FUND_CODE: "TEST1", PORT_CODE: "TEST1"}
解决方案
另一种方法是在您的对象中保留标志以识别其新的/修改的,
interface Item {
//other properties
newOrModified?: string;
}
_______________
items:Item[];
insertNewRow(){
var newItem = this.createNewRowData();
newItem.newOrModified="New";
var res = this.gridOptions.api.updateRowData({add: [newItem]});
}
onCellValueChanged(item: Item ) {
item.newOrModified="Modified";
}
saveChanges(){
cost modifieditems = this.items.filter(item=> item.newOrModified === "Modified" );
cost newitems= this.items.filter(item=> item.newOrModified === "New" );
}
<ag-grid-angular style="width: 100%; height: 500px;"
class="ag-fresh"
[columnDefs]="columnDefs"
[rowData]="items"
(gridReady)="onGridReady($event)"
(cellValueChanged)="onCellValueChanged($event)">
</ag-grid-angular>
你可以这样做,
//store your old records in one variable
this.previousrecords = this.http.getAllRecords();
this.bindRecrods = [...this.previousrecords];//bind this to grid
//when saving records try like this
this.bindRecrods.forEach(rec=> {
if( !this.previousrecords.includes(rec) ){
//insert or update record
}
});
推荐阅读
- axon - Spring boot + axon framework starter + 使用 JGroups 的分布式命令总线
- python - Python 从 FTP 下载文件,其中文件名以特定字符开头
- visual-studio-code - 启动时的 Visual Studio Code 拆分终端
- python - Pydub Slice Audio Segment By Sample
- ios - 如何修复 Xcode 项目中的文件夹引用
- apache-spark - Spark: spark-submit 通过 spark-submit 将 pandas 之类的包发送到所有节点
- pdfbox - COSName.B 是什么?
- python - csv 文件的 INSERTION 中格式字符串的参数不足
- python - 你可以在 Django 中将变量分配给下划线吗
- python-3.x - 在第一个元素在特定范围内的元组中查找元组列表中的最小值