使用 cellRendererFramework 添加操作按钮。
App.component.ts
columnDefs = [
{ headerName: 'First Name', field: 'firstName', sortable: true, filter: true },
{ headerName: 'Last Name', field: 'lastName', sortable: true, filter: true },
{ headerName: 'Email', field: 'email', sortable: true, filter: true },
{ headerName: 'User Name', field: 'userIdName', sortable: true, filter: true },
{ headerName: 'Role', field: 'role', sortable: true, filter: true },
{ headerName: 'Actions', field: 'action', cellRendererFramework: CellCustomComponent }];
rowData: any;
ngOnInit() {
const empDatas = localStorage.getItem('user');
const finalData = JSON.parse(this.empDatas);
this.rowData = this.finalData;
}
在上面的代码中,我们可以看到CellCustomComponent。创建该组件并添加按钮。
cell-custom.component.html
<button type="button" class="btn-success" (click)="editRow()">Edit</button>
<button type="button" class="btn-success" (click)="viewRow()">View</button>
现在在 cell-custom.component.ts 中添加函数
cell-custom.component.ts
export class CellCustomComponent implements OnInit {
data: any;
params: any;
constructor(private http: HttpClient, private router: Router) {}
agInit(params) {
this.params = params;
this.data = params.value;
}
ngOnInit() {}
editRow() {
let rowData = this.params;
let i = rowData.rowIndex;
console.log(rowData);
}
viewRow() {
let rowData = this.params;
console.log(rowData);
}
}
之后我们需要在App.module.ts
中添加这个组件
app.Module.ts
@NgModule({
declarations: [AppComponent, CellCustomComponent],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
HttpClientModule,
AgGridModule.withComponents([AppComponent])
],
providers: [],
entryComponents: [CellCustomComponent],
bootstrap: [AppComponent]
})
export class AppModule {}
现在我们可以使用 Button 触发组件文件中的方法。