【问题标题】:Click on cell button to display angular-material 2 modal window in Ag Grid单击单元格按钮以在 Ag Grid 中显示 angular-material 2 模态窗口
【发布时间】:2018-09-09 14:22:36
【问题描述】:

我在 Angular 5 中使用 Ag Grid。这里我显示来自 api 的数据。我正在使用角材料 2 进行设计。我想在 ag-grid 中包含一个按钮,我在其中单击该按钮。单击按钮后,将打开一个角度材料模态。我正在尝试很多时间,但无法弄清楚。有什么办法解决吗?

company.component.ts

export class CompanyComponent implements OnInit {
private gridApi;
private gridColumnApi;
public columnDefs;

constructor(private httpClient: HttpClient, public snackbar: MatSnackBar, 
public dialog: MatDialog) {
this.columnDefs = [
  {
    headerName: 'Action',
    field: 'action',
    width: 150,
    suppressFilter: true,
    },
  {
    headerName: 'Id',
    field: 'id',
    filter: 'agNumberColumnFilter',
    width: 80,
    maxWidth: 100,
    suppressMenu: true
  },
  {
    headerName: 'Company Name',
    field: 'companyName',
    width: 160,
    suppressMenu: true
  },
  {
    headerName: 'Address',
    field: 'companyAddress',
    width: 160,
    suppressFilter: true,
    },
  {
    headerName: 'Phone',
    field: 'phone',
    width: 130,
    suppressMenu: true
  },
  {
    headerName: 'Fax',
    field: 'fax',
    width: 130,
    suppressMenu: true
  },
  {
    headerName: 'Email',
    field: 'email',
    width: 150,
    suppressMenu: true

  },
  {
    headerName: 'Note',
    field: 'note',
    width: 200,
    suppressFilter: true
    },
  {
    headerName: 'Activation Status',
    field: 'appConfActivationStatusStatusName',
    width: 70,
    suppressMenu: true
  }
 ];
}

getCompanyInfo(params) {
this.gridApi = params.api;
this.gridColumnApi = params.columnApi;
this.httpClient.get('http://192.168.10.208:9092/hrm/companyList')
  .subscribe(
    data => {
      params.api.setRowData(data);
    },
    msg => {
      console.error(`Error: ${msg.status} ${msg.statusText}`);
    }
  );

  }
}

company.component.html

  <ag-grid-angular
#agGrid
style="width: 100%; height: 80%;"
id="myGrid"
class="ag-theme-balham"
[columnDefs]="columnDefs"
[enableSorting]="true"
[paginationPageSize]="5"
[pagination]="true"
[enableColResize]="true"
[enableFilter]="true"
[floatingFilter]="true"
(gridReady)="getCompanyInfo($event)">
[![enter image description here][1]][1]</ag-grid-angular>

【问题讨论】:

  • 请分享您现有的代码
  • 我分享了我的代码@bugs

标签: angular angular-material2 ag-grid


【解决方案1】:

我不熟悉 ag-grid 究竟是如何工作的,this is a working demomaterial-table 在单击每个条目的第一列(1、2、3 等)时会打开一个 material-modal,你应该能够轻松将此示例扩展到您的用例。

编辑:ag-grid 版本

您可以监听rowClicked 事件来显示模态。看看这个实现:

<ag-grid-angular
    #agGrid
    style="width: 100%; height: 80%;"
    id="myGrid"
    class="ag-theme-balham"
    [columnDefs]="columnDefs"
    [enableSorting]="true"
    [paginationPageSize]="5"
    [pagination]="true"
    [enableColResize]="true"
    [enableFilter]="true"
    [floatingFilter]="true"
    (rowClicked)="openDialog()"
    (gridReady)="getCompanyInfo($event)">
 </ag-grid-angular>

export class CompanyComponent implements OnInit {
private gridApi;
private gridColumnApi;
public columnDefs;

constructor(private httpClient: HttpClient, public snackbar: MatSnackBar, 
public dialog: MatDialog) {
this.columnDefs = [
  {
    headerName: 'Action',
    field: 'action',
    width: 150,
    suppressFilter: true,
    },
  {
    headerName: 'Id',
    field: 'id',
    filter: 'agNumberColumnFilter',
    width: 80,
    maxWidth: 100,
    suppressMenu: true
  },
  {
    headerName: 'Company Name',
    field: 'companyName',
    width: 160,
    suppressMenu: true
  },
  {
    headerName: 'Address',
    field: 'companyAddress',
    width: 160,
    suppressFilter: true,
    },
  {
    headerName: 'Phone',
    field: 'phone',
    width: 130,
    suppressMenu: true
  },
  {
    headerName: 'Fax',
    field: 'fax',
    width: 130,
    suppressMenu: true
  },
  {
    headerName: 'Email',
    field: 'email',
    width: 150,
    suppressMenu: true

  },
  {
    headerName: 'Note',
    field: 'note',
    width: 200,
    suppressFilter: true
    },
  {
    headerName: 'Activation Status',
    field: 'appConfActivationStatusStatusName',
    width: 70,
    suppressMenu: true
  }
 ];
}

openDialog(): void {
    let dialogRef = this.dialog.open(DialogDataExampleDialog, {
      width: '250px'
    });
    dialogRef.afterClosed().subscribe(result => {
      console.log('The dialog was closed');
    });
  }

getCompanyInfo(params) {
this.gridApi = params.api;
this.gridColumnApi = params.columnApi;
this.httpClient.get('http://192.168.10.208:9092/hrm/companyList')
  .subscribe(
    data => {
      params.api.setRowData(data);
    },
    msg => {
      console.error(`Error: ${msg.status} ${msg.statusText}`);
    }
  );

  }
}

@Component({
  selector: 'dialog-data-example-dialog',
  template: '123',
})
export class DialogDataExampleDialog {
  constructor( @Inject(MAT_DIALOG_DATA) public data: any) { }
}

【讨论】:

  • 让我看看我是否有时间用 ag-grid 实现它
  • 我已经修改了答案以包含一个带有 ag-grid 的示例
  • 这很简单。我应该这样做的。非常感谢@bugs
  • 很高兴它有帮助。如果它解决了您的问题,请考虑将答案标记为已接受,以便其他人将来可以从中受益
  • RowClicked 并没有真正回答这个问题。如果你的行有很多按钮怎么办?我已经看到了 CellClicked 事件,您可以在其中查看单击了哪一列并进行相应处理。但是,如果您的单元格有多个按钮怎么办? Ag-Grid 并没有让这一切变得简单。
【解决方案2】:

这将对您有所帮助:

 onRowClicked(event) {
    this.dialog.open(Component);
  }

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 2016-12-10
    • 2021-03-31
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 2018-03-26
    • 2018-12-02
    相关资源
    最近更新 更多