Skip to content

Commit 10e236e

Browse files
committed
Refactored syncronization of pf-table's fixed headers. Should be more consistent
1 parent 2235524 commit 10e236e

File tree

4 files changed

+65
-33
lines changed

4 files changed

+65
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55
### Fixed
66
- Removed incorrect fixed height in scrollable tables.
77
- Enabled flex-layout on container-flex.
8+
- Refactored syncronization of pf-table's fixed headers. Should be more consistent.
89

910
## [0.0.8] - 2017-01-25
1011
### Added

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"extract-text-webpack-plugin": "^1.0.1",
5454
"html-loader": "^0.4.4",
5555
"json-loader": "^0.5.4",
56+
"lodash": "^4.17.4",
5657
"resize-observer-polyfill": "^1.3.2",
5758
"style-loader": "^0.13.1",
5859
"uglifyjs": "^2.4.10",

src/components/Table.vue

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
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>
@@ -61,11 +64,14 @@
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)">
@@ -81,6 +87,7 @@
8187
<script>
8288
import ResizeObserver from 'resize-observer-polyfill';
8389
import PfTableRow from './TableRow.vue';
90+
import debounce from 'lodash/debounce';
8491
8592
export 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: {

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2158,7 +2158,7 @@ lodash.uniq@^4.3.0:
21582158
version "4.5.0"
21592159
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
21602160

2161-
lodash@^4.0.0, lodash@^4.2.0, lodash@^4.3.0:
2161+
lodash@^4.0.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0:
21622162
version "4.17.4"
21632163
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
21642164

0 commit comments

Comments
 (0)