@@ -26,6 +26,16 @@ export async function reset(id) {
2626 return ;
2727 }
2828
29+ if ( table . minWidthObserver ) {
30+ table . minWidthObserver . disconnect ( ) ;
31+ table . minWidthObserver = null ;
32+ }
33+ if ( table . minWidthRaf ) {
34+ cancelAnimationFrame ( table . minWidthRaf ) ;
35+ table . minWidthRaf = null ;
36+ }
37+ table . autoColumns = [ ] ;
38+
2939 table . columns = [ ] ;
3040 table . tables = [ ] ;
3141 table . dragColumns = [ ] ;
@@ -51,6 +61,19 @@ export async function reset(id) {
5161 const left = table . body . scrollLeft
5262 table . thead . scrollTo ( left , 0 )
5363 } ) ;
64+
65+ if ( window . ResizeObserver ) {
66+ table . minWidthObserver = new ResizeObserver ( ( ) => {
67+ if ( table . minWidthRaf ) {
68+ cancelAnimationFrame ( table . minWidthRaf ) ;
69+ }
70+ table . minWidthRaf = requestAnimationFrame ( ( ) => {
71+ table . minWidthRaf = null ;
72+ applyColumnMinWidth ( table ) ;
73+ } ) ;
74+ } ) ;
75+ table . minWidthObserver . observe ( table . body ) ;
76+ }
5477 }
5578 else {
5679 table . isExcel = shim . firstElementChild . classList . contains ( 'table-excel' )
@@ -91,6 +114,10 @@ export async function reset(id) {
91114 table . loopCheckHeightHandler = requestAnimationFrame ( ( ) => check ( table ) ) ;
92115 return ;
93116 }
117+
118+ if ( table . thead ) {
119+ setColSize ( table , table . options ) ;
120+ }
94121}
95122
96123export async function switchCardView ( id ) {
@@ -113,6 +140,13 @@ const destroyTable = table => {
113140 if ( table . loopCheckHeightHandler ) {
114141 cancelAnimationFrame ( table . loopCheckHeightHandler ) ;
115142 }
143+ if ( table . minWidthObserver ) {
144+ table . minWidthObserver . disconnect ( ) ;
145+ table . minWidthObserver = null ;
146+ }
147+ if ( table . minWidthRaf ) {
148+ cancelAnimationFrame ( table . minWidthRaf ) ;
149+ }
116150 if ( table . thead ) {
117151 EventHandler . off ( table . body , 'scroll' )
118152 }
@@ -1062,6 +1096,9 @@ export async function updateTableState(id, options) {
10621096 if ( options . resetColumns ) {
10631097 resetColumns ( table , options ) ;
10641098 }
1099+ else if ( options . measureColumnMinWidth ) {
1100+ setColSize ( table , options ) ;
1101+ }
10651102
10661103 if ( options . resetColumnListPopover ) {
10671104 resetColumnListPopover ( table ) ;
@@ -1121,18 +1158,57 @@ const resetColumns = (table, options) => {
11211158}
11221159
11231160const setColSize = ( table , options ) => {
1124- var zeroWidthColumns = options . columnStates . filter ( i => i . width === null && i . visible === true ) ;
1125- zeroWidthColumns . forEach ( col => {
1126- const headerCollection = [ ...table . tables [ 0 ] . querySelectorAll ( 'thead > tr > th' ) ] ;
1161+ if ( ! table . tables || table . tables . length === 0 ) {
1162+ return ;
1163+ }
1164+ const headerCollection = [ ...table . tables [ 0 ] . querySelectorAll ( 'thead > tr > th' ) ] ;
1165+ const columnMinWidth = table . options . columnMinWidth || 0 ;
1166+ const autoColumns = [ ] ;
1167+ options . columnStates . forEach ( col => {
1168+ if ( col . width != null || col . visible === false ) {
1169+ return ;
1170+ }
11271171 const th = headerCollection . find ( i => i . getAttribute ( 'data-bb-field' ) === col . name ) ;
1128- if ( th . offsetWidth === 0 ) {
1129- const width = getCellWidth ( th ) ;
1130- const colIndex = headerCollection . indexOf ( th ) ;
1131- col . width = Math . max ( width , getColumnMaxCellWidth ( table , colIndex ) ) ;
1132- table . tables . forEach ( table => {
1133- table . querySelectorAll ( 'colgroup col' ) [ colIndex ] . style . width = `${ col . width } px` ;
1134- } ) ;
1172+ if ( th === void 0 ) {
1173+ return ;
11351174 }
1175+ const colIndex = headerCollection . indexOf ( th ) ;
1176+ const minWidth = Math . max ( getCellWidth ( th ) + getHeaderIconsWidth ( th ) , getColumnMaxCellWidth ( table , colIndex ) , columnMinWidth ) | 0 ;
1177+ autoColumns . push ( { colIndex, minWidth } ) ;
1178+ } ) ;
1179+ table . autoColumns = autoColumns ;
1180+ applyColumnMinWidth ( table ) ;
1181+ }
1182+
1183+ const getHeaderIconsWidth = th => {
1184+ let width = 0 ;
1185+ th . querySelectorAll ( '.sort-icon, .filter-icon, .toolbox-icon, .col-copy' ) . forEach ( icon => {
1186+ width += getWidth ( icon ) ;
1187+ } ) ;
1188+ return width ;
1189+ }
1190+
1191+ const applyColumnMinWidth = table => {
1192+ if ( ! table . thead || ! table . body || ! table . autoColumns || table . autoColumns . length === 0 ) {
1193+ return ;
1194+ }
1195+ setAutoColWidths ( table , true ) ;
1196+ const compact = table . body . scrollWidth > table . body . clientWidth + 1 ;
1197+ if ( ! compact ) {
1198+ setAutoColWidths ( table , false ) ;
1199+ }
1200+ }
1201+
1202+ const setAutoColWidths = ( table , apply ) => {
1203+ const colgroups = table . tables . map ( t => t . querySelectorAll ( 'colgroup col' ) ) ;
1204+ table . autoColumns . forEach ( ( { colIndex, minWidth } ) => {
1205+ const width = apply ? `${ minWidth } px` : '' ;
1206+ colgroups . forEach ( cols => {
1207+ const colEl = cols [ colIndex ] ;
1208+ if ( colEl ) {
1209+ colEl . style . width = width ;
1210+ }
1211+ } ) ;
11361212 } ) ;
11371213}
11381214
0 commit comments