1. 程式人生 > >Easyui combobox下拉框動態繫結資料

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;
	}

}

資料庫查詢省略,主要配置完成!