Browse Source

table修复排序之后reloadData设置固定滚动条的情况下还是发生滚动条回零现象;新增table.refresh支持将cache信息重新渲染。

pull/1189/head
sunxb 2 years ago
parent
commit
21f89d1d95
  1. 58
      src/modules/table.js

58
src/modules/table.js

@ -1023,26 +1023,17 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
}
};
// 数据渲染
Class.prototype.renderData = function(opts){
// 渲染视图
Class.prototype.renderView = function (sort, type) {
var that = this;
var options = that.config;
var res = opts.res;
var curr = opts.curr;
var count = opts.count;
var sort = opts.sort;
var data = table.cache[that.key];
var data = res[options.response.dataName] || []; //列表数据
var totalRowData = res[options.response.totalRowName]; //合计行数据
var trs = [];
var trs_fixed = [];
var trs_fixed_r = [];
// 渲染视图
var render = function(){ // 后续性能提升的重点
// 同步表头父列的相关值
options.HAS_SET_COLS_PATCH || that.setColsPatch();
options.HAS_SET_COLS_PATCH = true;
@ -1052,7 +1043,7 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
}
layui.each(data, function(i1, item1){
var tds = [], tds_fixed = [], tds_fixed_r = []
,numbers = i1 + options.limit*(curr - 1) + 1; // 序号
,numbers = i1 + options.limit*(that.page - 1) + 1; // 序号
// 数组值是否为 object,如果不是,则自动转为 object
if(typeof item1 !== 'object'){
@ -1173,7 +1164,7 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
});
// 容器的滚动条位置
if(!(options.scrollPos === 'fixed' && opts.type === 'reloadData')){
if(!(options.scrollPos === 'fixed' && type === 'reloadData')){
that.layBody.scrollTop(0);
}
if(options.scrollPos === 'reset'){
@ -1199,8 +1190,23 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
that.haveInit = true;
layer.close(that.tipsIndex);
};
that.renderTotal(data, that.totalRowData); //数据合计
that.layTotal && that.layTotal.removeClass(HIDE);
}
// 数据渲染
Class.prototype.renderData = function(opts){
var that = this;
var options = that.config;
var res = opts.res;
var curr = opts.curr;
var count = opts.count;
var sort = opts.sort;
var data = res[options.response.dataName] || []; //列表数据
that.totalRowData = res[options.response.totalRowName]; //合计行数据
table.cache[that.key] = data; //记录数据
//显示隐藏合计栏
@ -1222,16 +1228,11 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
that.layFixLeft.removeClass(HIDE);
}
//如果执行初始排序
that.renderView(sort, opts.type); //渲染数据
if (sort) {
return render();
return;
}
//正常初始化数据渲染
render(); //渲染数据
that.renderTotal(data, totalRowData); //数据合计
that.layTotal && that.layTotal.removeClass(HIDE);
//同步分页状态
if(options.page){
options.page = $.extend({
@ -1447,7 +1448,8 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
res: res,
curr: that.page,
count: that.count,
sort: true
sort: true,
type: formEvent ? '' : 'reloadData' // 通过按钮触发排序会默认回滚到顶部否则根据情况处理
});
if(formEvent){
@ -2654,6 +2656,16 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports){
that.resize();
}
// 重新渲染视图
table.refresh = function (id) {
var that = getThisTable(id);
if (!that) {
return;
}
that.renderView(null, 'reloadData');
}
// 自动完成渲染
$(function(){
table.init();

Loading…
Cancel
Save