jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
 * Paging 组件
 * @description 基于laytpl 、laypage、layer 封装的组件
 * @author Van zheng_jinfan@126.com
 * @link http://m.zhengjinfan.cn
 * @license MIT
 * @version 1.0.1
 */
layui.define(['layer', 'laypage', 'laytpl'], function(exports) {
    "use strict";
    var $ = layui.jquery,
        layer = parent.layui.layer === undefined ? layui.layer : parent.layui.layer,
        laytpl = layui.laytpl;
 
    var Paging = function() {
        this.config = {
            url: undefined, //数据远程地址
            type: 'POST', //数据的获取方式  get or post
            elem: undefined, //内容容器
            params: {}, //获取数据时传递的额外参数
            openWait: false, //加载数据时是否显示等待框 
            tempElem: undefined, //模板容器
            tempType: 0,
            paged: true,
            pageConfig: { //参数应该为object类型
                elem: undefined,
                pageSize: 15 //分页大小
            },
            success: undefined, //type:function
            fail: undefined, //type:function
            complate: undefined, //type:function
            serverError: function(xhr, status, error) { //ajax的服务错误
                throwError("错误提示: " + xhr.status + " " + xhr.statusText);
            }
        };
    };
    /**
     * 版本号
     */
    Paging.prototype.v = '1.0.2';
 
    /**
     * 设置
     * @param {Object} options
     */
    Paging.prototype.set = function(options) {
        var that = this;
        $.extend(true, that.config, options);
        return that;
    };
    /**
     * 初始化
     * @param {Object} options
     */
    Paging.prototype.init = function(options) {
        var that = this;
        $.extend(true, that.config, options);
        var _config = that.config;
        if(_config.url === undefined) {
            throwError('Paging Error:请配置远程URL!');
        }
        if(_config.elem === undefined) {
            throwError('Paging Error:请配置参数elem!');
        }
        if($(_config.elem).length === 0) {
            throwError('Paging Error:找不到配置的容器elem!');
        }
        if(_config.tempType === 0) {
            if(_config.tempElem === undefined) {
                throwError('Paging Error:请配置参数tempElem!');
            }
            if($(_config.tempElem).length === 0) {
                throwError('Paging Error:找不到配置的容器tempElem!');
            }
        }
        if(_config.paged) {
            var _pageConfig = _config.pageConfig;
            if(_pageConfig.elem === undefined) {
                throwError('Paging Error:请配置参数pageConfig.elem!');
            }
        }
        if(_config.type.toUpperCase() !== 'GET' && _config.type.toUpperCase() !== 'POST') {
            throwError('Paging Error:type参数配置出错,只支持GET或都POST');
        }
        that.get({
            pageIndex: 1,
            pageSize: _config.pageConfig.pageSize
        });
 
        return that;
    };
    /**
     * 获取数据
     * @param {Object} options
     */
    Paging.prototype.get = function(options) {
        var that = this;
        var _config = that.config;
        var loadIndex = undefined;
        if(_config.openWait) {
            loadIndex = layer.load(2);
        }
        //默认参数
        var df = {
            pageIndex: 1,
            pageSize: _config.pageConfig.pageSize
        };
        $.extend(true, _config.params, df, options);
        $.ajax({
            type: _config.type,
            url: _config.url,
            data: _config.params,
            dataType: 'json',
            success: function(result, status, xhr) {
                if(result.code === 0) {
                    //获取模板
                    var tpl = _config.tempType === 0 ? $(_config.tempElem).html(): _config.tempElem;
                    //渲染数据
                    laytpl(tpl).render(result, function(html) {
                        $(_config.elem).html(html);
                    });
                    if(_config.paged) {
                        if(result.count === null || result.count === 0) {
                            throwError('Paging Error:请返回数据总数!');
                            return;
                        }
                        var _pageConfig = _config.pageConfig;
                        var pageSize = _pageConfig.pageSize;
                        var pages = result.count % pageSize == 0 ?
                            (result.count / pageSize) : (result.count / pageSize + 1);
 
                        var defaults = {
                            cont: $(_pageConfig.elem),
                            curr: _config.params.pageIndex,
                            pages: pages,
                            jump: function(obj, first) {
                                //得到了当前页,用于向服务端请求对应数据
                                var curr = obj.curr;
                                if(!first) {
                                    that.get({
                                        pageIndex: curr,
                                        pageSize: pageSize
                                    });
                                }
                            }
                        };
                        $.extend(defaults, _pageConfig); //参数合并
                        layui.laypage(defaults);
                    }
                    if(_config.success) {
                        _config.success(); //渲染成功
                    }
                } else {
                    if(_config.fail) {
                        _config.fail(result.msg); //获取数据失败
                    }
                }
                if(_config.complate) {
                    _config.complate(); //渲染完成
                }
                if(loadIndex !== undefined)
                    layer.close(loadIndex); //关闭等待层
            },
            error: function(xhr, status, error) {
                if(loadIndex !== undefined)
                    layer.close(loadIndex); //关闭等待层
                _config.serverError(xhr, status, error); //服务器错误
            }
        });
    };
    /**
     * 抛出一个异常错误信息
     * @param {String} msg
     */
    function throwError(msg) {
        throw new Error(msg);
    };
 
    var paging = new Paging();
    exports('paging', function(options) {
        return paging.set(options);
    });
});