Skip to content

Commit e16d402

Browse files
committed
feat: Add extraWidth and stretch column definition properties
1 parent 26ad8e4 commit e16d402

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/UiGridAutoFitColumnsService.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import UiGridMetrics from './UiGridMetrics';
44

55
interface IExtendedColumnDef extends uiGrid.IColumnDef {
66
enableColumnAutoFit: boolean;
7+
stretch: boolean;
8+
extraWidth: number;
79
}
810

911
interface IExtendedGridColumn extends uiGrid.IGridColumn {
@@ -102,6 +104,8 @@ export class UiGridAutoFitColumnsService {
102104

103105
if (column.colDef.enableColumnAutoFit) {
104106
const columnKey = column.field || column.name;
107+
// Extra width to consider besides the text, such as from an image
108+
const extraWidth = column.colDef.extraWidth || 0;
105109
optimalWidths[columnKey] = Measurer.measureRoundedTextWidth(column.displayName, this.gridMetrics.getHeaderFont()) + this.gridMetrics.getHeaderButtonsWidth();
106110

107111
rows.forEach((row) => {
@@ -111,15 +115,21 @@ export class UiGridAutoFitColumnsService {
111115
cellText = this.getFilteredValue(cellText, column.colDef.cellFilter);
112116
}
113117

114-
const currentCellWidth = Measurer.measureRoundedTextWidth(cellText, this.gridMetrics.getCellFont());
118+
const currentCellWidth = Measurer.measureRoundedTextWidth(cellText, this.gridMetrics.getCellFont()) + extraWidth;
115119
const optimalCellWidth = currentCellWidth > 300 ? 300 : currentCellWidth;
116120

117121
if (optimalCellWidth > optimalWidths[columnKey]) {
118122
optimalWidths[columnKey] = optimalCellWidth;
119123
}
120124
});
121125

122-
column.colDef.width = optimalWidths[columnKey] + this.gridMetrics.getPadding() + this.gridMetrics.getBorder();
126+
if (column.colDef.stretch) {
127+
// If we want a column to stretch so that the grid can fill its parent container
128+
column.colDef.minWidth = optimalWidths[columnKey] + this.gridMetrics.getPadding() + this.gridMetrics.getBorder();
129+
column.colDef.width = '*';
130+
} else {
131+
column.colDef.width = optimalWidths[columnKey] + this.gridMetrics.getPadding() + this.gridMetrics.getBorder();
132+
}
123133
column.updateColumnDef(column.colDef, false);
124134
}
125135
});

0 commit comments

Comments
 (0)