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

Fix - Convert % column width to px when calculating default column width #3319

Merged
merged 7 commits into from
Jan 3, 2019
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -3687,7 +3687,14 @@ export abstract class IgxGridBaseComponent extends DisplayDensityBase implements
const columnsToSize = visibleChildColumns.length - columnsWithSetWidths.length;

const sumExistingWidths = columnsWithSetWidths
.reduce((prev, curr) => prev + parseInt(curr.width, 10), 0);
.reduce((prev, curr) => {
const colWidth = curr.width;
const widthValue = parseInt(colWidth, 10);
const currWidth = colWidth && typeof colWidth === 'string' && colWidth.indexOf('%') !== -1 ?
widthValue / 100 * computedWidth :
widthValue;
return prev + currWidth;
}, 0);

const columnWidth = !Number.isFinite(sumExistingWidths) ?
Math.max(computedWidth / columnsToSize, MINIMUM_COLUMN_WIDTH) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ describe('IgxGrid Component Tests', () => {
TestBed.configureTestingModule({
declarations: [
IgxGridDefaultRenderingComponent,
IgxGridColumnPercentageWidthComponent,
IgxGridWrappedInContComponent,
IgxGridFormattingComponent
],
Expand Down Expand Up @@ -752,6 +753,16 @@ describe('IgxGrid Component Tests', () => {
}
});
});

it('Should calculate default column width when a column has width in %', () => {
const fix = TestBed.createComponent(IgxGridColumnPercentageWidthComponent);
fix.componentInstance.initColumnsRows(5, 3);
fix.detectChanges();

const grid = fix.componentInstance.grid;
expect(grid.columns[1].width).toEqual('150');
expect(grid.columns[1].width).toEqual('150');
});
});

describe('IgxGrid - API methods', () => {
Expand Down Expand Up @@ -3232,6 +3243,20 @@ export class IgxGridDefaultRenderingComponent {
}
}

@Component({
template: `<igx-grid #grid [data]="data" [width]="'500px'" (onColumnInit)="initColumns($event)">
<igx-column *ngFor="let col of columns" [field]="col.key" [header]="col.key" [dataType]="col.dataType">
</igx-column>
</igx-grid>`
})
export class IgxGridColumnPercentageWidthComponent extends IgxGridDefaultRenderingComponent {
public initColumns(column) {
if (column.index === 0) {
column.width = '40%';
}
}
}

@Component({
template:
`<div [style.width.px]="outerWidth" [style.height.px]="outerHeight">
Expand Down