/** * $.ld * @extends jquery.1.4.2 * @fileOverview 鍒涘缓涓€缁勮仈鍔ㄩ€夋嫨妗 * @author 鏄庢渤鍏卞奖 * @email mohaiguyan12@126.com * @site wwww.36ria.com * @version 0.2 * @date 2010-08-18 * Copyright (c) 2010-2010 鏄庢渤鍏卞奖 * @example * $(".ld-select").ld(); */ (function($){ $.fn.ld = function(options){ var opts; var DATA_NAME = "ld"; //杩斿洖API if(typeof options == 'string'){ if(options == 'api'){ return $(this).data(DATA_NAME); } } else{ var options = options || {}; //瑕嗙洊鍙傛暟 opts = $.extend(true, {}, $.fn.ld.defaults, options); } if($(this).length > 0){ var ld = new yijs.Ld(opts); ld.$applyTo = $(this); ld.render(); $(this).data(DATA_NAME,ld); } return $(this); } var yijs = yijs || {}; yijs.Ld = function(options){ //鍙傛暟 this.options = options; //璧蜂綔鐢ㄧ殑瀵硅薄 this.$applyTo = this.options.applyTo && $(this.options.applyTo) || null; //缂撳瓨鍓嶇紑 this.cachePrefix = "data_"; //鍐欏叆鍒伴€夋嫨妗嗙殑option鐨勬牱寮忓悕 this.OPTIONS_CLASS = "ld-option"; //缂撳瓨锛屼负涓€涓璞″瓧闈㈤噺銆 this.cache = {}; } yijs.Ld.prototype = { /** * 杩愯 * @return {Object} this */ render: function(){ var _that = this; var _opts = this.options; if (this.$applyTo != null && this.size() > 0) { _opts.style != null && this.css(_opts.style); //鍔犺浇榛樿鏁版嵁锛屽悜绗竴涓€夋嫨妗嗗~鍏呮暟鎹 this.load(_opts.defaultLoadSelectIndex,_opts.defaultParentId); _opts.texts.length > 0 && this.selected(_opts.texts); //缁欐瘡涓€夋嫨妗嗙粦瀹歝hange浜嬩欢 this.$applyTo.each(function(i){ // i < _that.size()-1 && $(this).bind(_opts.drevent+".ld",{target:_that,index:i},_that.onchange); }) } return this; }, texts : function(ts){ var that = this; var $select = this.$applyTo; var _arr = []; var txt = null; var $options; $select.each(function(){ txt = $(this).children('.'+that.OPTIONS_CLASS+':selected').text(); _arr.push(txt); }) return _arr; }, /** * 鑾峰彇鑱斿姩閫夋嫨妗嗙殑鏁伴噺 * @return {Number} 閫夋嫨妗嗙殑鏁伴噺 */ size : function(){ return this.$applyTo.length; }, /** * 璁剧疆閫夋嫨妗嗙殑鏍峰紡 * @param {Object} style 鏍峰紡 * @return {Object} this */ css : function(style){ style && this.$applyTo.css(style); return this; }, /** * 璇诲彇鏁版嵁锛屽苟鍐欏叆鍒伴€夋嫨妗 * @param {Number} selectIndex 閫夋嫨妗嗘暟缁勭殑绱㈠紩鍊 * @param {String} parent_id 鐖剁骇id */ load : function(selectIndex,parent_id,callback){ var _that = this; //娓呯悊index浠ヤ笅鐨勯€夋嫨妗嗙殑閫夐」 for(var i = selectIndex ; i< _that.size();i++){ _that.removeOptions(i); } //瀛樺湪缂撳瓨鏁版嵁,鐩存帴浣跨敤缂撳瓨鏁版嵁鐢熸垚閫夋嫨妗嗙殑瀛愰」锛涗笉瀛樺湪锛屽垯璇锋眰鏁版嵁 if(_that.cache[parent_id]){ _that._create(_that.cache[parent_id],selectIndex); _that.$applyTo.eq(selectIndex).trigger("afterLoad"); if(callback) callback.call(this); }else{ var _ajaxOptions = this.options.ajaxOptions; var _d = _ajaxOptions.data; var _parentIdField = this.options.field['parent_id']; _d[_parentIdField] = parent_id; //浼犻€掔粰鍚庡彴鐨勫弬鏁 _ajaxOptions.data = _d; //ajax鑾峰彇鏁版嵁鎴愬姛鍚庣殑鍥炶皟鍑芥暟 _ajaxOptions.success = function(res){ //console.log(res); var ops = res.data; //閬嶅巻鏁版嵁锛岃幏鍙杊tml瀛楃涓 if (ops.length > 0) { //鏈彍鍗曟湁鍐呭鎵嶆樉绀猴紝鍚﹁€呴殣钘?dayrui娣诲姞) _that.$applyTo.eq(selectIndex).show(); } else { _that.$applyTo.eq(selectIndex).hide(); // 璇存槑宸茬粡閫夋嫨鍒板熬閮ㄤ簡 var html = res.html; if (html.length > 0) { $('#'+_that.options.inputId).html(html); //console.log('#'+_that.options.inputId+'_select'); } } var _h = _that._getOptionsHtml(ops); _that._create(_h,selectIndex); _that.cache[parent_id] = _h; _that.$applyTo.eq(selectIndex).trigger("afterLoad.ld"); if(callback) callback.call(this); } $.ajax(_ajaxOptions); } }, /** * 鍒犻櫎鎸囧畾index绱㈠紩鍊肩殑閫夋嫨妗嗕笅鐨勯€夋嫨椤 * @param {Number} index 閫夋嫨妗嗙殑绱㈠紩鍊 * @return {Object} this */ removeOptions : function(index){ this.$applyTo.eq(index).children("."+this.OPTIONS_CLASS).remove(); return this; }, selected : function(t,completeCallBack){ var _that = this; if(t && typeof t == "object" && t.length > 0){ var $select = this.$applyTo; _load(_that.options.defaultLoadSelectIndex,_that.options.defaultParentId); } /** * 閫掑綊鑾峰彇閫夋嫨妗嗘暟鎹 * @param {Number} selectIndex 閫夋嫨妗嗙殑绱㈠紩鍊 * @param {Number} parent_id id */ function _load(selectIndex,parent_id){ _that.load(selectIndex,parent_id,function(){ var id = _selected(selectIndex,t[selectIndex]); selectIndex ++; if(selectIndex > _that.size()-1) { if(completeCallBack) completeCallBack.call(this); return; } _load(selectIndex,id); }); } /** * 閫変腑鍖呭惈鎸囧畾鏂囨湰鐨勯€夋嫨椤 * @param {Number} index 閫夋嫨妗嗙殑绱㈠紩鍊 * @param {String} text 閫夋嫨妗嗙殑value鍊 (dayrui淇敼涓烘寜id鍖归厤) * @return {Number} 璇ラ€夋嫨妗嗙殑value鍊 */ function _selected(index,text){ var id = 0; _that.$applyTo.eq(index).children().each(function(){ if(text != undefined && text.toString() == $(this).val()){ $(this).attr("selected",true); id = $(this).val(); return; } }) return id; } return this; }, /** * 閫夋嫨妗嗙殑鍊兼敼鍙樺悗瑙﹀彂鐨勪簨浠 * @param {Object} e 浜嬩欢 */ onchange : function(e){ //瀹炰緥鍖栧悗鐨勫璞″紩鐢 var _that = e.data.target; //閫夋嫨妗嗙殑绱㈠紩鍊 var index = e.data.index; //鐩爣閫夋嫨妗 var $target = $(e.target); var _parentId = $target.val(); var _i = index+1; _that.load(_i,_parentId); }, /** * 灏嗘暟鎹簮锛坖son鎴杧ml锛夎浆鎴恏tml * @param {Object} data * @return {String} html浠g爜瀛楃涓 */ _getOptionsHtml : function(data){ var _that = this; var ajaxOptions = this.options.ajaxOptions; var dataType = ajaxOptions.dataType; var field = this.options.field; var _h = ""; _h = _getOptions(data,dataType,field).join("");; /** * 鑾峰彇閫夋嫨妗嗛」html浠g爜鏁扮粍 * @param {Object | Array} data 鏁版嵁 * @param {String} dataType 鏁版嵁绫诲瀷 * @param {Object} field 瀛楁 * @return {Array} aStr */ function _getOptions(data,dataType,field){ var optionClass = _that.OPTIONS_CLASS; var aStr = []; var id,name; if (dataType == "json") { $.each(data,function(i){ id = data[i][field.region_id]; name = data[i][field.region_name]; var _option = ""; aStr.push(_option); }) }else if(dataType == "xml"){ $(data).children().children().each(function(){ id = $(this).find(field.region_id).text(); name = $(this).find(field.region_name).text(); var _option = ""; aStr.push(_option); }) } return aStr; } return _h; }, /** * 鍚戦€夋嫨妗嗘坊鍔爃tml * @param {String} _h html浠g爜 * @param {Number} index 閫夋嫨妗嗙殑绱㈠紩鍊 */ _create : function(_h,index){ var _that = this; this.removeOptions(index); this.$applyTo.eq(index).append(_h); } } $.fn.ld.defaults = { /**閫夋嫨妗嗗璞℃暟缁?/ selects : null, drevent : 'change', /**ajax閰嶇疆*/ ajaxOptions : { url : null, type : 'get', data : {}, dataType : 'json', success : function(){}, beforeSend : function(){} }, /**榛樿鐖剁骇id*/ defaultParentId : 0, /**榛樿璇诲彇鏁版嵁鐨勯€夋嫨妗?/ defaultLoadSelectIndex : 0, /**榛樿閫夋嫨妗嗕腑鐨勯€変腑椤?/ texts : [], /**閫夋嫨妗嗙殑鏍峰紡*/ style : null, inputId : null, /**閫夋嫨妗嗗€兼敼鍙樻椂鐨勫洖璋冨嚱鏁?/ change : function(){}, field : { region_id : "region_id", region_name : "region_name", parent_id : "parent_id" } } })(jQuery);