简体   繁体   English

Angular ag-Grid:自定义单元格渲染器,可在行中的所有可编辑单元格上打开编辑模式

[英]Angular ag-Grid: custom cell renderer that turns on edit mode over all editable cells in row

I have some problems with making custom cell renderer with menu for edit all row in one click.我在使用菜单制作自定义单元格渲染器时遇到一些问题,以便一键编辑所有行。

在此处输入图像描述

First of all I want to render menu icon, on user click the dropdown shows and I select 'edit' option (don't bother about remove option).首先,我想渲染菜单图标,在用户单击下拉菜单时显示,我 select '编辑'选项(不要担心删除选项)。 Ok so I have visual aspect of cell in the definiton of the column:好的,所以我在列的定义中有单元格的视觉方面:

columnsDefinition.push({
  headerName: '',
  cellRendererFramework: EditRendererComponent,
  cellRendererParams: {
    // params will be here
  }
});

Ok lets go to the next step.好的,让 go 进入下一步。 I need some extra params, I know how to pass static params of component, but I also need data from the row where my cell is rendered - I don't know how to pass it to the custom renderer component.我需要一些额外的参数,我知道如何传递组件的 static 参数,但我还需要渲染单元格的行中的数据 - 我不知道如何将其传递给自定义渲染器组件。

This is my EditRendererComponent:这是我的 EditRendererComponent:

export class EditRendererComponent implements ICellRendererAngularComp {
  isEditing = false;
  params: any;

  agInit(params: ICellRendererParams) {
this.params = params;
  }

  refresh(params: any): boolean {
return false;
  }

editMode() {
// we need to loop thru all rows, find the column with the renderer, and 'cancel the edit mode'.
// otherwise, we will end up with rows that has SAVE buttons appearing but not in edit mode
const renderersInOtherRows = this.params.api.getCellRendererInstances(this.params);

if( renderersInOtherRows && renderersInOtherRows.length > 0 ) {
  for(const wrapper of renderersInOtherRows) {
    if (wrapper.getFrameworkComponentInstance() instanceof EditRendererComponent ) {
      const foundRenderer = wrapper.getFrameworkComponentInstance() as EditRendererComponent;
      if( foundRenderer.isEditing ) {
        foundRenderer.onCancel();
      }
    }
  }
}
this.isEditing = true;
this.params.api.startEditingCell( { rowIndex: this.params.node.rowIndex, colKey: 'some-col-field-name'});
}

onCancel() {
this.isEditing = false;
// restore previous data or reload
}
onSave() {
this.isEditing = false;
// do your saving logic
}
}

Summary, I need help with:总结,我需要帮助:

1) Pass record data to the renderer component 1) 将记录数据传递给渲染器组件

2) editMode() method doesn't work at all, any ideas how it should be resolved to make all cells editable (only when columns def editable is set to true)? 2) editMode()方法根本不起作用,有什么想法应该如何解决以使所有单元格都可编辑(仅当columns def editable设置为true时)?

You need not pass data through cellRendererParams .您无需通过cellRendererParams传递数据。 Inside your cell renderer component you can access row data using params.在您的单元格渲染器组件中,您可以使用参数访问行数据。

  agInit(params: any): void {
    this.params = params;
    data = params.data // access row data here
  }

If you take a look at this example from docs, you need to implement your button click similar to the example.如果您从文档中查看此示例,则需要实现与示例类似的按钮单击。 The way you have it in your code should do the trick.您在代码中使用它的方式应该可以解决问题。

You just need to add [suppressClickEdit] = true to your gridOption so edit is activated only via button click.您只需将[suppressClickEdit] = true添加到您的gridOption ,以便仅通过单击按钮激活编辑。 And you are ensuring only the current row is active for editing by passing rowIndex to the startEditing api.并且您通过将rowIndex传递给startEditing api 来确保只有当前行处于活动状态以进行编辑。

Your editMode() which I assume is called on click of edit button should be really simple something like this -我假设在单击编辑按钮时调用的editMode()应该非常简单,如下所示 -

editMode() {
this.params.api.startEditingCell( { rowIndex: this.params.node.rowIndex, colKey: 'some-col-field-name'});
}

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

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