Angular 6 ag-grid单元格渲染器单击功能

Pon*_*314 12 typescript ag-grid angular

所以我试图设置ag-grid,我不能让一件事情起作用.我希望有一个列是行动.然后我希望它有一个链接或按钮触发组件ts文件中的方法.

对于列def,我有以下内容.我究竟做错了什么?

 {
    headerName: 'Actions',
    cellRenderer: params => {
      return `<a (click)="onEditClick("${params.data.hostname}")">Edit</a>`;
    }
 }
Run Code Online (Sandbox Code Playgroud)

Pet*_*982 13

我使用cellRenderFramework:

    {
    headerName: '', width: 30,
    cellRendererFramework: ActionCellRendererComponent,
    cellRendererParams: {
      icon: 'fa-download',
      action: this.downloadAttachmentAction
    }
  }
Run Code Online (Sandbox Code Playgroud)

而且我有自定义组件

@Component({
  selector: 'cu-download-link-cell-renderer',
  template: `
  <div class="text-center">
    <i class="fa {{params.icon}}" (click)="onClick()" aria-hidden="true"></i>
  </div>`
})
export class ActionCellRendererComponent {

    params;

    constructor() {
    }

    agInit(params): void {
        this.params = params;
        if (_.isNil(params.action)) {
            throw new Error('Missing action parameter for ActionCellRendererComponent');
       }
   }

   onClick(): void {
       this.params.action(this.params);
   }
}

export type CellAction = (params) => void;
Run Code Online (Sandbox Code Playgroud)

  • @LucasSantos 不,我确实创建了一个 cellrenderer 组件。然后,我没有使用“action”参数直接调用我的组件函数,而是从 cellrenderer 组件发出事件并在我的主组件上侦听它们 (2认同)

Jer*_*oel 7

使用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;
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我们可以看到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>
Run Code Online (Sandbox Code Playgroud)

现在在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);
  }
}
Run Code Online (Sandbox Code Playgroud)

之后我们需要在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 {}
Run Code Online (Sandbox Code Playgroud)

现在我们可以使用 Button 触发组件文件中的一个方法。

Ag-Grid Angular 7