AG网格:检测更改并在用户尝试编辑其他行而不保存第一行时发出警报

时间:2020-09-30 11:41:47

标签: angular ag-grid ag-grid-angular ag-grid-validation

我们在Angular 8中有一个新项目,我们正在使用AG Grid,现在用户希望如果有人编辑一行并且不保存将其移动到另一条记录/行而触发验证。 它应该说先保存您的数据。

我已经尝试过onRowChange功能,但是用户不希望在单击时触发验证,他们只希望在编辑某些内容时触发验证。

下面是我所做的示例代码,但这还没有完全达到目的,有人可以帮我吗? 我也尝试过onValueChange(),但没有用。

代码:

    onRowClick(event: any) {
        if (this.cellChangeCount === 0) {
          this.UnsavedNodeIndex = event.node.rowIndex;
          this.UnsavedNodeID = event.data.ID;
        }
        

        this.commonAgGrid.commonAgGridfunc(event, this.gridApi, this.cellChangeCount, this.UnsavedNodeIndex, this.newRowClicked,
          this.UnsavedNodeID, this.colName);
          this.cellChangeCount++;
      }

  commonAgGridfunc(event: any, gridApi, cellChangeCount, UnsavedNodeIndex, newRowClicked, UnsavedNodeID, colName) {
    
    cellChangeCount = cellChangeCount == 0 ? 0 : cellChangeCount;
    cellChangeCount = newRowClicked == true ? 0 : cellChangeCount;
    if (cellChangeCount !== 0 && (UnsavedNodeID != event.data.ID) && !newRowClicked) {

      if (typeof UnsavedNodeID != "undefined") {
        this.alertService.info("Save data first.");
        setTimeout(() => this.alertService.clear(), 2000);
        this.onBtStartEditing(gridApi, UnsavedNodeIndex, colName);
      }
    }
    if (newRowClicked == true && (UnsavedNodeID != event.data.ID)) {
      gridApi.stopEditing();
      gridApi.setFocusedCell(0, colName);
      gridApi.startEditingCell({
        rowIndex: 0,
        colKey: colName,
      });
      gridApi.forEachNode(node => node.rowIndex == 0 ? node.setSelected(true) : node.setSelected(false))
    }

  }

1 个答案:

答案 0 :(得分:0)

您可以尝试使用onCellvalueChanged()代替rowClick。

 onCellvalueChanged(event: any) {
    if (this.cellChangeCount === 0) {
      this.UnsavedNodeIndex = event.node.rowIndex;
      this.UnsavedNodeID = event.data.ID;
    }