11<template >
22<div class =" table-wrapper" :style =" {'margin-right': wrapperMargin}" >
3- <table v-if =" scrollable" class =" table dataTable table-head-clone" role =" grid" :class =" {
4- 'table-striped': striped,
5- 'table-bordered': bordered,
6- 'table-hover': hover,
7- }" >
3+ <table v-if =" scrollable"
4+ class =" table dataTable table-head-clone"
5+ :style =" {opacity: showClones ? 1 : 0}"
6+ :class =" {
7+ 'table-striped': striped,
8+ 'table-bordered': bordered,
9+ 'table-hover': hover,
10+ }" >
811 <thead ref =" thead-clone" >
9- <tr role = " row " >
12+ <tr >
1013 <th v-if =" selectable" class =" table-view-pf-select" aria-label =" Select all rows" >
1114 <label >
1215 <span class =" sr-only" >Select all rows</span >
6164 </table >
6265 </div >
6366
64- <table v-if =" scrollable" class =" table dataTable table-foot-clone" role =" grid" :class =" {
65- 'table-striped': striped,
66- 'table-bordered': bordered,
67- 'table-hover': hover,
68- }" >
67+ <table v-if =" scrollable"
68+ class =" table dataTable table-foot-clone"
69+ :style =" {opacity: showClones ? 1 : 0}"
70+ :class =" {
71+ 'table-striped': striped,
72+ 'table-bordered': bordered,
73+ 'table-hover': hover,
74+ }" >
6975 <tfoot v-if =" pages > 1" >
7076 <tr >
7177 <td class =" table-summary" :colspan =" columns.length + (selectable ? 1 : 0)" >
8187<script >
8288import ResizeObserver from ' resize-observer-polyfill' ;
8389import PfTableRow from ' ./TableRow.vue' ;
90+ import debounce from ' lodash/debounce' ;
8491
8592export default {
8693 name: ' pf-table' ,
@@ -120,10 +127,13 @@ export default {
120127 data () {
121128 return {
122129 wrapperOffset: 0 ,
130+ showClones: false ,
123131 };
124132 },
125133
126134 mounted () {
135+ this .syncHeaders = debounce (this .syncHeaders , 50 );
136+
127137 this .resizeObserver = new ResizeObserver ((entries ) => {
128138 if (! this .scrollable ) {
129139 return ;
@@ -142,32 +152,24 @@ export default {
142152 const cr = entry .contentRect ;
143153
144154 if (entry .target .tagName == ' THEAD' ) {
145- this .wrapperOffset = theadClone .clientWidth - cr .width ;
146- continue ;
147- }
148-
149- let i = Array .prototype .indexOf .call (entry .target .parentElement .children , entry .target );
150- const lastCol = i == entry .target .parentElement .children .length - 1 ;
151- const thClone = theadClone .firstChild .children [i];
152-
153- if (this .selectable ) {
154- i -= 1 ;
155- }
156-
157- if (i < 0 ) {
158- continue ;
159- }
160-
161- if (lastCol) {
162- thClone .style .width = ' auto' ;
155+ this .wrapperOffset = theadClone .clientWidth + this .wrapperOffset - cr .width ;
163156 } else {
164- thClone . style . width = ` ${ entry . target . clientWidth + 1 } px ` ;
157+ this . syncHeaders () ;
165158 }
166159 }
167160 });
168161
169- this .resizeObserver .observe (this .$refs .thead );
170- this .observeThead ();
162+ this .$watch (' scrollable' , () => {
163+ if (this .scrollable ) {
164+ this .showClones = false ;
165+ this .resizeObserver .observe (this .$refs .thead );
166+ this .observeThead ();
167+ } else {
168+ this .resizeObserver .disconnect ();
169+ }
170+ }, {
171+ immediate: true ,
172+ });
171173 },
172174
173175 destroyed () {
@@ -225,6 +227,34 @@ export default {
225227 this .resizeObserver .observe (row .children [i]);
226228 }
227229 },
230+
231+ syncHeaders () {
232+ if (! this .$refs [' thead-clone' ] || ! this .$refs .thead ) {
233+ return ;
234+ }
235+
236+ const ths = this .$refs .thead .firstChild .children ;
237+ const thsC = this .$refs [' thead-clone' ].firstChild .children ;
238+
239+ let i = 0 ;
240+ for (const th of ths) {
241+ if (i == 0 && this .selectable ) {
242+ i++ ;
243+ continue ;
244+ }
245+
246+ const thC = thsC[i];
247+
248+ if (i == thsC .length - 1 ) {
249+ thC .style .width = ' auto' ;
250+ } else {
251+ thC .style .width = ` ${ th .clientWidth + 1 } px` ;
252+ }
253+ i++ ;
254+ }
255+
256+ this .showClones = true ;
257+ },
228258 },
229259
230260 watch: {
0 commit comments