| | |
| | | // 设置共有多少页
|
| | | 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)
|
| | | }
|
| | | }; |