问题
I have a table which scales to all available width even though its content is less .
Demo: "https://plnkr.co/edit/6L8bTAwkEV6R6Be4M1Qm?p=preview"
Tried to solve it by giving a parent a width
and ag-grid max-width
but as you see it has not effect. Has anyone any idea ?
Thanks in advance.
回答1:
You need to use sizeColumnsToFit()
method.
sizeColumnsToFit() Gets columns to adjust in size to fit the grid horizontally.
Official doc
Code:
onGridReady(params) {
this.gridApi = params.api;
this.gridColumnApi = params.columnApi;
this.gridApi.sizeColumnsToFit();
....
}
Modified your plnkr sample
Update: explanation of
sizeColumnsToFit
You need to remember the life cycle of the ag-grid
.
So in this case, to make sizeColumnsToFit
works - grid columns
should be defined and assigned to gridOptions
property. If you need to have a dynamic solution, don't forget to inform your grid via setColumnDefs
first, and then call the sizeColumnsToFit
method.
来源:https://stackoverflow.com/questions/52344268/ag-grid-prevent-table-to-scale-to-full-width