指向新屏幕的单元格中的 Ag-grid 按钮

Ag-grid button in cell that directs to new screen

我在 table 的网格中为每一行添加了“编辑”按钮。 table 的数据来自具有以下 JSON 响应的 api。

{ 
 Id: 1783
 total: 2
 trasfer: true
 Sizing: true
 name: "runner"
}

我正在尝试实现当用户单击编辑按钮时会出现一个新屏幕,可以在其中编辑该行的值。到目前为止,我已经实现了一个按钮呈现组件并在单击按钮时发出警报。我如何实现路由器到新屏幕以及该特定行的 editable 数据。

演示:https://ag-grid2.zoltanhalasz.net/

按钮-renderer.component.ts

@Component({
  selector: 'app-button-renderer',
  template: `
    <button type="button" (click)="onClick($event)">{{label}}</button>
    `
})
export class ButtonRendererComponent implements ICellRendererAngularComp { 
    afterGuiAttached?(params?: import("ag-grid-community").IAfterGuiAttachedParams): void {
        return;
    }  
    ngAfterViewInit(): void {
        return;
    }
  public params;
  label: string;

  constructor(){}
  
  agInit(params: ICellRendererParams): void {
    this.params = params;
    this.label = this.params.label || null;
}
  refresh(params: any): boolean {
    return false;
}

public onClick(event) {
    this.params.data[this.params.colDef.field] = event.checked;

    if (typeof this.params.context.componentParent.notifyCellUpdate === "function"){
        this.params.context.componentParent.CellUpdate(this.params);
    }
    
 }
}

app.component.ts

       columnDef = [   {
            headerName: 'Button',     field : 'changeSettings',     
            cellRenderer: 'buttonRenderer',
            cellStyle: {'text-align': 'center'},
            cellRendererParams: {
              label: 'Open'
            }
          } ]

 CellUpdate(params){
      
        if (params.colDef.field === "changeSettings"){
            alert("Notified Button Clicked");
   
        }
    }

创建一个组件并使用输入数据导航到它。 在组件之间转换数据的方法有很多种。

Read this article

在您的示例演示中,您应该使用 ID 定义路由并导航到它的组件,然后调用 api 来获取数据。

您也可以打开模式来显示数据。 示例:

 CellUpdate(params){
      
        if (params.colDef.field === "changeSettings"){
          const modalRef = this.modalService.open(YourEditComponent);
          modalRef.componentInstance.data= params;   
        }
    }