Skip to content

Commit

Permalink
Merge pull request #857 from vusion/hotfix-v3.3.x/tableviewvirtual
Browse files Browse the repository at this point in the history
fix(TableView): 数据源更换时重置虚拟滚动状态
  • Loading branch information
myronliu347 authored Apr 17, 2024
2 parents 36fd768 + 2ae35a4 commit c2d5589
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,110 @@ exports[`u-table-view.vue Demo-sequence 1`] = `
</div>
`;

exports[`u-table-view.vue Demo-tab切换虚拟滚动状态重置 1`] = `
<div>
<div appear="square" class="root" item-width="auto" size="normal">
<div>
<span vusion-slot-name="extra"> </span>
<nav>
<span></span>
<div>
<div></div>
</div>
<span></span>
</nav>
</div>
<div>
</div>
</div>
<a class="root" hovertype="underline" tabindex="0" target="_self" vusion-slot-name-edit="text">
click reload
</a>
<div>
<span>b</span>
<span style="display: none;"></span>
<input type="text">
</div>
<div class="root" style="max-height: 600px;">
<div class="table" position="static">
<div class="head" style="top: 0px;">
<table class="root head-table" sticky-fixed="true" striped="true" style="width: 0px;">
<colgroup></colgroup>
<thead>
<tr></tr>
</thead>
</table>
</div>
<div class="body" sticky-fixed="true">
<div class="root scrollcview">
<div class="wrap">
<div>
<table class="root" sticky-fixed="true" striped="true" style="width: 0px;">
<colgroup></colgroup>
<tbody>
<tr>
<td class="center" colspan="0" vusion-slot-name="empty">
<div class="root" direction="horizontal" justify="center" layout="block" type vusion-slot-name="default">
暂无数据
</div>
</td>
</tr>
</tbody>
</table>
<div></div>
</div>
</div>
<div class="root" direction="horizontal">
<div class="thumb" style="transform: translateX(0%); webkit-transform: translateX(0%);"></div>
</div>
<div class="root" direction="vertical">
<div class="thumb" style="transform: translateY(0%); webkit-transform: translateY(0%);"></div>
</div>
</div>
</div>
</div>
<div class="root" style="display: none;">
<div class="line" style="display: none;"></div>
<div class="mask" style="display: none;"></div>
</div>
<nav class="root pagination" size="normal">
<div class="root sizer" tabindex="0">
<span>b</span>
<span>请选择</span>
<div class style="direction: ltr;" tags-overflow="collapse">
</div>
<transition-stub name="fade" style="width: 100%;">
<div class="root" style="display: none;">
<div>
<div class="root" style="direction: ltr;" vusion-slot-name="default">
10条/页
</div>
<div class="root" style="direction: ltr;" vusion-slot-name="default">
20条/页
</div>
<div class="root" style="direction: ltr;" vusion-slot-name="default">
50条/页
</div>
</div>
</div>
</transition-stub>
</div>
<a class="item-wrap" disabled="disabled">
<i class="item" role="prev"></i>
</a>
<a class="item" selected="selected">1</a>
<a class="item-wrap" disabled="disabled">
<i class="item" role="next"></i>
</a>
</nav>
<div>
<div style="display: none;">
</div>
</div>
</div>
</div>
`;

exports[`u-table-view.vue Demo-u-grid-layout 和 u-tabs 中的布局问题 1`] = `
<div appear="square" class="root" item-width="auto" size="normal">
<div>
Expand Down
147 changes: 147 additions & 0 deletions src/components/u-table-view.vue/docs/cases.md
Original file line number Diff line number Diff line change
Expand Up @@ -2837,4 +2837,151 @@ export default {
},
};
</script>
```

### tab切换虚拟滚动状态重置

``` vue
<template>
<div>
<u-tabs :value.sync="value"
:dataSource="loadTabs"
titleField="title"
valueField="value"
closableField="closable"
showScrollButtons='auto'
ref="tabs1"
@select="onSelectTab">
<template #title="current">{{ current.item && current.item.title }}title</template>
<template #content="current">{{ current.item && current.item.title }}content</template>
</u-tabs>
<u-button @click="onClickReload">click reload</u-button>
<u-input :value.sync="inputValue"></u-input>
<!-- <u-table-view striped :data-source="load1" pagination resizable :page-size="1000" ref="tableview" virtual :item-height="42" style="max-height: 600px" ellipsis>
<u-table-view-column-dynamic :data-source="loadSubList" ellipsis>
<div slot="title" slot-scope="{ columnItem }">
<u-text>{{ columnItem.name }}</u-text>
</div>
<template #cell="{ item, columnItem, index }">
<u-text>{{index}}{{ item.name }} {{ columnItem.name }}</u-text>
</template>
</u-table-view-column-dynamic>
</u-table-view> -->
<u-table-view striped :data-source="list" pagination resizable :page-size="1000" ref="tableview" virtual :item-height="42" style="max-height: 600px" ellipsis>
<u-table-view-column-dynamic :data-source="loadSubList" ellipsis>
<div slot="title" slot-scope="{ columnItem }">
<u-text>{{ columnItem.name }}</u-text>
</div>
<template #cell="{ item, columnItem, index }">
<u-text>{{index}}{{ item.name }} {{ columnItem.name }}</u-text>
</template>
</u-table-view-column-dynamic>
</u-table-view>
</div>
</template>
<script>
export default {
data() {
return {
value: 1,
inputValue: 1,
list: [],
};
},
methods: {
loadTabs() {
const data = [{
title: '标签页 1',
value: 1,
contentUrl: '/components/u-tabs/cases',
closable: true,
}, {
title: '标签页 2',
value: 2,
contentUrl: '/components/u-tabs/cases#形态',
closable: false,
}, {
title: '标签页 3',
value: 3,
contentUrl: '/components/u-tabs/cases#添加',
closable: true,
}] ;
for (let i = 0; i < 5; i++) {
const newData = data.map((item) => {
const index = Math.floor(Math.random() * data.length);
return { ...item, index, value: item.value + index}
});
data.push(...newData);
}
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(data);
}, 500);
});
},
onClickReload() {
console.log('reload');
this.$refs.tabs1.reload();
this.$refs.tableview.reload();
},
load() {
// 这里使用 Promise 和 setTimeout 模拟一个异步请求
const index = Math.random() * 20 >> 0;
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(this.tabList.map((item)=>{
const newItem = Object.assign({}, item);
newItem.title = `${item.title}${index}`;
return newItem;
}));
}, 500);
});
},
load1(params) {
const data = [{ id: '07cdcb8ed5e94cec', name: '张三', phone: '18612917895', email: '[email protected]', address: '浙江省杭州市滨江区网商路599号网易大厦', createdTime: 1464421931000, loginTime: 1527515531000 },
{ id: '5cd49be8f65c4738', name: '小明', phone: '13727160283', email: '[email protected]', address: '浙江省杭州市滨江区江虹路459号英飞特科技园', createdTime: 1520864676000, loginTime: 1552400676000 },
{ id: 'f799a0467c494601', name: '李四', phone: '18897127809', email: '[email protected]', address: '浙江省杭州市滨江区秋溢路606号西可科技园', createdTime: 1494488730000, loginTime: 1558165530000 },
{ id: '40e8ca488a1c4bce', disabled: true, name: '李华', phone: '18749261214', email: '[email protected]', address: '浙江省杭州市滨江区长河路590号东忠科技园', createdTime: 1476073921000, loginTime: 1544428081000 },
{ id: '150823cc351642b6', name: '王五', phone: '13579340020', email: '[email protected]', address: '浙江省杭州市滨江区网商路599号网易大厦二期', createdTime: 1468614726000, loginTime: 1531675926000 }];
for (let i = 0; i < 10; i++) {
const newData = data.map((item) => {
return { ...item, index: Math.floor(Math.random() * data.length) };
});
data.push(...newData);
}
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
list: data,
total: data.length,
});
}, 500);
});
},
loadSubList(params) {
let data = [{name: `一月份`, columnName:'one'}, {name: `二月份`, columnName:'tow'}];
for (let i = 0; i < 3; i++) {
const newData = data.map((item) => {
const index = Math.floor(Math.random() * data.length);
return { ...item, index, name: `${item.name}-${index}` };
});
data.push(...newData);
}
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(data);
}, 500);
});
},
onSelectTab({ item }) {
this.$refs.tableview.reload();
this.load1().then((res)=>{
this.list = res.list;
})
},
},
};
</script>
```
10 changes: 10 additions & 0 deletions src/components/u-table-view.vue/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2944,13 +2944,23 @@ export default {
}
},
handleResizeListener() {
if (this.virtual) {
this.virtualIndex = 0;
this.virtualTop = 0;
this.virtualBottom = 0;
}
const rootWidth = this.$refs.root.offsetWidth;
// 放在线性布局flex下,或者某些设置了fit-content,table-width会缓慢增长,导致表格一直动
// 如果两次width变化不大,不要重新计算每列的computedWidth等
const reComputedWidth = !this.preRootWidth || Math.abs(this.preRootWidth - rootWidth) > 8;
this.handleResize(reComputedWidth);
},
reHandleResize() {
if (this.virtual) {
this.virtualIndex = 0;
this.virtualTop = 0;
this.virtualBottom = 0;
}
this.preRootWidth = null;
this.handleResize(true);
},
Expand Down

0 comments on commit c2d5589

Please sign in to comment.