Angular Ag-Grid: Add PrimeNg P-dropdown as html element in Ag Grid cells

寵の児 提交于 2020-08-09 13:59:22

问题


How to include basic HTML elements in Ag-Grid cells.

Below is my html PrimeNg p-dropdown in MyComponent.html

<p-dropdown [options]="cars" [(ngModel)]="selectedCar" (click)="doSomething()" [style]="{'width':'150px'}"></p-dropdown>

And ag-grid now to be used to include above p-dropdown in one of the cells

<ag-grid-angular #agGrid style="width: 100%; height: 350px;" class="ag-theme-alpine"
[gridOptions]="gridOptions"
[rowData]="rowData"
[columnDefs]="columnDefs">
</ag-grid-angular>

And below is the code in MyComponent.ts

 this.columnDefs= [{
    headerName: 'Type',
    field: 'type',
    editable: true
   },
   {
    headerName: 'DropdownColumn'
    field: 'ddValue',
    cellEditor:'agRichSelectCellEditor',
    cellEditorParams: function(params) {
    
    },
    cellRenderer: function(params) {
     'What to do here'
    }
]

How to include any HTML elements of Angular(including ng-model + click functions) in any Ag-Grid cells


回答1:


If you want complex HTML inside AgGrid the cells, then it's time to use cell renderers.

You need to define a custom component that will implement ICellRendererAngularComp interface and receive the value of a cell through the agInit method.

prime-ng-dropdown.component.ts

import { Component, OnInit } from "@angular/core";
import { ICellRendererAngularComp } from "ag-grid-angular";
import { ICellRendererParams } from "ag-grid-community";
@Component({
  selector: "app-prime-ng-dropdown",
  templateUrl: "./prime-ng-dropdown.component.html",
  styleUrls: ["./prime-ng-dropdown.component.css"]
})
export class PrimeNgDropdownComponent implements ICellRendererAngularComp {
  params: ICellRendererParams;

  cars = [
    { label: "Honda", value: "eHonda" },
    { label: "Jaguar", value: "fJaguar" },
    { label: "Mercedes", value: "gMercedes" }
  ];

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

  onChange(value) {
    this.params.data[this.params.colDef.field] = value;
  }

  refresh() {
    return true;
  }

  doSomething() {}
}

prime-ng-dropdown.component.html

<p-dropdown [options]="cars" [ngModel]="params.value" (ngModelChange)="onChange($event)"
    (click)="doSomething()" appendTo="body"
    [style]="{'width':'150px'}">
</p-dropdown>

Now that we have our component, we need to tell ag-Grid about it. All custom components should be listed in frameworkComponents configuration option. So let’s import our custom cell renderer and specify it in the configuration:

app.component.ts

frameworkComponents = {
  primeNgDropdown: PrimeNgDropdownComponent,
  ^^^^^^^^^^^^^^^
remember this framework key
};

app.component.html

<ag-grid-angular
  ...
  [frameworkComponents]="frameworkComponents"

Also, you have to pass this component to AgGridModule.withComponents call:

@NgModule({
  imports:      [ 
      ...
      AgGridModule.withComponents([PrimeNgDropdownComponent])
    ],

Finally, you only need to specify which component to use for your column through specifying framework key in cellRenderer option:

columnDefs = [
  ...
  {
    headerName: "DropdownColumn",
    field: "ddValue",
    cellRenderer: 'primeNgDropdown' <----------------- this one
  }
];

Stackblitz Example



来源:https://stackoverflow.com/questions/63102882/angular-ag-grid-add-primeng-p-dropdown-as-html-element-in-ag-grid-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!