@@ -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 ( ) ) ) ;
0 commit comments