关于easyui combobox下拉框实现多选框以及全选、全不选的实现

参考文章:http://blog.youkuaiyun.com/u012027337/article/details/53927376

实现效果如下图:

当勾选全选的时候,可以选中下列所有的选项,当取消勾选时可取消所有勾选。

废话不多说 贴代码吧:

前端代码:


//这里的id是上面的combobox的id,因为我要在点击一个按钮的之后再动态的加载出来,所以我把它单独的抽取出来了。如果需要一开始就加载数据加载方式为:
$(function(){
initCombobox(id);//id为你上面的控件id,例如我的控件id为fhry,那么我这里调用就是initCombobox(fhry);这个方法可以放在任何一个function中调用。
)
function initCombobox(id){
			var value="";
			$('#'+id).combobox({
				url: 'fhrygl/getFhyxm.action',//你要加载数据的后台链接,我这里是以一个Map<String,String>的形式返回。
				method:'post',
				panelHeight:200,
				valueField:'text',
				textField:'text',
				multiple:true, 
				queryParams : {
						xlname:$('#xltree').tree('getSelected').text,
						 whqd:selectgd
				}//参数,可根据自己的需要来修改或者不要
				formatter:function(row){
					var opts=$(this).combobox('options');
					return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField]  
				},
				onLoadSuccess:function(){
					 var opts = $(this).combobox('options');  
	                    var target = this;  
	                    var values = $(target).combobox('getValues');//获取选中的值的values  
	                    $.map(values, function (value) {  
	                        var el = opts.finder.getEl(target, value);  
	                        el.find('input.combobox-checkbox')._propAttr('checked', true);   
	                    })  
				},
				onSelect: function (row) { //选中一个选项时调用  
                    var opts = $(this).combobox('options');  
                    //获取选中的值的values  
                    var name=$("#"+id).val($(this).combobox('getValues'));
					  //当点击全选时,则勾中所有的选项					
                      if(name="全选"){
                    	var a= $("#"+id).combobox('getData');
                    	 for(var i=0;i<a.length;i++){
                    		  var el = opts.finder.getEl(this, a[i].text);  
                             el.find('input.combobox-checkbox')._propAttr('checked', true); 
                    	} 
                     }
                   //设置选中值所对应的复选框为选中状态  
                    var el = opts.finder.getEl(this, row[opts.valueField]);  
                    el.find('input.combobox-checkbox')._propAttr('checked', true);  
                },  
                onUnselect: function (row) {//不选中一个选项时调用  
                    var opts = $(this).combobox('options');  
                    //获取选中的值的values  
                    $("#"+id).val($(this).combobox('getValues')); 
					//当取消全选勾中时,则取消所有的勾选					
                    if($(this).combobox('getValues')=="全选"){
                    	var a= $("#"+id).combobox('getData');
                    	for(var i=0;i<a.length;i++){
                  		  var el = opts.finder.getEl(this, a[i].text);  
                           el.find('input.combobox-checkbox')._propAttr('checked', false); 
                  		} 
                    }
                    var el = opts.finder.getEl(this, row[opts.valueField]);  
                    el.find('input.combobox-checkbox')._propAttr('checked', false);  
                }  
            });  
				
		}






easyui combobox下拉框默认是单的,如果要实现,需要做以下修改: 1.combobox的options中添加multiple:true,表示启用多模式。 2.combobox的options中添加onSelect和onUnselect两个事件,用于在择和取消择时更新中的值。 3.combobox的panel中添加checkbox或者复框,用于择多个项。 以下是实现的示例代码: HTML代码: ``` <input id="cc" class="easyui-combobox" style="width:200px;"> ``` JavaScript代码: ``` $('#cc').combobox({ url: 'get_data.php', valueField: 'id', textField: 'text', multiple: true, panelHeight: 'auto', onSelect: function(record){ var value = $(this).combobox('getValues'); value.push(record.id); $(this).combobox('setValues', value); }, onUnselect: function(record){ var value = $(this).combobox('getValues'); var index = value.indexOf(record.id); if (index >= 0){ value.splice(index, 1); } $(this).combobox('setValues', value); }, onLoadSuccess: function(){ var data = $(this).combobox('getData'); var value = $(this).combobox('getValues'); var panel = $(this).combobox('panel'); panel.find('.combo-panel-checkbox').remove(); $.each(data, function(index, item){ var checkbox = $('<input type="checkbox" class="combo-panel-checkbox">').val(item.id); if (value.indexOf(item.id) >= 0){ checkbox.prop('checked', true); } checkbox.insertBefore(panel.find('.combo-panel-list')); }); }, onShowPanel: function(){ var data = $(this).combobox('getData'); var value = $(this).combobox('getValues'); var panel = $(this).combobox('panel'); panel.find('.combo-panel-checkbox').prop('checked', false); $.each(data, function(index, item){ if (value.indexOf(item.id) >= 0){ panel.find('.combo-panel-checkbox[value="'+item.id+'"]').prop('checked', true); } }); } }); ``` 注:示例代码中的get_data.php是一个返回JSON格式数据的接口,可以根据实际情况修改。
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值