您可以只使用“cellRenderer”在 ag-grid 的单元格中实现 Dropdown。不需要“cellEditor”
@Component({
selector: 'app-project-status-management',
templateUrl: './project-status-management.component.html',
styleUrls: ['./project-status-management.component.scss']
})
export class ProjectStatusManagementComponent implements OnInit {
// Template of the Grid
@ViewChild('agGrid', { static: false }) agGrid: AgGridAngular;
// Template to the Select (Dropdown)
@ViewChild('templateCell', { static: true }) templateCell: TemplateRef<any>;
columnDefs: any;
frameworkComponents: any;
gridContext: any;
data: any[];
// Values shown on Dropdown
availableStatus: WorkflowStatus[] = [
{
workflowStatusId: 1,
name: 'InDesign',
description: 'In Design'
},
{
workflowStatusId: 3,
name: 'SourceReviewed',
description: 'Source Reviewed'
},
{
workflowStatusId: 5,
name: 'Translated',
description: 'Translated'
},
];
ngOnInit() {
this.setUpGrid();
// Set the context of the ag-grid
this.gridContext = {
availableStatus: this.availableStatus,
};
}
setUpGrid() {
// setup columns
this.columnDefs = [
{
colId: 'projectStatus',
headerName: 'Status',
field: 'workflowStatus.workflowStatusId',
cellRenderer: 'GridActionButtonCell',
cellRendererParams: {
ngTemplate: this.stringStatusCell
},
width: 170,
}];
this.frameworkComponents = {
GridActionButtonCell: GridActionButtonComponent,
};
}
}
<div>
...
<ag-grid-angular #agGrid
[rowData]="data"
[columnDefs]="columnDefs" [context]="gridContext"
[suppressRowClickSelection]="true"
...
[frameworkComponents]="frameworkComponents">
</ag-grid-angular>
...
</div>
<ng-template #templateCell let-row let-context="params">
<div class="d-flex flex-row" style="height: 100%;">
<select [(ngModel)]="row.workflowStatus.workflowStatusId">
<option *ngFor="let status of context.context.availableStatus" [ngValue]="status.workflowStatusId">{{status.description}}</option>
</select>
</div>
</ng-template>
定义一个包含元素的模板“#templateCell”。
备注:
1 - "let-row" => 网格的每个 rowData(在本例中为 this.data[i])
2 - "let-context="params" => 这是我们分配给 Grid 以便向其传递变量的上下文。在这种情况下,我们在 Context 中设置了一个名为“availableStatus”的变量:
availableStatus: WorkflowStatus[] = [
{
workflowStatusId: 1,
name: 'InDesign',
description: 'In Design'
},
{
workflowStatusId: 3,
name: 'SourceReviewed',
description: 'Source Reviewed'
},
{
workflowStatusId: 5,
name: 'Translated',
description: 'Translated'
},
];
ngOnInit() {
....
this.gridContext = {
availableStatus: this.availableStatus,
};
}