Skip to content

Commit 613920d

Browse files
author
黄书伟
committed
table 组件高度未设置或者设置的高度大于实际表格高度时,表格高度自适应
1 parent de17a36 commit 613920d

File tree

4 files changed

+50
-39
lines changed

4 files changed

+50
-39
lines changed

packages/v-table/src/frozen-columns-mixin.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
* 固定列
33
*
44
* */
5-
import utils from '../../src/utils/utils.js'
65

76
export default {
8-
computed:{
7+
computed: {
98
// 冻结的列集合
109
frozenCols(){
1110
return this.internalColumns.filter(x => x.isFrozen === true);
@@ -65,29 +64,5 @@ export default {
6564
},
6665

6766

68-
},
69-
70-
methods:{
71-
72-
// 获取滚动条高度(当没有设置高度或者设置的高度大于所有列高度之和时)
73-
setInternalHeightByFrozen(totalColumnsHeight){
74-
75-
// 包含固定列
76-
if (this.$el && this.hasFrozenColumn) {
77-
78-
this.$nextTick(x => {
79-
80-
if (this.hasBodyHorizontalScrollBar()) {
81-
82-
totalColumnsHeight += utils.getScrollbarWidth();
83-
84-
}
85-
this.internalHeight = totalColumnsHeight;
86-
})
87-
}else{
88-
89-
this.internalHeight = totalColumnsHeight;
90-
}
91-
}
9267
}
9368
}

packages/v-table/src/scroll-bar-control-mixin.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ export default {
1414
// 判断右侧区域是否有横向滚动条
1515
hasBodyHorizontalScrollBar(){
1616

17-
return this.rightViewWidth < this.totalNoFrozenColumnsWidth;
17+
if (this.$el){
1818

19-
/* var rightViewBody = this.$el.querySelector('.v-table-rightview .v-table-body'),
20-
rightViewContent = this.$el.querySelector('.v-table-rightview .v-table-body .v-table-btable');
19+
let rightViewBody = this.$el.querySelector('.v-table-rightview .v-table-body'),
20+
rightColumnsWidth = Math.round(this.totalNoFrozenColumnsWidth);
2121

22-
return rightViewBody.clientWidth + 2 < rightViewContent.clientWidth;*/
22+
return rightViewBody.clientWidth + 2 < rightColumnsWidth;
23+
}
24+
25+
return false;
2326
}
2427
}
2528

packages/v-table/src/table-resize-mixin.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,34 @@ export default {
3737
this.getResizeColumns();
3838
},
3939

40+
adjustHeight(){
41+
42+
setTimeout(x => {
43+
44+
if (!this.$el || this.isVerticalResize) {
45+
return false;
46+
}
47+
48+
var totalColumnsHeight = this.getTotalColumnsHeight(),
49+
scrollbarWidth = utils.getScrollbarWidth(),
50+
hasScrollBar = this.hasBodyHorizontalScrollBar();
51+
52+
// 当没有设置高度时计算总高度 || 设置的高度大于所有列高度之和时
53+
if (!(this.height && this.height > 0) || this.height > totalColumnsHeight) {
54+
55+
if (hasScrollBar && this.internalHeight + 2 < totalColumnsHeight + scrollbarWidth) {
56+
57+
this.internalHeight += scrollbarWidth;
58+
59+
} else if (!hasScrollBar) {
60+
61+
this.internalHeight = totalColumnsHeight;
62+
}
63+
}
64+
})
65+
66+
},
67+
4068
// 随着窗口改变表格自适应
4169
tableResize(){
4270

@@ -120,6 +148,8 @@ export default {
120148
}
121149
}
122150

151+
this.adjustHeight();
152+
123153
if (currentWidth >= initResizeWidths || differ > 0) {
124154

125155
var average = differ / this.resizeColumns.length;

packages/v-table/src/table.vue

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -780,26 +780,25 @@
780780
// 当没设置宽度和高度时动态计算
781781
initView(){
782782
783-
var self = this;
784783
// 当没有设置宽度计算总宽度
785-
if (!(self.internalWidth && self.internalWidth > 0)) {
784+
if (!(this.internalWidth && this.internalWidth > 0)) {
786785
787-
if (self.columns && self.columns.length > 0) {
788-
self.internalWidth = self.columns.reduce((total, curr) => total + curr.width, 0);
786+
if (this.columns && this.columns.length > 0) {
787+
this.internalWidth = this.columns.reduce((total, curr) => total + curr.width, 0);
789788
790789
}
791790
}
792791
793-
var totalColumnsHeight = self.getTotalColumnsHeight();
792+
var totalColumnsHeight = this.getTotalColumnsHeight();
794793
795794
// 当没有设置高度时计算总高度 || 设置的高度大于所有列高度之和时
796-
if (!(self.height && self.height > 0) || self.height > totalColumnsHeight) {
795+
if (!(this.height && this.height > 0) || this.height > totalColumnsHeight) {
797796
798-
this.setInternalHeightByFrozen(totalColumnsHeight);
797+
this.internalHeight = totalColumnsHeight;
799798
800-
} else if (self.height <= totalColumnsHeight) {
799+
} else if (this.height <= totalColumnsHeight) {
801800
802-
self.internalHeight = self.height;
801+
this.internalHeight = this.height;
803802
}
804803
},
805804
@@ -846,6 +845,8 @@
846845
},
847846
mounted(){
848847
848+
this.adjustHeight();
849+
849850
this.tableEmpty();
850851
851852
if (Array.isArray(this.tableData) && this.tableData.length > 0) {
@@ -898,6 +899,8 @@
898899
}
899900
900901
this.resize();
902+
903+
this.adjustHeight();
901904
},
902905
deep: true
903906
}

0 commit comments

Comments
 (0)