繁体   English   中英

如何在 Ag-grid 中获取更新的行

[英]How to Get Updated Rows in 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
 }
});

回答这个问题有点晚了,但最近我遇到了同样的问题,并提出了一种帮助我克服这个问题的方法,所以想分享一下。

set: Set<string> = new Set<string>();

onCellValueChanged(params: any) {
  this.set.add(params.node.id);
}

onSubmit() {
  this.set.forEach( id => console.log(JSON.stringify(this.grid.api.getRowNode(id).data)));
  // call the api to update data
  // clear the set post update call
  this.set.clear();
}

调用方法onCellValueChanged,对事件cellValueChanged。 在方法内部,将正在编辑的行的行 ID 添加到集合中。 在提交数据时,遍历已编辑的行 ID 集并使用 grid api 获取该行。

检查https://www.ag-grid.com/javascript-grid-accessing-data/#for-each-node

有时您可能想要遍历网格中的所有 rowNode。 这可以使用网格的迭代 API 来完成。 迭代 API 会遍历每个 rowNode,而不管 rowNode 是否显示。 例如,如果分组并且组关闭,则组的子项不会显示在网格中,但是子项包含在迭代“for-each”方法中。

// iterate through every node in the grid
api.forEachNode( function(rowNode, index) {
    console.log('node ' + rowNode.data.athlete + ' is in the grid');
});

// if the order of the iteration is important
api.forEachNodeAfterFilterAndSort( function(rowNode, index) {
    console.log('node ' + rowNode.data.athlete + ' passes the filter and is in this order');
});

选择你需要的函数,然后将每一行的内容推送到数组中:

data = [];
api.forEachNode( function(rowNode, index) {
    data.push(rowNode.data);
});

编辑检查要保存/未保存的行,您可以设置并获取它们的属性: https : //www.ag-grid.com/javascript-grid-row-styles/

在创建和更改行的单元格之一时将 unsaved 设置为 true。

rowNode.setDataValue("unsaved", true);

并在gridOptions进行获取,以便您可以为所有已编辑/插入的行设置“未保存”样式:

gridOptions.getRowStyle = function(params) {
    if (params.node.node.getData().unsaved) {
        return { background: 'red' }
    }
}

如果这不起作用,请告诉我,我在半年内没有使用 aggrid。

暂无
暂无

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

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