select标签实现动态下拉列表


部分代码不能公布,大家可以参考思路

需求:需要一个下拉列表且值为数据库中某个字段

在这里插入图片描述

代码实现

前端

<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值