Skip to content

Commit 08e4ebe

Browse files
committed
feat(resize): Add byHeader argument to the autosize method.
1 parent 7c8aa3d commit 08e4ebe

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ All notable changes for each version of this project will be documented in this
2121
- `IgxSelect` support for `igxHint` directive added.
2222
- Allows the user to add `igxHint` to be displayed bellow the input element.
2323

24+
- `IgxColumn`
25+
- Added `byHeader` parameter to the `autosize` method which specifies if the autosizing should be based only on the header content width.
26+
2427
## 9.1.1
2528

2629
### General

projects/igniteui-angular/src/lib/grids/columns/column.component.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,10 +1750,11 @@ export class IgxColumnComponent implements AfterContentInit {
17501750
* column.autosize();
17511751
* ```
17521752
* @memberof IgxColumnComponent
1753+
* @param byHeader Set if column should be autized based only on the header content
17531754
*/
1754-
public autosize() {
1755+
public autosize(byHeader = false) {
17551756
if (!this.columnGroup) {
1756-
this.width = this.getLargestCellWidth();
1757+
this.width = !byHeader ? this.getLargestCellWidth() : Object.values(this.getHeaderCellWidths()).reduce((a, b) => a + b);
17571758
this.grid.reflow();
17581759
}
17591760
}
@@ -1769,6 +1770,31 @@ export class IgxColumnComponent implements AfterContentInit {
17691770
return this._calcWidth;
17701771
}
17711772

1773+
1774+
/**
1775+
* @hidden
1776+
* Returns the width and padding of a header cell.
1777+
*/
1778+
public getHeaderCellWidths() {
1779+
const range = this.grid.document.createRange();
1780+
let headerWidth;
1781+
if (this.headerTemplate && this.headerCell.elementRef.nativeElement.children[0].children.length > 0) {
1782+
headerWidth = Math.max(...Array.from(this.headerCell.elementRef.nativeElement.children[0].children)
1783+
.map((child) => getNodeSizeViaRange(range, child)));
1784+
} else {
1785+
headerWidth = getNodeSizeViaRange(range, this.headerCell.elementRef.nativeElement.children[0]);
1786+
}
1787+
1788+
if (this.sortable || this.filterable) {
1789+
headerWidth += this.headerCell.elementRef.nativeElement.children[1].getBoundingClientRect().width;
1790+
}
1791+
1792+
const headerStyle = this.grid.document.defaultView.getComputedStyle(this.headerCell.elementRef.nativeElement);
1793+
const headerPadding = parseFloat(headerStyle.paddingLeft) + parseFloat(headerStyle.paddingRight) +
1794+
parseFloat(headerStyle.borderRightWidth);
1795+
return { width: headerWidth, padding: headerPadding};
1796+
}
1797+
17721798
/**
17731799
* @hidden
17741800
* Returns the size (in pixels) of the longest currently visible cell, including the header cell.
@@ -1801,23 +1827,8 @@ export class IgxColumnComponent implements AfterContentInit {
18011827
}
18021828

18031829
if (this.headerCell) {
1804-
let headerCell;
1805-
if (this.headerTemplate && this.headerCell.elementRef.nativeElement.children[0].children.length > 0) {
1806-
headerCell = Math.max(...Array.from(this.headerCell.elementRef.nativeElement.children[0].children)
1807-
.map((child) => getNodeSizeViaRange(range, child)));
1808-
} else {
1809-
headerCell = getNodeSizeViaRange(range, this.headerCell.elementRef.nativeElement.children[0]);
1810-
}
1811-
1812-
if (this.sortable || this.filterable) {
1813-
headerCell += this.headerCell.elementRef.nativeElement.children[1].getBoundingClientRect().width;
1814-
}
1815-
1816-
const headerStyle = this.grid.document.defaultView.getComputedStyle(this.headerCell.elementRef.nativeElement);
1817-
const headerPadding = parseFloat(headerStyle.paddingLeft) + parseFloat(headerStyle.paddingRight) +
1818-
parseFloat(headerStyle.borderRightWidth);
1819-
largest.set(headerCell, headerPadding);
1820-
1830+
const headerCellWidths = this.getHeaderCellWidths();
1831+
largest.set(headerCellWidths.width, headerCellWidths.padding);
18211832
}
18221833

18231834
const largestCell = Math.max(...Array.from(largest.keys()));

projects/igniteui-angular/src/lib/grids/grid/column-resizing.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,22 @@ describe('IgxGrid - Deferred Column Resizing #grid', () => {
599599
await wait(16);
600600
});
601601

602+
it('should autosize column programmatically based only on header.', async() => {
603+
const fixture = TestBed.createComponent(LargePinnedColGridComponent);
604+
fixture.detectChanges();
605+
606+
const column = fixture.componentInstance.grid.columnList.filter(c => c.field === 'ReleaseDate')[0];
607+
expect(column.width).toEqual('100px');
608+
609+
column.autosize(true);
610+
fixture.detectChanges();
611+
612+
expect(parseInt(column.width, 10)).toEqual(111);
613+
614+
// height/width setter rAF
615+
await wait(16);
616+
});
617+
602618
it('should autosize pinned column programmatically.', async() => {
603619
const fixture = TestBed.createComponent(LargePinnedColGridComponent);
604620
fixture.detectChanges();

0 commit comments

Comments
 (0)