@@ -1750,10 +1750,12 @@ 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 ( ) :
1758
+ ( Object . values ( this . getHeaderCellWidths ( ) ) . reduce ( ( a , b ) => a + b ) + 'px' ) ;
1757
1759
this . grid . reflow ( ) ;
1758
1760
}
1759
1761
}
@@ -1769,6 +1771,36 @@ export class IgxColumnComponent implements AfterContentInit {
1769
1771
return this . _calcWidth ;
1770
1772
}
1771
1773
1774
+
1775
+ /**
1776
+ * @hidden
1777
+ * Returns the width and padding of a header cell.
1778
+ */
1779
+ public getHeaderCellWidths ( ) {
1780
+ const range = this . grid . document . createRange ( ) ;
1781
+ let headerWidth ;
1782
+ if ( this . headerTemplate && this . headerCell . elementRef . nativeElement . children [ 0 ] . children . length > 0 ) {
1783
+ headerWidth = Math . max ( ...Array . from ( this . headerCell . elementRef . nativeElement . children [ 0 ] . children )
1784
+ . map ( ( child ) => getNodeSizeViaRange ( range , child ) ) ) ;
1785
+ } else {
1786
+ headerWidth = getNodeSizeViaRange ( range , this . headerCell . elementRef . nativeElement . children [ 0 ] ) ;
1787
+ }
1788
+
1789
+ if ( this . sortable || this . filterable ) {
1790
+ headerWidth += this . headerCell . elementRef . nativeElement . children [ 1 ] . getBoundingClientRect ( ) . width ;
1791
+ }
1792
+
1793
+ const headerStyle = this . grid . document . defaultView . getComputedStyle ( this . headerCell . elementRef . nativeElement ) ;
1794
+ const headerPadding = parseFloat ( headerStyle . paddingLeft ) + parseFloat ( headerStyle . paddingRight ) +
1795
+ parseFloat ( headerStyle . borderRightWidth ) ;
1796
+
1797
+ // Take into consideration the header group element, since column pinning applies borders to it if its not a columnGroup.
1798
+ const headerGroupStyle = this . grid . document . defaultView . getComputedStyle ( this . headerGroup . element . nativeElement ) ;
1799
+ const borderSize = ! this . parent ? parseFloat ( headerGroupStyle . borderRightWidth ) + parseFloat ( headerGroupStyle . borderLeftWidth ) : 0 ;
1800
+
1801
+ return { width : Math . ceil ( headerWidth ) , padding : Math . ceil ( headerPadding + borderSize ) } ;
1802
+ }
1803
+
1772
1804
/**
1773
1805
* @hidden
1774
1806
* Returns the size (in pixels) of the longest currently visible cell, including the header cell.
@@ -1795,29 +1827,14 @@ export class IgxColumnComponent implements AfterContentInit {
1795
1827
const index = cellsContentWidths . indexOf ( Math . max ( ...cellsContentWidths ) ) ;
1796
1828
const cellStyle = this . grid . document . defaultView . getComputedStyle ( this . cells [ index ] . nativeElement ) ;
1797
1829
const cellPadding = parseFloat ( cellStyle . paddingLeft ) + parseFloat ( cellStyle . paddingRight ) +
1798
- parseFloat ( cellStyle . borderRightWidth ) ;
1830
+ parseFloat ( cellStyle . borderLeftWidth ) + parseFloat ( cellStyle . borderRightWidth ) ;
1799
1831
1800
1832
largest . set ( Math . max ( ...cellsContentWidths ) , cellPadding ) ;
1801
1833
}
1802
1834
1803
1835
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
-
1836
+ const headerCellWidths = this . getHeaderCellWidths ( ) ;
1837
+ largest . set ( headerCellWidths . width , headerCellWidths . padding ) ;
1821
1838
}
1822
1839
1823
1840
const largestCell = Math . max ( ...Array . from ( largest . keys ( ) ) ) ;
0 commit comments