I have an ag-grid table that changes its size dynamically based on some buttons. more or less columns will be added or removed from the table. However, as the number of columns increases, the table would eventually spill out of the page, which cannot be seen. I have tried to apply horizontal scrolling but to no avail.
This is my code:
<div class="row text-center" style="justify-content: center;">
<div>
<ag-grid-angular
style="font-size: 15px; width: 100%;"
class="ag-theme-balham"
[rowData]="data"
[columnDefs][="columnDefs"
[defaultColDef]="defaultColumnDefs"
(gridready)="onGridReady($event)"
</ag-grid-angular>
</div>
</div>
Basically, I want either two of the followings:
And also, I would like the table to be centralized in the middle of the page as well. Thanks!
You can try:
<ag-grid-angular
style="font-size: 15px; width: 100%;"
class="ag-theme-balham"
[rowData]="data"
[columnDefs][="columnDefs"
[defaultColDef]="defaultColumnDefs"
(gridReady)="onGridReady($event)"
(gridSizeChanged)="onGridSizeChanged($event)
</ag-grid-angular>
onGridReady(params: GridReadyEvent) {
params.api.sizeColumnsToFit();
}
onGridSizeChanged(params: GridSizeChangedEvent) {
params.api.sizeColumnsToFit();
}
I see your grid takes 100% width, make sure the parent div has a fixed width. To center the grid, I see you have done justify-content: center;
which is good but make sure this div has display: flex;
on it.
gridSizeChanged
will be called every time the grid size changes and then we call sizeColumnsToFit
so this will shrink all columns to fit with the fixed width.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.