layui 翻頁後水平滾動條位置不變

html

<div class="layui-card-body">
    <table id="layuiTable" lay-filter="layuiTable"></table>
    <div id="loadLayPage"></div>
</div>

 

js

layui.define(["table", 'layer', 'laypage'], function (e) {
    /**引入組件*/
    let table = layui.table
        , layer = layui.layer
        , layPage = layui.laypage
        , queryParam = {};


    /**加載表格 ...*/
    table.render({
        id: 'layuiTable',
        elem: '#layuiTable',
        // 表格樣式
        height: "full-190",
        skin: 'line',
        // 請求和響應參數
        url: '/system/users',
        where: queryParam,
        text: "無數據顯示",
        cols: [[
            {type: 'checkbox'},
            {field: "userCode", title: "用戶編號", hide: true},
            {field: "username", title: "用戶名", cellMinWidth: 235},
            {field: "name", title: "姓名", width: 85},
            {field: "mailAddress", title: "郵箱地址", width: 160},
            {field: "status", title: "狀態", width: 70},
            {field: "lastLoginIp", title: "最後登錄IP", width: 130},
            {field: "lastLoginTime", title: "最後登錄時間", width: 150},
            {field: "updatedTime", title: "修改時間", width: 150}
        ]],
        done: function (res, curr, count) {
            // 加載分頁 ...
            loadLayPage(res.count, res.data);
			// 設置滾動條位置
			setTableScroll(this.elem.next('.layui-table-view'), this.scroll)
        }
    });

    function loadLayPage(count, limit) {
        layPage.render({
            elem: 'loadLayPage'
            , count: count
            , curr: queryParam.page || 1
            , limit: queryParam.limit || 10
            , limits: [10, 20, 30]
            , layout: ['count', 'limit', 'prev', 'page', 'next', 'skip']
            , jump: function (obj, first) {
                queryParam.page = obj.curr;
                queryParam.limit = obj.limit;
                if (!first) {
                    reloadTable({
                        where: queryParam
                    });
                }
            }
        });
    }

    /**重新加載表格數據*/
    function reloadTable(option = {}) {
		option.scroll = getTableScroll($('#layuiTable').next('.layui-table-view'));
        table.reload("layuiTable", option);
    }

    //獲取滾動條
    function getTableScroll(tbView) {
        //var scrollTop = tbView.find('.layui-table-body').scrollTop();
        var scrollTop = 0;
        var scrollLeft = tbView.find('.layui-table-body').scrollLeft();
        return {
            scrollTop: scrollTop, scrollLeft: scrollLeft
        }
    }

    //設置滾動條
    function setTableScroll(tbView, scroll = {scrollTop: 0, scrollLeft: 0}) {
        tbView.find('.layui-table-body').scrollTop(scroll.scrollTop);
        tbView.find('.layui-table-body').scrollLeft(scroll.scrollLeft);
        table.resize('layuiTable'); //重置表格尺寸
    }

});

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章