Ag 网格,防止 table 缩放到全宽

Ag grid, prevent table to scale to full width

我有一个 table 可以缩放到所有可用宽度,即使它的内容较少。
演示:“https://plnkr.co/edit/6L8bTAwkEV6R6Be4M1Qm?p=preview
试图通过给 parent 一个 width 和 ag-grid max-width 来解决它,但如您所见,它没有效果。有谁知道吗?
提前致谢。

您需要使用sizeColumnsToFit()方法。

sizeColumnsToFit() Gets columns to adjust in size to fit the grid horizontally.

Official doc

代码:

onGridReady(params) {
    this.gridApi = params.api;
    this.gridColumnApi = params.columnApi;

    this.gridApi.sizeColumnsToFit();
    ....
}

修改了你的plnkr sample

Update: explanation of sizeColumnsToFit

你需要记住 ag-grid 的生命周期。 因此,在这种情况下,要使 sizeColumnsToFit 有效 - 应定义网格 columns 并将其分配给 gridOptions 属性。如果您需要动态解决方案,请不要忘记先通过 setColumnDefs 通知您的网格,然后调用 sizeColumnsToFit 方法。