采用枚举或map封装多选框数据

  • 在实际开发中,我们需要用到很多选择框,然而怎么将这个map类型的数据绑定到jsp或者HTML页面中呢,此处我给出三种实现方案,两种动态,一种静态:

  • 一 : 采用枚举封装后的方式:
    首先建立一个枚举样本:

public enum OwnerTypeEnum {
    INVALID(-1, "无效业主类型"), OWNER(1, "业主"), TENANT(2, "租户");

    /**
     * 状态值
     */
    private int status;

    /**
     * 状态描述
     */
    private String desc;

    private OwnerTypeEnum(int status, String desc) {
        this.status = status;
        this.desc = desc;
    }

    public int getStatus() {
        return status;
    }

    public String getDesc() {
        return desc;
    }

    /**
     * 根据状态值获取枚举值
     * 
     * @param status
     * @return
     */
    public static OwnerTypeEnum getValue(int status) {
        for (OwnerTypeEnum e : OwnerTypeEnum.values()) {
            if (e.getStatus() == status) {
                return e;
            }
        }

        return INVALID;
    }
}
  • 当写好了枚举之后,接下来我们就需要在控制层对其进行处理:
@RequestMapping("index.do")
public ModelAndView index(HttpServletRequest request) {
    Map<String, Object> model = new HashMap<String, Object>();
    /** 业主类型 */
    Map<Integer, String> type = new TreeMap<Integer, String>();
    //将枚举遍历,获得对应的id 和 value;
    for (OwnerTypeEnum ownerType : OwnerTypeEnum.values()) {
        if (ownerType.getStatus() != OwnerTypeEnum.INVALID.getStatus()) {
            type.put(ownerType.getStatus(), ownerType.getDesc());
        }
    }
    model.put("type", type);
    return new ModelAndView("crm/owner/owner", model);
}
<td>客户类型:</td>
<td><select class="easyui-combobox" name="type" data-options="required:true" style="width:150px;">
    <c:forEach var="item" items="${type}">
        <option value="${item.key}" selected="selected">${item.value}</option>
    </c:forEach>
    </select>
</td>
  • 以上就将该map的枚举样式放到了页面中;
    这里写图片描述

当然通过枚举的方法生成的map;如果要将数据显示到表单中,那么相对要容易下 直接在实体的VO的构造器中加入 :

String ownerTypeTxt = OwnerTypeEnum.getValue(owner.getType()).getDesc();

这样就直接获得value,将其显示出来。
二 : 直接采用map的方式,这种方式适用于那些不常用的数据,这样就不用对其进行枚举封装:

@RequestMapping("getYhqByResidenceCode.do")
@ResponseBody
public List<Map<String, Object>> getYhqByResidenceCode(WyFeeYhqDetailQuery condition){
        condition.setStatus(String.valueOf(1));//首先创建查询条件,这个根据需求而定;
        List<WyFeeYhqDetailPO> list = yhqService.queryWyFeeYhqNumPOBycodition(condition);//将查询到的数据封装到list中;
        List<Map<String, Object>> yhqMaps = new ArrayList<Map<String,Object>>();//创建list 的map 数据格式,因为页面的select选择框是键值对的样式;
        for (WyFeeYhqDetailPO po : list) {
            Map<String, Object>  map = new HashMap<String, Object>();
            map.put("id", po.getId());//这里的键我放的是id;
            if(po.getYhq_type()==1){
                map.put("text", po.getContent()+"折 ,"+po.getSoule());
            }else {
                map.put("text", "[-" + po.getContent()+"]"+po.getSoule());//值是放的描述,根据个人需求存放数据;
            }
            yhqMaps.add(map);//将map 放到 list中
        }
        return yhqMaps;  //最后将list的键值对样式返回成json对象,前台取数据
    }
<td>优惠券:</td>
<td><input id="wyYhq" style="width: 100px" class="easyui-combobox" name="wyYhq"  data-options="valueField:'id',textField:'text'" />  
</td>
//js将数据数据取出并放到easyui combobox中;具体用法,参考easyui官方文档,这里我就不做阐述了;
$('#wyYhq').combobox('reload','getYhqByResidenceCode.do?residence_code='+row.roomCode); 

这里写图片描述

  • 以上两个就完成了将map放到jsp或者HTML中,这两个方法实现动态的数据添加,不管是增加新的枚举,或者数据库新增数据都不用更改页面数据,属于动态的生成map,接下来就是最为简单的静态的方法,这种方法适用于固定的key value, 例如 ‘男’,’女’。
  • 三:固定的选择是采用以下方法:
<select id="execution_status" class="easyui-combobox" style="width:200px;"> 
    <option value="">请选择</option> 
    <option value="0">男</option> 
    <option value="1">女</option>   
</select>  
//当然也可以采用JS添加到 select框中
<select id="execution_status" class="easyui-combobox" style="width:200px;">
</select>
var arr = [{ "text": "男", "value": "0" },{ "text": "女", "value": "1" }];
$('#execution_status').combobox('loadData',arr);     

版权声明:本文为Megamind_HL原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。