fix(treeTable): 修复重载数据时,指定参数 scrollPos: 'fixed' 无效的问题 (#1892)
* 修正treeTable重载数据时,指定参数 scrollPos: 'fixed' , 无法实现保持滚动条在原有位置的问题。 * 1,把修改代码移动到Class.prototype.render方法体中; 2,把let修改为var * 修改let->var * Update src/modules/treeTable.js Co-authored-by: morning-star <26325820+Sight-wcg@users.noreply.github.com> * Update src/modules/treeTable.js Co-authored-by: morning-star <26325820+Sight-wcg@users.noreply.github.com> * 按照评审人给出的修改意见进行修改 主要修改点: 1,通过给done包装函数设置scrollPosPatch标志位,避免重复包装; 2,包装后的done函数执行完毕后,恢复原来的done函数,避免以后再次触发done函数时,仍然执行保持滚动条位置的逻辑; * 函数调用时,call->apply * refactor: 简化代码 1. 将修复代码迁移至 updateOptions 2. table done 回调第四个参数,现在可以区分 reloadData 和 renderData --------- Co-authored-by: morning-star <26325820+Sight-wcg@users.noreply.github.com>
This commit is contained in:
parent
bd1e5c31d0
commit
627dc8b738
72
examples/treeTable-test-reloadData.html
Normal file
72
examples/treeTable-test-reloadData.html
Normal file
@ -0,0 +1,72 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>树形表格 - layui</title>
|
||||
<link rel="stylesheet" href="../src/css/layui.css">
|
||||
</head>
|
||||
<body class="layui-padding-5">
|
||||
|
||||
<table class="layui-hide" id="test"></table>
|
||||
|
||||
<script type="text/html" id="TPL-treeTable-demo">
|
||||
<div class="layui-btn-container">
|
||||
<a class="layui-btn layui-btn-sm" lay-event="reloadData">重载数据</a>
|
||||
</div>
|
||||
</script>
|
||||
|
||||
|
||||
<script src="../src/layui.js"></script>
|
||||
<script>
|
||||
layui.use(['treeTable', 'dropdown', 'layer'], function(){
|
||||
var treeTable = layui.treeTable;
|
||||
var dropdown = layui.dropdown;
|
||||
var layer = layui.layer;
|
||||
|
||||
// 渲染
|
||||
var inst = treeTable.render({
|
||||
elem: '#test',
|
||||
url: './json/treeTable/demo-1.json',
|
||||
maxHeight: '700px',
|
||||
toolbar: '#TPL-treeTable-demo',
|
||||
tree: {
|
||||
customName: {
|
||||
name: 'name'
|
||||
},
|
||||
view: {
|
||||
iconLeaf: ''
|
||||
}
|
||||
},
|
||||
cols: [[
|
||||
{type: 'checkbox', fixed: 'left'},
|
||||
{type: 'numbers', fixed: 'left'},
|
||||
{field: 'id', title: 'ID', width: 145, sort: true, fixed: 'left', totalRow: '合计:'},
|
||||
{field: 'name', title: '用户名', width: 180, fixed: 'left'},
|
||||
{field: 'experience', title: '积分', width: 90, sort: true},
|
||||
{field: 'sex', title: '性别', width: 80, sort: true},
|
||||
{field: 'score', title: '评分', width: 80, sort: true},
|
||||
{field: 'city', title: '城市', width: 150},
|
||||
{field: 'description', title: '描述', minWidth: 200},
|
||||
{field: 'createTime', title: '创建时间', width: 170},
|
||||
]],
|
||||
page: true
|
||||
});
|
||||
|
||||
// 表头工具栏工具事件
|
||||
treeTable.on('toolbar('+ inst.config.id +')', function (obj) {
|
||||
var config = obj.config;
|
||||
var tableId = config.id;
|
||||
var status = treeTable.checkStatus(tableId);
|
||||
if (obj.event === "reloadData") {
|
||||
treeTable.reloadData(tableId, {
|
||||
scrollPos: 'fixed'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -1079,7 +1079,7 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
|
||||
// 耗时(接口请求+视图渲染)
|
||||
options.time = (new Date().getTime() - that.startTime) + ' ms';
|
||||
}
|
||||
done(res);
|
||||
done(res, opts.type);
|
||||
},
|
||||
error: function(e, msg){
|
||||
that.errorView('请求异常,错误提示:'+ msg);
|
||||
@ -1109,7 +1109,7 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
|
||||
type: opts.type
|
||||
}), sort();
|
||||
|
||||
done(res);
|
||||
done(res, opts.type);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -137,6 +137,14 @@ layui.define(['table'], function (exports) {
|
||||
var parseData = options.parseData;
|
||||
var done = options.done;
|
||||
|
||||
// treeTable重载数据时,会先加载显示顶层节点,然后根据重载数据前的子节点展开状态,展开相应的子节点,
|
||||
// 那么如果重载数据前有滚动条滚动在某个位子,重新加载时顶层节点如果比较少,只显示顶层节点时没有滚动条的情况下,
|
||||
// 自动展开子节点后,滚动条就会显示在顶部,无法保持在重载数据之前的位置。
|
||||
// 处理保持滚动条的问题,重载数据前记录滚动条的位置
|
||||
if(reload === 'reloadData' && thatOptionsTemp.scrollPos === 'fixed'){
|
||||
that.scrollTopCache = that.config.elem.next().find(ELEM_BODY).scrollTop();
|
||||
}
|
||||
|
||||
if (thatOptionsTemp.url) {
|
||||
// 异步加载的时候需要处理parseData进行转换
|
||||
if (!reload || (reload && parseData && !parseData.mod)) {
|
||||
@ -180,7 +188,11 @@ layui.define(['table'], function (exports) {
|
||||
options.done = function () {
|
||||
var args = arguments;
|
||||
var doneThat = this;
|
||||
var isRenderData = args[3]; // 是否是 renderData
|
||||
// undefined: 初始 render 或 reload,两者本质没有区别可以不做区分
|
||||
// 'reloadData': 重载数据
|
||||
// 'renderData': 重新渲染数据
|
||||
var renderType = args[3];
|
||||
var isRenderData = renderType === 'renderData';
|
||||
if (!isRenderData) {
|
||||
delete that.isExpandAll;
|
||||
}
|
||||
@ -206,6 +218,11 @@ layui.define(['table'], function (exports) {
|
||||
|
||||
that.renderTreeTable(tableView);
|
||||
|
||||
// 恢复滚动条位置
|
||||
if(renderType === 'reloadData' && doneThat.scrollPos === 'fixed'){
|
||||
tableView.find(ELEM_BODY).scrollTop(that.scrollTopCache);
|
||||
}
|
||||
|
||||
if (layui.type(done) === 'function') {
|
||||
return done.apply(doneThat, args);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user