AG-GRID 无法将图标按钮添加到我的 ag-grid 中的 ag-grid 和多选复选框的每一行

AG-GRID unable to add icon button into each row of the ag-grid and multiselection checkbox in my ag-grid

在我的农业网格的每一行中。我需要在 ag-grid 的每一行中添加 angular material 图标按钮。但是,结果只显示了文本按钮。它不会在我的农业网格中显示我的图标按钮。

接下来,我需要在我的 ag-grid 中有多个 selection 复选框。但是,在我的 ag-grid 中只能 select 一行。如何在 ag-grid 列表中实现多个 selection 行。

以下是我的源码

  constructor() { 
      this.gridOptions = <GridOptions>{
          paginationPageSize: 18,
          animateRows: true,
    };
      this.gridOptions.columnDefs = [
        {
            field: "",
            width: 110,
            checkboxSelection: true
        },
        {
            headerName: "Distributor Code",
            field: "dist_code",
            width: 330,
            sortingOrder: ["asc", "desc"]
        },
        {
            headerName: "Distributor Name",
            field: "dist_name",
            width: 330,
            sortingOrder: ["asc", "desc"]
        },
        {
            headerName: "Status",
            field: "status",
            width: 330,
            sortingOrder: ["asc", "desc"],

        },
        {
            field: "", 
            width: 110, 
            cellRenderer: function clickNextRendererFunc(){
                    return '<button mat-stroked-button><mat-icon>keyboard_arrow_right</mat-icon></button>';
            }
        }
    ];
    this.gridOptions.rowData = [
        {dist_code: 1, dist_name: "ABC Enterprise", status: "Inactive"},
        {dist_code: 2, dist_name: "Go Go Enterprise", status: "Active"},
        {dist_code: 3, dist_name: "Alibaba Enterprise", status: "Active"},
        {dist_code: 4, dist_name: "Silver Enterprise", status: "Active"},
        {dist_code: 5, dist_name: "George Enterprise", status: "Inactive"},
        {dist_code: 6, dist_name: "Kent Enterprise", status: "Active"},
        {dist_code: 7, dist_name: "NP Enterprise", status: "Active"},
        {dist_code: 8, dist_name: "ED Enterprise", status: "Inactive"},
        {dist_code: 9, dist_name: "DD Enterprise", status: "Active"},
        {dist_code: 10, dist_name: "DF Enterprise", status: "Active"},
        {dist_code: 11, dist_name: "JS Enterprise", status: "Active"},
        {dist_code: 12, dist_name: "JD Enterprise", status: "Inactive"},
        {dist_code: 13, dist_name: "KFC Enterprise", status: "Active"},
        {dist_code: 14, dist_name: "MCD Enterprise", status: "Inactive"},
        {dist_code: 15, dist_name: "AH Enterprise", status: "Active"},
        {dist_code: 16, dist_name: "PP Enterprise", status: "Active"},
        {dist_code: 17, dist_name: "KOH Enterprise", status: "Active"},
        {dist_code: 18, dist_name: "HH Enterprise", status: "Active"},
        {dist_code: 19, dist_name: "GG Enterprise", status: "Inactive"},
        {dist_code: 20, dist_name: "PP2 Enterprise", status: "Active"}
    ]

    }

以下是我的打印画面

您当前正在使用呈现的字符串,您需要的是一个组件,或者您可以使用像 this 一样处理它的自定义 class。但是,由于您要渲染 material 组件,您仍然需要编译 material 按钮。

我建议您使用 cellRendererFramework 并使用自定义组件, 创建名为 RedComponentComponent 的自定义组件并像这样渲染它。

cellRendererFramework: RedComponentComponent,//your custom component

那么,您的单元格将如下所示

      {
        headerName: "Value",
        field: "value",
        cellRendererFramework: RedComponentComponent,
        width: 100
      },

并且这个组件会有你的按钮代码,它会负责你的渲染。

我fork了他们的demo,here你可以看看

这个问题有 3 个部分。

1.启用多选:

您需要在 ag-grid-angular 元素上具有此属性:rowSelection="multiple"

2。启用选择复选框:

第一个 ColDef 保留 checkboxSelection: true,您已经在做。

3。每行添加angular material图标按钮

为此您需要使用 cellRenderer 和 return html 字符串。

cellRenderer: (data) => {
    return `<mat-icon class="mat-icon material-icons mat-icon-no-color" role="img" aria-hidden="true">
       home</mat-icon>`;
}

Since cellRenderer expects html string in return, you should not simply provide <mat-icon>home</mat-icon> as it won't get compiled.

试试这个方法:

 cellRenderer: params => {
          let eIconGui = document.createElement('span');         
            return  eIconGui.innerHTML = '<em class="material-icons">arrow</em>'  + ' ' + params.data.value;          
        }