81041
2019-01-08 a65e54393692aefaa8ee9c61bd8c64d851da07a5
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
// 生成特定的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';
    }
};
 
// 省-市-区联动
var LinkAge = function(url, province, city, county, callback) {
    this.province = province;
    this.city = city;
    this.county = county;
    this.url = url;
    this.callback= callback;
    this.data = {};
    this._init();
};
LinkAge.prototype = {
    _init: function() {
        // 设置回调函数
        if(typeof this.callback === 'function') {
            this.callback = this.callback;
        }else {
            this.callback = function() {};
        }
        
        this._setData();
        this._updateDom();
        // this._addEvent();
    }
    ,_setData: function() {
        var _this = this;
        // 请求json数据
        $.ajax({
            type: 'post'
            ,async: false
            ,url: this.url
            ,data: null
            ,dataType: 'json'
            ,success: function(res) {
                _this.data = res;
            },
            error: function(res) {
                console.log(res)
            }
        });
    }
    ,_getProvinces: function() {
        var provinces = this._analyseData(this.data);
        return provinces;
    }
    ,_getCities: function(province) {
        var cities = this.data[province].child;
        var rsCities = this._analyseData(cities);
        return rsCities;
    }
    ,_getCounty: function(province, city) {
        var cities = this.data[province].child;
        var counties = cities[city].child;
        var rsCounties = this._analyseData(counties);
        return rsCounties;
    }
    ,_analyseData: function(data) {
        var rsData = [];
        // 解析数据
        Object.keys(data).forEach(function(key) {
            var _data = data[key];
            if(_data)
            var tmp = {
                id: key
                ,name: _data.name
            };
 
            if(!_data.child && _data == '市辖区') {
 
            }else {
                tmp.id = key;
                tmp.name = _data.child?_data.name:_data;
                rsData.push(tmp);
            }
            
        });
 
        return rsData;
    }
    ,_updateDom: function() {
        // 省
        var proData = this._getProvinces();
        var proOptions = this._createDom(proData);
        this.province.html(proOptions);
        
        // 市
        var citiesData = this._getCities(this.province.find('option:selected').attr('num'));
        var citiesOptions = this._createDom(citiesData);
        this.city.html(citiesOptions);
 
        // 区/县
        var countiesData = this._getCounty(this.province.find('option:selected').attr('num'), this.city.find('option:selected').attr('num'));
        var countiesOptions = this._createDom(countiesData);
        this.county.html(countiesOptions);
        
        this.callback();
    }
    ,_createDom: function(data) {
        var options = '';
        for(var i=0; i<data.length; i++) {
            var _data = data[i];
            options += '<option num="'+_data.id+'" value="'+_data.name+'">'+_data.name+'</option>';
        }
 
        return options;
    }
    ,_addEvent: function() {
        var _this = this;
        // 切换省调整市和区/县
        this.province.off('change.LinkAge.event').on('change.LinkAge.event', function() {
            var val = $(this).find('option:selected').attr('num');
            console.log(val);
            // 市
            var citiesData = _this._getCities(val);
            var citiesOptions = _this._createDom(citiesData);
            _this.city.html(citiesOptions);
 
 
            // 区/县
            var countiesData = _this._getCounty(val, _this.city.find('option:selected').attr('num'));
            var countiesOptions = _this._createDom(countiesData);
            _this.county.html(countiesOptions);
            
            _this.callback();
        });
 
        // 切换市
        this.city.off('change.LinkAge.event').on('change.LinkAge.event', function() {
            var val = $(this).find('option:selected').attr('num');
            console.log(val);
            // 区/县
            var countiesData = _this._getCounty(_this.province.find('option:selected').attr('num'), val);
            var countiesOptions = _this._createDom(countiesData);
            _this.county.html(countiesOptions);
            _this.callback();
        });
    }
};
 
//layui表格内容更新
var LayuiTbl = function(options, layui, laytpl, cache) {
    this.opts = options;
    this.cache = cache;
    this.layui = layui;
    this.tpl = laytpl;
    this.ele = options.elem;
    this.col = options.cols[0];
};
 
// 设置原型函数
LayuiTbl.prototype = {
    updateTr: function(index, data) {
        var layui = this.layui;
        var preData = this.cache[index];
        var _data = $.extend(preData||{}, data||{});
        var tds = this._getBodyTds(index);
        var tdRs = this._getFixedRTds(index);
        //console.log(_data);
        // 遍历
        var _this = this;
        layui.each(_data, function(key, value) {
            // 遍历tds的值
            tds.each(function(k) {
                var _field = $(this).data('field');
                if(key == _field) {
                    var cell = $(this).find('.layui-table-cell');
                    var content = _this._getContent(key, value, _data);
                    cell.html(content);
                }
            });
        });
        
        // 修改浮动
        layui.each(_data, function(key, value) {
            // 遍历tds的值
            tdRs.each(function(k) {
                var _field = $(this).data('field');
                if(key == _field) {
                    var cell = $(this).find('.layui-table-cell');
                    var content = _this._getContent(key, value, _data);
                    cell.html(content);
                }
            });
        });
    }
    ,updateCol: function(major, field, data) {            // 更新列的值
        // 判断当前属性是否是data的属性
        if(field in data) {
            var index = this.getIndex(major, data[major]);        // 获取需要更新的行
            if(index != undefined) {                            // 获取到
                this.updateTr(index, data);                        // 更新表格的数据
            }
        }
        
    }
    ,update: function(field, data) {
        for(var i=0; i<data.length; i++) {
            var _data = data[i];
            if(field in _data) {                                // 判断对象中是否存在标识位
                var _field = _data[field];                        // 获取标识位的值
                var index = this.getIndex(field, _field);        // 获取对象所在的行
                if(index != undefined) {                        // 判断是否获取到了行号
                    this.updateTr(index, _data);                // 更新指定的行
                }else {
                    console.log(' 第'+i+'个对象没有对象的表格行');
                    console.log(_data);
                }
            }else {
                console.log(' 第'+i+'个对象的'+field+'主键标识不存在');
            }
        }
    }
    ,_getBodyTds: function(index) {
        var layuiTableView = $(this.ele).next('.layui-table-view');
        var layuiTableBody = layuiTableView.find('.layui-table-body .layui-table tbody');
        var trList = layuiTableBody.find('tr');
 
        return trList.eq(index).find('td');
    }
    ,_getFixedRTds: function(index) {
        var layuiTableView = $(this.ele).next('.layui-table-view');
        var layuiFixedR = layuiTableView.find('.layui-table-fixed.layui-table-fixed-r .layui-table tbody');
        var trList = layuiFixedR.find('tr');
        return trList.eq(index).find('td');
    }
    ,_getContent: function(key, value, data) {
        var col = this.col;             //  所有的列
        var laytpl = this.tpl;
        var rs = '';
        var templet = '';
        // 遍历所有的列
        for(var i=0; i<col.length; i++) {
            var _col = col[i];
            // 判断当前内容是否为对应的需要更新的数据
            if(key == _col.field) {
                // 判断是否有模板
                // console.log(key+'***'+_col.field);
                // console.log(_col.templet);
                templet = _col.templet?$(_col.templet).html():_col.toolbar?$(_col.toolbar).html():typeof value == 'number'?value.toString():value;
                //console.log(templet);
                templet = templet?templet:typeof value == 'number'?value.toString():value;
                // 根据模板构成content
                laytpl(templet).render(data, function(html) {
                    rs = html;
                });
 
                return rs;
            }
        }
 
        return false;
    }
    ,getIndex: function(field, value) {        // 获取tr的下标
        // 遍历cache的值
        var cache = this.cache;
        var index = undefined;
        for(var i=0; i<cache.length; i++) {
            var _cache = cache[i];
            // console.log(_cache)
            if(field in _cache && _cache[field] == value) {
                index = i;
                break;
            }
        }
        
        return index;
    }
    ,getTr: function(field, value) {        // 获取Tr的数据
        // 遍历cache的值
        var cache = this.cache;
        var rs = {
            code: 0
            ,data: {}
            ,msg: '获取标识位'+field+',值为'+value+'对应的tr的数据失败'
        };
        // 遍历cache的值
        for(var i=0; i<cache.length; i++) {
            var _cache = cache[i];
            if(field in _cache && _cache[field] == value) {
                rs.code = 1;
                rs.data = _cache;
                rs.msg = '获取标识位'+field+',值为'+value+'对应的tr的数据成功'
                break;
            }
        }
        
        return rs;
    }
    ,setCache: function(cache) {
        this.cache = cache;
    }
};