//获取连接中的指定参数
|
function getQueryString(name) {
|
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
|
var r = window.location.search.substr(1).match(reg);
|
if (r != null) {
|
return decodeURI(r[2]);
|
}
|
return null;
|
}
|
|
// 生成特定的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);
|
}
|
}
|
|
// 关闭iframe弹出层
|
function hideParentIframe() {
|
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
|
parent.layer.close(index); //再执行关闭
|
}
|
|
// 检测下拉框的必要条件是否存在
|
function checkLayuiParent(parent) {
|
var status = false;
|
var pVal = parent.val();
|
status = pVal?true:false; // 根据pVal的值返回必要条件是否存在
|
return status;
|
}
|
|
// 构造生成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;
|
}
|
};
|
|
//获取layui的form表单的值(支持input和select)
|
var LayuiForm = function(ele) {
|
this.ele = ele;
|
};
|
// 定义原型方法
|
LayuiForm.prototype = {
|
get: function(callback) {
|
var ele = $('#'+this.ele);
|
var isGood = this._checkForm(ele);
|
if(isGood) {
|
var iptsVals = this._getIpts(ele);
|
var selVals = this._getSels(ele);
|
// 获取所有的form值
|
var rs = $.extend({}, iptsVals, selVals);
|
callback(rs);
|
}
|
}
|
,_getIpts: function(ele) {
|
var ipts = ele.find('input');
|
var iptVals = {};
|
ipts.each(function() {
|
var name = $(this).attr('name');
|
if(name) {
|
iptVals[name] = $(this).val();
|
}
|
});
|
|
return iptVals;
|
}
|
,_getSels: function(ele) {
|
var sels = ele.find('select');
|
var selVals = {};
|
sels.each(function() {
|
var name = $(this).attr('name');
|
if(name) {
|
selVals[name] = $(this).val();
|
}
|
});
|
|
return selVals;
|
}
|
|
,_checkForm: function(ele) {
|
var isGood = true;
|
var ipts = ele.find('input');
|
var sels = ele.find('select');
|
// 遍历所有的input查询是否有数据不正确的
|
ipts.each(function() {
|
if($(this).hasClass('layui-form-danger')) {
|
isGood = false;
|
}
|
});
|
|
// 遍历所有的select查询是否有数据不正确的
|
sels.each(function() {
|
if($(this).hasClass('layui-form-danger')) {
|
isGood = false;
|
}
|
});
|
|
// 返回结果
|
return isGood;
|
}
|
};
|
|
// 统计数据选中个数/总个数
|
var QuantNumber = function(ele) {
|
this.ele = ele;
|
this.mol = 0;
|
this.den = 0;
|
this._init();
|
}
|
// 设置原型方法
|
QuantNumber.prototype = {
|
_init: function() {
|
this._setEleTxt();
|
}
|
,set: function(mol, den) {
|
this.mol = mol;
|
this.den = den;
|
this._setEleTxt();
|
}
|
,setMol: function(val) {
|
this.mol = val;
|
this._setEleTxt();
|
}
|
,setDen: function(val) {
|
this.den = val;
|
this._setEleTxt();
|
}
|
,_setEleTxt: function() {
|
var str = this.mol+'/'+this.den;
|
this.ele.text(str)
|
}
|
};
|
|
|
//获取电池品牌列表
|
function getBattProducers() {
|
var producers = [];
|
// 请求后台查询电池品牌
|
$.ajax({
|
type: 'post'
|
,async: false
|
,url: 'BattInfAction!serchByBattProducer'
|
,data: null
|
,dataType: 'json'
|
,success: function(res) {
|
var rs = JSON.parse(res.result);
|
// 判断查询结果
|
if(rs.code == 1) {
|
var data = rs.data;
|
// 遍历查询结果
|
for(var i=0; i<data.length; i++) {
|
var _data = data[i];
|
if(_data.BattProducer) { // 处理为空的品牌
|
producers.push(_data.BattProducer);
|
}
|
}
|
producers.push('其他');
|
}
|
}
|
,complete: function() {
|
|
}
|
});
|
|
return producers;
|
}
|
|
// 设置样式
|
var CarouselStyle = function(container) {
|
this.container = container;
|
this.contWd = 100;
|
this.contHt = 100;
|
this.ratio = 1;
|
this.init();
|
this._resize();
|
};
|
// 定义原型函数
|
CarouselStyle.prototype = {
|
init: function() {
|
this.contWd = this.container.width();
|
this.contHt = this.container.height();
|
this.ratio = this._getRatio(this.contWd, this.contHt);
|
this._setStyle();
|
}
|
,_setStyle() {
|
var _this = this;
|
var imgs = this.container.find('img');
|
console.log(imgs.length);
|
imgs.each(function() {
|
var size = $(this).data();
|
// 判断宽高是否存在
|
if(size.width && size.height) {
|
var imgRatio = _this._getRatio(size.width, size.height);
|
// 容器的比例小于当前图片的比例
|
if(_this.ratio<imgRatio) {
|
var mrt = _this._getMrt(_this.contWd, imgRatio);
|
$(this).css({
|
width: '100%'
|
,height: 'auto'
|
,'margin-top': mrt+'px'
|
});
|
}else {
|
$(this).css({
|
width: 'auto'
|
,height: '100%'
|
});
|
}
|
}
|
});
|
}
|
,_getRatio(w, h) {
|
var ratio = 0;
|
if(w && h) {
|
ratio = Number((w/h).toFixed(2));
|
}
|
|
return ratio;
|
}
|
,_getMrt: function(w, ratio) {
|
var halfHt = this.contHt/2;
|
var halfImgHt = w/ratio/2;
|
var mrt = halfHt -halfImgHt;
|
|
return mrt;
|
}
|
,_resize: function() {
|
var _this = this;
|
$(window).resize(function() {
|
_this.init();
|
});
|
}
|
};
|
|
//获取当前用户的权限
|
function getterUserPermits(){
|
var _permits = [];
|
$.ajax({
|
type: 'post',
|
async: false,
|
url: 'User_permitgroup_dataAction!FromSessiongetPermits',
|
data: null,
|
dataType: 'json',
|
success:function(data) {
|
data = JSON.parse(data.result);
|
data = JSON.parse(data);
|
_permits = data;
|
}
|
});
|
|
return _permits;
|
}
|
|
//根据权限名判断是否具有某个权限
|
function isHasPermit(_permitName,_permitList)
|
{
|
//console.info(_permitList);
|
var _isHasPermit=false;
|
//遍历_permitList根据_permitName找到该权限是否具有
|
for(var i=0;i<_permitList.length;i++)
|
{
|
//console.info(_permitList[i].permit_item_name);
|
//console.info(_permitList[i].permit_item_value);
|
if(_permitList[i].permit_item_name == _permitName && _permitList[i].permit_item_value == 1)
|
{
|
_isHasPermit = true;
|
return _isHasPermit;
|
}
|
}
|
return _isHasPermit;
|
}
|
|
// 给表格添加的行添加data属性
|
function addDataToTblTr(container, allData) {
|
var tr = container.find("tbody tr");
|
var len = allData.length;
|
// 遍历tr
|
tr.each(function(i){
|
if(i<len) {
|
$(this).data('data', allData[i]);
|
}
|
});
|
}
|
|
/**
|
* 对象依赖layui.form, layui.laytpl, layui.laydate
|
*/
|
var OptionsList = function(form, laytpl, laydate) {
|
this.list=[]; // 存储条件选择集合
|
this.form= form; // layui的form
|
this.laytpl = laytpl; // layui的laytpl
|
this.laydate = laydate; // layui的laydate
|
this.formFilter=""; // 条件列表所在容器form的lay-filter
|
this.select=""; // 条件列表所在容器的lay-filter
|
this.content=""; // 条件详细内容容器所在的DOM
|
this.isInit=false; // 标识是否已经被初始化
|
};
|
OptionsList.prototype={
|
/**
|
* 初始化内容
|
* @param array list 条件集合
|
* @param string formFilter 条件列表所在容器form的lay-filter
|
* @param string select 条件列表所在容器的lay-filter(id)
|
* @param string content 条件详细内容所在的DOM的id
|
*/
|
init: function(list, formFilter, select, content) {
|
|
this.list = list;
|
this.acList;
|
this.formFilter = formFilter;
|
this.select = select;
|
this.content = content;
|
|
// 生成内容
|
this._onCreateList();
|
|
// 添加绑定事件
|
if(!this.isInit) {
|
this.isInit=true; // 设置已经对容器进行了初始化操作
|
this._onEvent(); // 给生成内容绑定事件
|
}
|
|
},
|
// 设置下拉框的值
|
setItemAttr: function(id, attr, data) {
|
var list = this.list;
|
for(var i=0; i<list.length;i++) {
|
var _list = list[i];
|
if(_list.id == id) {
|
_list[attr] = data;
|
|
}
|
}
|
},
|
// 获取列表的值
|
getList: function() {
|
return this.list;
|
},
|
// 获取当前选中的选项
|
getAcList: function() {
|
return this.acList;
|
},
|
// 根据数据生成列表内容
|
_onCreateList: function() {
|
// 生成条件列表
|
var list = this.list;
|
var select = $("#"+this.select);
|
// 清空select
|
select.text("");
|
// 遍历list集合生成下拉内容
|
for(var i=0; i<list.length; i++) {
|
var _list = list[i];
|
var option = $("<option></option>");
|
option.val(_list.value);
|
option.text(_list.text);
|
option.data("data", _list);
|
select.append(option);
|
}
|
// 使用layui进行渲染
|
this.form.render("select", this.formFilter);
|
this._onCreateContent(list[0]);
|
},
|
// 根据数据详细信息
|
_onCreateContent: function(data) {
|
this.acList = data; // 设置被选中的值
|
var _this = this;
|
var laytpl = this.laytpl;
|
var form = this.form;
|
var contentId = this.content;
|
// 更具选中的筛选条件生成内容
|
var tpl = $('#'+data.id).html();
|
if(tpl) {
|
//console.log(data);
|
laytpl(tpl).render(data, function(html) {
|
$("#"+contentId).html(html);
|
form.render("select", data.filter);
|
// 判断当前是否为时间控件
|
if(data.timeRange) {
|
_this._onCreateTime(data);
|
}
|
});
|
}else {
|
$("#"+contentId).html("");
|
console.log("没有找到#"+data.id+"模板");
|
}
|
},
|
_onCreateTime:function(data) {
|
var laydate = this.laydate;
|
switch(data.type) {
|
case "month":
|
laydate.render({
|
elem: '#'+data.timeId
|
,type: "month"
|
,range: true
|
});
|
break;
|
default:
|
laydate.render({
|
elem: '#'+data.timeId
|
,range: true
|
});
|
break;
|
}
|
},
|
// 添加绑定事件
|
_onEvent: function() {
|
var _this = this;
|
var laytpl = this.laytpl;
|
var form = this.form;
|
var contentId = this.content;
|
// 给条件列表下拉框添加事件
|
this.form.on("select("+this.select+")", function(data) {
|
var content = _this.content;
|
var ele = $(data.elem);
|
var data = ele.find("option:selected").data("data");
|
_this._onCreateContent(data);
|
});
|
}
|
};
|
|
/**
|
* 自定义日期
|
* @author hdw
|
*/
|
var HdwDate = function() {};
|
|
/**
|
* 设置MyDate的原型函数
|
*/
|
HdwDate.prototype = {
|
/**
|
* 获取输入的年份是闰年还是普通年份
|
* @return 返回是否为闰年
|
*/
|
isLeapYear: function(year) {
|
var isLeap = false;
|
// 输入的年份能够被4整除
|
if(year%4==0) {
|
// 输入的年份不能被100整除
|
if(year%100 != 0) {
|
isLeap = true;
|
}else if(year%400 == 0){
|
isLeap = true;
|
}
|
}
|
|
return isLeap;
|
},
|
/**
|
* 根据字符串yyyy-MM获取当前月有几天
|
* @return 返回当前月有多少天
|
*/
|
getMonthDay: function(date) {
|
var dates = date.split("-");
|
var year = Number(dates[0]);
|
var month = Number(dates[1]);
|
var day = 30;
|
// 判断是否为2月
|
switch(month) {
|
case 2:
|
var isLead = this.isLeapYear(year);
|
day = isLead?29:28;
|
break;
|
case 1:
|
case 3:
|
case 5:
|
case 7:
|
case 8:
|
case 10:
|
case 12:
|
day = 31;
|
break;
|
default:
|
day = 30;
|
break;
|
}
|
return day;
|
}
|
|
};
|
// 实例化对象
|
var hdwDate = new HdwDate();
|
|
/**
|
* 根据配置生成请求WebSocket的方法
|
* @param opts
|
* @param opts.ws 地址
|
* @param ops.data 请求后台的参数
|
* @param opts.message 返回结果函数
|
* @param opts.open opts.error opts.close 回到函数
|
*/
|
var MyWebSocket = function(opts) {
|
this.opts = opts;
|
this.websocket;
|
this._init();
|
};
|
MyWebSocket.prototype={
|
_init: function() {
|
var opts = this.opts;
|
var ws = opts.ws;
|
//判断当前浏览器是否支持WebSocket
|
if ('WebSocket' in window) {
|
this.websocket = new WebSocket(ws);
|
}
|
else {
|
alert('当前浏览器 Not support websocket')
|
}
|
|
var msg = this._checkCallback(this.opts.message);
|
var onopen = this._checkCallback(this.opts.open);
|
var error = this._checkCallback(this.opts.error);
|
var close = this._checkCallback(this.opts.close);
|
var data = this.opts.data;
|
this._setWebSocket(this.websocket, msg, open, error, close, data);
|
|
//监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
|
window.onbeforeunload = function () {
|
this.websocket.close();
|
}
|
},
|
_setWebSocket: function(websocket, msg, open, error, close, data) {
|
|
//连接发生错误的回调方法
|
websocket.onerror = function () {
|
error();
|
};
|
|
//连接成功建立的回调方法
|
websocket.onopen = function () {
|
websocket.send(data);
|
}
|
|
//接收到消息的回调方法
|
websocket.onmessage = function (event) {
|
msg(event.data);
|
}
|
|
//连接关闭的回调方法
|
websocket.onclose = function () {
|
close();
|
}
|
},
|
_checkCallback: function(callback) {
|
var rs =function() {};
|
if(typeof callback == "function") {
|
rs = callback;
|
}
|
return rs;
|
}
|
};
|
|
// 向layui的下拉框中添加清空按钮
|
function ClearSelectVal(sel) {
|
this.sel = sel;
|
}
|
ClearSelectVal.prototype.init = function() {
|
var self = this;
|
var sel = this.sel.next('.layui-form-select');
|
var $input = sel.find('.layui-input');
|
var selTitle = sel.find('.layui-select-title');
|
var clear = selTitle.find('.my-layui-clear');
|
var $clear = $('<i class="fa fa-close my-layui-clear"></i>');
|
if(clear.length == 0) {
|
selTitle.append($clear);
|
}
|
$clear.click(function() {
|
$input.click();
|
$input.focus();
|
$input.val('');
|
});
|
|
};
|