hdw
2018-12-20 b8c1269fe493177cb0a6659fbc032193032737aa
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// 生成特定的select列表
function createLayuiSelect(ele, list) {
    // 清空内容
    ele.text('');
    // 遍历list结果集生成下拉
    for(var i=0; i<list.length; i++) {
        var _list = list[i];
        var option = $('<option></option>');    // option元素
        // 设置option的文本/属性值/data值
        option.text(_list.txt);
        option.val(_list.val);
        option.attr('selected', _list.selected);
        option.data('data', _list.data);
        Object.keys(_list.attr).forEach(function(key){
            var val = _list.attr[key];
            option.attr(key, val);
        });
        ele.append(option);
    }
}
 
// 构造生成select列表的对象数组
function getLayuiSelect(txt, val, attr, data, isSelected) {
    var obj = {
        val: '',
        txt: '',
        attr: {},
        data: {}
    };
    
    // 设置内容的值
    obj.val = val;        // option的value值
    obj.txt = txt;        // option的文本值    
    obj.attr = attr;    // option的自定义的属性
    obj.data = data;    // option的data值
    obj.selected = isSelected?true:false;
    
    // 返回构造的对象
    return obj;
}
 
//格式化时间
Date.prototype.format =function(format)
{
    var o = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(),    //day
    "h+" : this.getHours(),   //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
    "S" : this.getMilliseconds() //millisecond
    };
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
    (this.getFullYear()+"").substr(4- RegExp.$1.length));
    for(var k in o)if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,
    RegExp.$1.length==1? o[k] :
    ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
};
 
// 页面中分页信息对象
var TblPage = function() {
    this.size = 10;
    this.curr = 1;
    this.all = 0;
    this.num = 1;
};
 
// 初始化分页信息对象
TblPage.prototype.init = function() {
    this.size = 10;
    this.curr = 1;
    this.all = 0;
    this._setNum();
};
 
// 设置分页信息对象
TblPage.prototype.set = function(curr, all, size) {
    this.size = size;
    this.curr = curr;
    this.all = all;
    this._setNum();
};
TblPage.prototype.getPage = function() {
    var temp = {
        pageSize: this.size
        ,pageCurr: this.curr
        ,pageAll: this.all
    };
    
    return temp;
};
// 设置分页信息的当前页
TblPage.prototype.setCurr = function(curr) {
    this.curr = curr;
};
 
//设置分页信息的每页显示的条数
TblPage.prototype.setSize = function(size) {
    this.size = size;
    this._setNum();
};
 
//设置分页信息的总条数
TblPage.prototype.setAll = function(all) {
    this.all = all;
    this._setNum();
};
 
// 设置共有多少页
TblPage.prototype._setNum = function() {
    this.num = Math.ceil(this.all/this.size);
}
 
// 定义页面中分页元素的对象
var PagePage = function(opts) {
    this.search = '';        // 查询
    this.home = '';                // 首页
    this.pre = '';                // 上一页
    this.next = '';                // 下一页
    this.last = '';                // 尾页
    this.size = '';                // 每页显示条数
    this.num = '';                // 跳转的页数
    this.go = '';                // 页面跳转
    this.current = '';            // 当前页/总页数
    this.total = '';            // 数据总量
    this.page = opts.page;        // 分页信息
    this._initDom(opts);
    this.callback = "";
}
 
// 设置PagePage的方法
PagePage.prototype = {
    _initDom: function(opts) {
        this.search = this._getEle(opts.search);
        this.home = this._getEle(opts.home);
        this.pre = this._getEle(opts.pre);
        this.next = this._getEle(opts.next);
        this.last = this._getEle(opts.last);
        this.num = this._getEle(opts.num);
        this.go = this._getEle(opts.go);
        this.current = this._getEle(opts.current);
        this.total = this._getEle(opts.total);
        this.size = this._getEle(opts.size);
        //console.log(this.search);
        this.addEvent();
    }
    ,_getEle: function(ele) {
        //console.log(ele);
        var len = ele?ele.length:0;
        //console.log(len);
        var rsEle = len?ele:$('#PagePageTmp');
        return rsEle;
    }
    ,addEvent: function() {
        var _this = this;
        
        //  点击查询
        this.search.off('click.PagePage.event').on('click.PagePage.event', function() {
            if(_this._checkCallback()) {
                _this.callback();
            } 
        });
        
        // 点击首页
        this.home.off('click.PagePage.event').on('click.PagePage.event', function() {
            var Page = _this.page;
            if(Page.curr != 1){
                Page.setCurr(1);
                _this.callback();
            }
        });
        
        // 点击上一页
        this.pre.off('click.PagePage.event').on('click.PagePage.event', function() {
            var Page = _this.page;
            if(Page.curr > 1){
                Page.setCurr(Page.curr-1);
                _this.callback();
            }
        });
        
        // 点击下一页
        this.next.off('click.PagePage.event').on('click.PagePage.event', function() {
            var Page = _this.page;
            if(Page.num > Page.curr){
                Page.setCurr(Page.curr+1);
                _this.callback();
            }
        });
        
        // 设置每页显示条数
        this.size.off('blur.PagePage.event').on('blur.PagePage.event', function() {
            var Page = _this.page;
            var value= $(this).val();
            //当输入的数大于0时
            if(value>0){
                if(value != Page.size){
                    Page.setSize(parseInt(value));
                    Page.setCurr(1);
                }
            }else{
                //当输入非法数字时
                alert("请输入合法的数字");                        /* 请输入合法的整数 */
                $(this).val(Page.size);                    
            }
        });
        
        // 尾页
        this.last.off('click.PagePage.event').on('click.PagePage.event', function() {
            var Page = _this.page;
            if(Page.curr < Page.num){
                Page.setCurr(Page.num);
                _this.callback();
            }
        });
        
        // 跳转
        this.go.off('click.PagePage.event').on('click.PagePage.event', function() {
            var Page = _this.page;
            var tarpage= _this.num.val();
            if(tarpage > Page.num){
                Page.setCurr(Page.num);
            }else if(tarpage > 0 && tarpage!= Page.curr){
                Page.setCurr(parseInt(tarpage));
            }
            _this.callback();
        });
        
    }
    ,setCallback:function(callback) {
        this.callback = callback;
    }
    ,setPage: function(page) {
        this.page = page;
    }
    ,init:function(page, callback) {
        this.setPage(page);
        this.setCallback(callback);
        this.setVal();
    }
    ,setVal: function() {
        this.current.text(this.page.curr+'/'+this.page.num);
        this.total.text(this.page.all);
        this.size.val(this.page.size);
    }
    ,_checkCallback: function() {
        return typeof this.callback === 'function';
    }
};