@@ -1750,10 +1750,11 @@ export class IgxColumnComponent implements AfterContentInit {
1750
1750
* column.autosize();
1751
1751
* ```
1752
1752
* @memberof IgxColumnComponent
1753
+ * @param byHeader Set if column should be autized based only on the header content
1753
1754
*/
1754
- public autosize ( ) {
1755
+ public autosize ( byHeader = false ) {
1755
1756
if ( ! this . columnGroup ) {
1756
- this . width = this . getLargestCellWidth ( ) ;
1757
+ this . width = ! byHeader ? this . getLargestCellWidth ( ) : Object . values ( this . getHeaderCellWidths ( ) ) . reduce ( ( a , b ) => a + b ) ;
1757
1758
this . grid . reflow ( ) ;
1758
1759
}
1759
1760
}
@@ -1769,6 +1770,31 @@ export class IgxColumnComponent implements AfterContentInit {
1769
1770
return this . _calcWidth ;
1770
1771
}
1771
1772
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
+
1772
1798
/**
1773
1799
* @hidden
1774
1800
* Returns the size (in pixels) of the longest currently visible cell, including the header cell.
@@ -1801,23 +1827,8 @@ export class IgxColumnComponent implements AfterContentInit {
1801
1827
}
1802
1828
1803
1829
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 ) ;
1821
1832
}
1822
1833
1823
1834
const largestCell = Math . max ( ...Array . from ( largest . keys ( ) ) ) ;
0 commit comments