jQuery插件封装-多级联动

针对频繁使用到的基于标签的多级联动,例如省市县或者其他多级类型,自动扩展,无层级限制

  1. 参数说明
参数说明是否必填
data数组对象,至少包含编码,父编码,名称必填
value默认选中对象的编码,初始化select标签选填
scope初始化一个id和name为scope参数的值的input标签,select标签联动时,设置该input标签的value为最后一级select标签的值,供外部调用选填
field编码的字段名称必填
pField父编码的字段名称必填
nameField编码对应的名称的字段名称必填
styleselect标签的style选填
classselect标签的class选填
callback回调函数,当select标签被选择时调用,返回被选中对象选填
  1. 代码

select.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<div id="typeSelect"></div>
		<script type="text/javascript" src="../js/jquery.min.js" ></script>
		<script type="text/javascript" src="../js/jquery.type-select.js" ></script>
		<script>
			var data = [{id:'1',pId:null,name:'中国'},
						{id:'101',pId:'1',name:'湖北省'},
						{id:'102',pId:'1',name:'广东省'},
						{id:'103',pId:'1',name:'湖南省'},
						{id:'10101',pId:'101',name:'武汉市'},
						{id:'10102',pId:'101',name:'宜昌市'},
						{id:'10103',pId:'101',name:'恩施市'},
						{id:'10104',pId:'101',name:'襄阳市'},
						{id:'10105',pId:'101',name:'随州市'},
						{id:'10201',pId:'102',name:'广州市'},
						{id:'10202',pId:'102',name:'东莞市'},
						{id:'10203',pId:'102',name:'佛山市'},
						{id:'1010101',pId:'10101',name:'洪山区'},
						{id:'1010102',pId:'10101',name:'江岸区'},
						{id:'1010103',pId:'10101',name:'江汉区'}];
						
			$("#typeSelect").typeSelect({
	 	          data : data,
	 	          value : "101",//默认选中湖北省
	 	          scope : "address",
	              field : "id",
	              pField : "pId",
	              nameField : "name",
	              style : "margin-left:3px;margin-top:2px;",
	              callback : function(obj){
	              	console.log(obj);
	              }
 	    	});
		</script>
	</body>
</html>

jquery.type-select.js

(function($){
	$.fn.typeSelect = function(method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(
                    arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.typeSelect');
        }
    };
    $.fn.typeSelect._default = {
        data : null,
        value : null,
        scope : null,
        field : 'id',
        pField : 'pId',
        nameField : 'name',
        style : null,
        class : null,
        callback : null
	};
    var methods = {
    	_optionsDetail : null,
        init : function(_options){
           new TypeSelectClass(_options,$(this));
        }
    };
    var TypeSelectClass = function(){
    	this.init.apply(this, arguments);
    };
    TypeSelectClass.prototype = {
		_options : null,
    	_targetDom : null,
    	init : function(_options,container){
    	   this._options = $.extend({},$.fn.typeSelect._default,_options);
    	   this._targetDom = container;
    	   this._targetDom.empty();
    	   if(_options.value && _options.value != "undefined"){
    	   	   this._initTypeSelectWithValue(_options.value,1);
    	   	   if(_options.scope){
    				var html = '<input id="'+_options.scope+'" name="'+_options.scope+'" value="'+_options.value+'" type="hidden"/>';
    				this._targetDom.prepend(html);
    		   }
    	   }else{
	    	   this._initTypeSelect();
    	   }
    	},
    	_initTypeSelect : function(){
    		var _options = this._options;
    		var _data = _options.data;
    		var _randomStrId = Math.floor(Math.random()*10000000000)+"typeSelect";
    		var html = '';
    		if(_options.scope){
    			html += '<input id="'+_options.scope+'" name="'+_options.scope+'" value="" type="hidden"/>';
    		}	
    		html += '<select id="'+_randomStrId+'" class="PUFilterSelect01 '+_options.class+'" style="'+_options.style+'">';
    		html += '<option value="">-请选择-</option>';
    		for(var i=0;i<_data.length;i++){
    			if(!_data[i][_options.pField]){
	    			html += '<option value="'+_data[i][_options.field]+'">'+_data[i][_options.nameField]+'</option>';
    			}
    		}
    		html += '</select>';
    		this._targetDom.append(html);
    		this._bindTypeSelectChanges(_randomStrId);
    	},
    	_initTypeSelectWithValue : function(value,index){
    		var _options = this._options;
    		var _data = _options.data;
    		var _value = value;
    		var _pCode = "";
    		var _obj = this._getObject(_value);
    		if(_obj){
    			_pCode = _obj[_options.pField];
    		}
    		if(index==1){
    			_pCode = value;
    		}
    		var optionHtml = '';
    		for(var i=0;i<_data.length;i++){
    			if(_pCode){
    				if(_data[i][_options.pField] == _pCode){
    					if(_value == _data[i][_options.field]){
    						optionHtml += '<option value="'+_data[i][_options.field]+'" selected="selected">'+_data[i][_options.nameField]+'</option>';
    					}else{
    						optionHtml += '<option value="'+_data[i][_options.field]+'">'+_data[i][_options.nameField]+'</option>';
    					}
    				}
    			}else{
    				if(!_data[i][_options.pField]){
    					if(_value == _data[i][_options.field]){
    						optionHtml += '<option value="'+_data[i][_options.field]+'" selected="selected">'+_data[i][_options.nameField]+'</option>';
    					}else{
    						optionHtml += '<option value="'+_data[i][_options.field]+'">'+_data[i][_options.nameField]+'</option>';
    					}
    				}
    			}
    		}
    		if(optionHtml) {
    			var _randomStrId = Math.floor(Math.random()*10000000000)+"typeSelect";
        		var html = '';
        		html += '<select id="'+_randomStrId+'" class="PUFilterSelect01 '+_options.class+'" style="'+_options.style+'">';
        		html += '<option value="">-请选择-</option>';
        		html += optionHtml;
        		html += '</select>';
        		this._targetDom.prepend(html);
        		this._bindTypeSelectChanges(_randomStrId);
    		}
    		if(_pCode){
    			index ++;
	    		this._initTypeSelectWithValue(_pCode,index);
    		}
    	},
    	_getObject : function(value){
    		var _options = this._options;
    		var _data = _options.data;
    		if(_data && _data.length > 0){
    			for(var i=0;i<_data.length;i++){
    				if(_data[i][_options.field] == value){
    					return _data[i];
    				}
    			}
    		}
    		return null;
    	},
    	_bindTypeSelectChanges : function(id){
    		var _options = this._options;
    		var _data = _options.data;
	    	var _changeDom = $("#"+id);
	    	var _this = this;
    		_changeDom.unbind("change").bind("change",function(){
	    		var _randomStrId = Math.floor(Math.random()*10000000000)+"typeSelect";
	    		_changeDom.nextAll().remove();
    			var _hasChild = false;
    			var html = '<select id="'+_randomStrId+'" class="PUFilterSelect01 '+_options.class+'" style="margin-left:10px;'+_options.style+'">';
    			html += '<option value="">-请选择-</option>';
    			for(var i=0;i<_data.length;i++){
    				if(_changeDom.val() && _changeDom.val() == _data[i][_options.pField]){
	    				html += '<option value="'+_data[i][_options.field]+'">'+_data[i][_options.nameField]+'</option>';
	    				_hasChild = true;
    				}	
    			}
    			html += '</select>';
    			if(_hasChild){
    				_this._targetDom.append(html);
    				_this._bindTypeSelectChanges(_randomStrId);
    			}
    			var _changeDomVal = _changeDom.val();
    			var _changePDom = _changeDom.prev("select");
    			if(!_changeDomVal && _changePDom.length>0){
    				$("#"+_options.scope).val(_changePDom.val());
    			}else{
    				$("#"+_options.scope).val(_changeDomVal);
    			}
    			if(_options.callback){
    				_options.callback(_this._getObject(_changeDomVal));
    			}
    		});
    	}
	};
})(jQuery)
  1. 效果图
    在这里插入图片描述
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值