// 生成特定的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); // 更新表格的数据
|
}
|
}
|
|
}
|
,_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) {
|
// 遍历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;
|
}
|
,setCache: function(cache) {
|
this.cache = cache;
|
}
|
};
|
|