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

时间:2018-06-22 15:04:11

标签: angular typescript ag-grid

因此,我正在尝试设置农业电网,而我无法做一件事。我想有一列动作。然后,我希望它有一个链接或按钮来触发组件ts文件中的方法。

对于列def,我具有以下内容。我在做什么错了?

<c:if test="${loginFailed eq true}">
        <div id="message">
            Wrong UserName Or Password. Please Try Again
        </div>
</c:if>

2 个答案:

答案 0 :(得分:9)

我使用cellRenderFramework:

    {
    headerName: '', width: 30,
    cellRendererFramework: ActionCellRendererComponent,
    cellRendererParams: {
      icon: 'fa-download',
      action: this.downloadAttachmentAction
    }
  }

然后我有了自定义组件

@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;

答案 1 :(得分:0)

使用 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触发组件文件中的方法。

Ag-Grid Angular 7