From 6b8db8a4e41ef07981ebceb8a632336489bb0cee Mon Sep 17 00:00:00 2001 From: hdw <hdw@1> Date: 星期五, 21 十二月 2018 16:42:50 +0800 Subject: [PATCH] 添加机房省市区联动 --- gx_tieta/WebRoot/pages/js/mylayui.js | 138 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 138 insertions(+), 0 deletions(-) diff --git a/gx_tieta/WebRoot/pages/js/mylayui.js b/gx_tieta/WebRoot/pages/js/mylayui.js index 94808f7..91ecfb3 100644 --- a/gx_tieta/WebRoot/pages/js/mylayui.js +++ b/gx_tieta/WebRoot/pages/js/mylayui.js @@ -251,3 +251,141 @@ } }; +// 鐪�-甯�-鍖鸿仈鍔� +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 = []; + // 瑙f瀽鏁版嵁 + 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(); + }); + } +}; + -- Gitblit v1.9.1