部分代码不能公布,大家可以参考思路
需求:需要一个下拉列表且值为数据库中某个字段
代码实现
前端
<div>
<label style="width: 80px;">对接商务:</label>
<select id="businessPerId" name="businessPerId" style="width: 200px;">
<option value=""></option>
</select>
</div>
$(function () {
getBusinessPersonnel('businessPerId', true);//初始化商务
})
/*
* 初始化商务下拉列表
* @param {String} ele 商务元素id
* @param {boolean} ifEdit 默认展示请选择还是所有
* */
function getBusinessPersonnel(ele , ifEdit) {
var b;
$('#' + ele).empty();
$.ajax({
type: 'post',
url: ctx + '/web/common/getBusinessPersonnel',
async: false,
dataType: 'json',
success: function (data) {
b = data;
if (ifEdit) {
$('#' + ele).append("<option value='-1'>请选择</option>");
} else {
$('#' + ele).append("<option value='-1'>所有</option>");
}
$.each(data, function (i, a) {
$('#' + ele).append("<option value='{0}'>{1}</option>".format(a.businessPersonnelId, a.businessPersonnelName));
});
}
});
$('#' + ele).chosen();
}
后端【省略接口】
controller层
/**
* @author: kuangdaun
* @Description:(查询合作伙伴)
*/
@RequestMapping(value = "/getBusinessPersonnel", method = RequestMethod.POST)
@ResponseBody
public List<BusinessPersonnelEntity> getBusinessPersonnel() {
return iBusinessPersonnelService.getBusinessConfigs();
}
service层
@Resource
private IBusinessPersonnelReadDao iBusinessPersonnelReadDao;
@Override
public List<BusinessPersonnelEntity> getBusinessConfigs() {
return iBusinessPersonnelReadDao.getBusinessConfigs();
}
dao层
@Resource
private SqlSession sqlsessionRead;
@Override
public List<BusinessPersonnelEntity> getBusinessConfigs() {
return this.sqlsessionRead.selectList("read.businessPersonnel.getBusinessPersonnels");
}
mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="read.businessPersonnel">
<select id="getBusinessPersonnels" resultType="businessPersonnelEntity">
select b.BusinessPersonnelId , b.BusinessPersonnelName from BusinessPersonnel b where b.Status = 1
</select>
</mapper>