Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add extraWidth and stretch column definition properties #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/UiGridAutoFitColumnsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import UiGridMetrics from './UiGridMetrics';

interface IExtendedColumnDef extends uiGrid.IColumnDef {
enableColumnAutoFit: boolean;
stretch: boolean;
extraWidth: number;
}

interface IExtendedGridColumn extends uiGrid.IGridColumn {
Expand Down Expand Up @@ -102,6 +104,8 @@ export class UiGridAutoFitColumnsService {

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

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

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

if (optimalCellWidth > optimalWidths[columnKey]) {
optimalWidths[columnKey] = optimalCellWidth;
}
});

column.colDef.width = optimalWidths[columnKey] + this.gridMetrics.getPadding() + this.gridMetrics.getBorder();
if (column.colDef.stretch) {
// If we want a column to stretch so that the grid can fill its parent container
column.colDef.minWidth = optimalWidths[columnKey] + this.gridMetrics.getPadding() + this.gridMetrics.getBorder();
column.colDef.width = '*';
} else {
column.colDef.width = optimalWidths[columnKey] + this.gridMetrics.getPadding() + this.gridMetrics.getBorder();
}
column.updateColumnDef(column.colDef, false);
}
});
Expand Down