[英]Angular Ag-Grid: Add PrimeNg P-dropdown as html element in Ag Grid cells
How to include basic HTML elements in Ag-Grid cells.如何在 Ag-Grid 单元格中包含基本的 HTML 元素。
Below is my html PrimeNg p-dropdown in MyComponent.html
下面是我在
MyComponent.html
中的 html PrimeNg p-dropdown
<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 现在用于在其中一个单元格中包含上述 p-dropdown
<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
下面是
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如何在任何 Ag-Grid 单元格中包含 Angular 的任何 HTML 元素(包括 ng-model + 单击功能)
If you want complex HTML inside AgGrid the cells, then it's time to use cell renderers.如果您想在 AgGrid 单元格中使用复杂的 HTML,那么是时候使用单元格渲染器了。
You need to define a custom component that will implement ICellRendererAngularComp
interface and receive the value of a cell through the agInit
method.您需要定义一个自定义组件,该组件将实现
ICellRendererAngularComp
接口并通过agInit
方法接收单元格的值。
prime-ng-dropdown.component.ts 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 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.现在我们有了我们的组件,我们需要告诉 ag-Grid 它。 All custom components should be listed in
frameworkComponents
configuration option.所有自定义组件都应列在
frameworkComponents
配置选项中。 So let's import our custom cell renderer and specify it in the configuration:因此,让我们导入我们的自定义单元格渲染器并在配置中指定它:
app.component.ts app.component.ts
frameworkComponents = {
primeNgDropdown: PrimeNgDropdownComponent,
^^^^^^^^^^^^^^^
remember this framework key
};
app.component.html app.component.html
<ag-grid-angular
...
[frameworkComponents]="frameworkComponents"
Also, you have to pass this component to AgGridModule.withComponents
call:此外,您必须将此组件传递给
AgGridModule.withComponents
调用:
@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:最后,您只需要通过在 cellRenderer 选项中指定框架键来指定要用于列的组件:
columnDefs = [
...
{
headerName: "DropdownColumn",
field: "ddValue",
cellRenderer: 'primeNgDropdown' <----------------- this one
}
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.