@@ -536,24 +536,41 @@ const setExcelKeyboardListener = table => {
536536}
537537
538538const resetTableWidth = table => {
539+ const { options : { scrollWidth } } = table ;
539540 table . tables . forEach ( t => {
540541 const group = [ ...t . children ] . find ( i => i . nodeName === 'COLGROUP' )
541542 if ( group ) {
542- let width = 0 ;
543- [ ...group . children ] . forEach ( col => {
544- let colWidth = parseInt ( col . style . width ) ;
545- if ( isNaN ( colWidth ) ) {
546- colWidth = 100 ;
547- }
548- width += colWidth ;
549- } )
550- t . style . width = `${ width } px` ;
543+ const colgroup = getLastColgroup ( t , group ) ;
544+ if ( colgroup ) {
545+ colgroup . style = null ;
546+ }
547+ const width = getTableWidthByColumnGroup ( t , 100 ) ;
548+ if ( width >= t . parentElement . offsetWidth + scrollWidth ) {
549+ t . style . width = `${ width } px` ;
550+ }
551+ else {
552+ t . style . width = null ;
553+ }
551554
552555 saveColumnWidth ( table ) ;
553556 }
554557 } )
555558}
556559
560+ const getLastColgroup = ( table , group ) => {
561+ const p = table . parentElement ;
562+ if ( p ) {
563+ const length = group . children . length ;
564+ if ( p . classList . contains ( "table-fixed-header" ) ) {
565+ return group . children [ length - 2 ] ;
566+ }
567+ else {
568+ return group . children [ length - 1 ] ;
569+ }
570+ }
571+ return null ;
572+ }
573+
557574const setResizeListener = table => {
558575 if ( table . options . showColumnWidthTooltip ) {
559576 table . handlers . setResizeHandler = e => {
@@ -1047,15 +1064,33 @@ const saveColumnWidth = table => {
10471064 } ) ) ;
10481065}
10491066
1067+ const getTableWidthByColumnGroup = ( table , defaultWidth ) => {
1068+ return [ ...table . querySelectorAll ( 'colgroup col' ) ]
1069+ . map ( ( col , index ) => getColumnRenderWidth ( table , col , index , defaultWidth ) )
1070+ . reduce ( ( accumulator , val ) => accumulator + val , 0 ) ;
1071+ }
1072+
1073+ const getColumnRenderWidth = ( table , col , index , defaultWidth ) => {
1074+ let width = parseFloat ( col . style . width ) ;
1075+ if ( ! isNaN ( width ) && width > 0 ) {
1076+ return width ;
1077+ }
1078+
1079+ const header = table . querySelectorAll ( 'thead > tr:last-child > th' ) . item ( index ) ;
1080+ width = header ?. offsetWidth ?? 0 ;
1081+ if ( width > 0 ) {
1082+ return width ;
1083+ }
1084+
1085+ const row = [ ...table . querySelectorAll ( 'tbody > tr' ) ] . find ( i => ! i . classList . contains ( 'is-detail' ) ) ;
1086+ width = row ?. children . item ( index ) ?. offsetWidth ?? 0 ;
1087+ return width > 0 ? width : defaultWidth ;
1088+ }
1089+
10501090const setTableDefaultWidth = table => {
10511091 if ( table . tables . length > 0 && isVisible ( table . tables [ 0 ] ) ) {
10521092 const { scrollWidth, columnMinWidth } = table . options ;
1053- const tableWidth = [ ...table . tables [ 0 ] . querySelectorAll ( 'col' ) ]
1054- . map ( i => {
1055- const colWidth = parseFloat ( i . style . width ) ;
1056- return isNaN ( colWidth ) ? columnMinWidth : colWidth ;
1057- } )
1058- . reduce ( ( accumulator , val ) => accumulator + val , 0 ) ;
1093+ const tableWidth = getTableWidthByColumnGroup ( table . tables [ 0 ] , columnMinWidth ) ;
10591094
10601095 if ( tableWidth > table . el . offsetWidth ) {
10611096 table . tables [ 0 ] . style . setProperty ( 'width' , `${ tableWidth } px` ) ;
0 commit comments