@@ -1788,10 +1788,11 @@ export class IgxColumnComponent implements AfterContentInit {
1788
1788
* column.autosize();
1789
1789
* ```
1790
1790
* @memberof IgxColumnComponent
1791
+ * @param byHeader Set if column should be autized based only on the header content
1791
1792
*/
1792
- public autosize ( ) {
1793
+ public autosize ( byHeader = false ) {
1793
1794
if ( ! this . columnGroup ) {
1794
- const size = this . getAutoSize ( ) ;
1795
+ const size = this . getAutoSize ( byHeader ) ;
1795
1796
this . width = size ;
1796
1797
this . grid . reflow ( ) ;
1797
1798
}
@@ -1800,8 +1801,9 @@ export class IgxColumnComponent implements AfterContentInit {
1800
1801
/**
1801
1802
* @hidden
1802
1803
*/
1803
- public getAutoSize ( ) {
1804
- const size = this . getLargestCellWidth ( ) ;
1804
+ public getAutoSize ( byHeader = false ) {
1805
+ const size = ! byHeader ? this . getLargestCellWidth ( ) :
1806
+ ( Object . values ( this . getHeaderCellWidths ( ) ) . reduce ( ( a , b ) => a + b ) + 'px' ) ;
1805
1807
const gridAvailableSize = this . grid . calcWidth ;
1806
1808
let newWidth ;
1807
1809
const isPercentageWidth = this . width && typeof this . width === 'string' && this . width . indexOf ( '%' ) !== - 1 ;
@@ -1825,6 +1827,36 @@ export class IgxColumnComponent implements AfterContentInit {
1825
1827
return this . _calcWidth ;
1826
1828
}
1827
1829
1830
+
1831
+ /**
1832
+ * @hidden
1833
+ * Returns the width and padding of a header cell.
1834
+ */
1835
+ public getHeaderCellWidths ( ) {
1836
+ const range = this . grid . document . createRange ( ) ;
1837
+ let headerWidth ;
1838
+ if ( this . headerTemplate && this . headerCell . elementRef . nativeElement . children [ 0 ] . children . length > 0 ) {
1839
+ headerWidth = Math . max ( ...Array . from ( this . headerCell . elementRef . nativeElement . children [ 0 ] . children )
1840
+ . map ( ( child ) => getNodeSizeViaRange ( range , child ) ) ) ;
1841
+ } else {
1842
+ headerWidth = getNodeSizeViaRange ( range , this . headerCell . elementRef . nativeElement . children [ 0 ] ) ;
1843
+ }
1844
+
1845
+ if ( this . sortable || this . filterable ) {
1846
+ headerWidth += this . headerCell . elementRef . nativeElement . children [ 1 ] . getBoundingClientRect ( ) . width ;
1847
+ }
1848
+
1849
+ const headerStyle = this . grid . document . defaultView . getComputedStyle ( this . headerCell . elementRef . nativeElement ) ;
1850
+ const headerPadding = parseFloat ( headerStyle . paddingLeft ) + parseFloat ( headerStyle . paddingRight ) +
1851
+ parseFloat ( headerStyle . borderRightWidth ) ;
1852
+
1853
+ // Take into consideration the header group element, since column pinning applies borders to it if its not a columnGroup.
1854
+ const headerGroupStyle = this . grid . document . defaultView . getComputedStyle ( this . headerGroup . element . nativeElement ) ;
1855
+ const borderSize = ! this . parent ? parseFloat ( headerGroupStyle . borderRightWidth ) + parseFloat ( headerGroupStyle . borderLeftWidth ) : 0 ;
1856
+
1857
+ return { width : Math . ceil ( headerWidth ) , padding : Math . ceil ( headerPadding + borderSize ) } ;
1858
+ }
1859
+
1828
1860
/**
1829
1861
* @hidden
1830
1862
* Returns the size (in pixels) of the longest currently visible cell, including the header cell.
@@ -1851,29 +1883,14 @@ export class IgxColumnComponent implements AfterContentInit {
1851
1883
const index = cellsContentWidths . indexOf ( Math . max ( ...cellsContentWidths ) ) ;
1852
1884
const cellStyle = this . grid . document . defaultView . getComputedStyle ( this . cells [ index ] . nativeElement ) ;
1853
1885
const cellPadding = parseFloat ( cellStyle . paddingLeft ) + parseFloat ( cellStyle . paddingRight ) +
1854
- parseFloat ( cellStyle . borderRightWidth ) ;
1886
+ parseFloat ( cellStyle . borderLeftWidth ) + parseFloat ( cellStyle . borderRightWidth ) ;
1855
1887
1856
1888
largest . set ( Math . max ( ...cellsContentWidths ) , cellPadding ) ;
1857
1889
}
1858
1890
1859
1891
if ( this . headerCell ) {
1860
- let headerCell ;
1861
- if ( this . headerTemplate && this . headerCell . elementRef . nativeElement . children [ 0 ] . children . length > 0 ) {
1862
- headerCell = Math . max ( ...Array . from ( this . headerCell . elementRef . nativeElement . children [ 0 ] . children )
1863
- . map ( ( child ) => getNodeSizeViaRange ( range , child ) ) ) ;
1864
- } else {
1865
- headerCell = getNodeSizeViaRange ( range , this . headerCell . elementRef . nativeElement . children [ 0 ] ) ;
1866
- }
1867
-
1868
- if ( this . sortable || this . filterable ) {
1869
- headerCell += this . headerCell . elementRef . nativeElement . children [ 1 ] . getBoundingClientRect ( ) . width ;
1870
- }
1871
-
1872
- const headerStyle = this . grid . document . defaultView . getComputedStyle ( this . headerCell . elementRef . nativeElement ) ;
1873
- const headerPadding = parseFloat ( headerStyle . paddingLeft ) + parseFloat ( headerStyle . paddingRight ) +
1874
- parseFloat ( headerStyle . borderRightWidth ) ;
1875
- largest . set ( headerCell , headerPadding ) ;
1876
-
1892
+ const headerCellWidths = this . getHeaderCellWidths ( ) ;
1893
+ largest . set ( headerCellWidths . width , headerCellWidths . padding ) ;
1877
1894
}
1878
1895
1879
1896
const largestCell = Math . max ( ...Array . from ( largest . keys ( ) ) ) ;
0 commit comments