Easyui combobox下拉框动态绑定数据

easyui-combobox根据数据库查询绑定数据,如下:

1、html

<select id="st_cmpy" editable="false" class="easyui-combobox" name="compyId" data-options="label:'公司:'"></select>

 

2、JS:绑定查询:

//下拉框值加载
$("#st_cmpy").combobox({
    url: basePath+'/compy/getCompyNameList',
    method : "post",
    valueField: 'value',
    textField: 'text',
    selected: 'selected'
});	
	

3、后台Controller,返回Json格式数据

// 查询公司名称列表
@PostMapping("/getCompyNameList")
@ResponseBody
public List<ComboBoxUtils> getCompyNameList() {
	List<ComboBoxUtils> dataList = heiniuCompyService.getCompyNameList();
	return dataList;
}

4、Service实现:(设置selected属性,默认第一个选中)

@Override
public List<ComboBoxUtils> getCompyNameList() {
	List<ComboBoxUtils> list = new ArrayList<ComboBoxUtils>();
	try {
		List<Compy> result = compyDao.selectCompanyNameList();
		if (result != null && result.size() > 0) {
			boolean selected = false;
			for (int i = 0; i < result.size(); i++) {
				Compy obj = result.get(i);
				String value = obj.getCompyId();
				String text = obj.getCompyName();
				if (i == 0) {
					selected = true;
				} else {
					selected = false;
				}
				ComboBoxUtils item = new ComboBoxUtils(i + "", value, text, selected);
				list.add(item);
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return list;
}

5、ComboBoxUtils下拉框工具类:

/**
 * @ClassName: ComboBoxUtils 
 * @Description: easyui下拉框工具类
 * @author sam 
 * @date 2018年9月24日 下午12:59:55
 */
public class ComboBoxUtils {

	private String id;
	private String value;
	private String text;
	private boolean selected;

	public ComboBoxUtils(String id, String value, String text) {
		super();
		this.id = id;
		this.value = value;
		this.text = text;
	}

	public ComboBoxUtils(String id, String value, String text, boolean selected) {
		super();
		this.id = id;
		this.value = value;
		this.text = text;
		this.selected = selected;
	}

	public ComboBoxUtils() {
		super();
		// TODO Auto-generated constructor stub
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

}

数据库查询省略,主要配置完成!

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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格式数据的接口,可以根据实际情况修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值