表单下拉选加载,选项源自数据字典

一般遇到表单添加下拉选,选项要配置在数据字典里面,写一个通用的js,方便复用。
在这里插入图片描述
//加载数据字典
JSP $(document).ready(function() 里面调用

 loadSelectData("security","OA_DOCRED_SECURITY",'${entity.security}');
 loadSelectData("urgency","OA_DOCRED_URGENCY",'${entity.urgency}');

JS

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="../../../../taikangkm/oa/common/taglibs.jsp" %>
<script>
    //数据字典的加载
function loadSelectData(inputId,code,value){
    $.ajax({
        url: "${ctx}/system/datadict/tree.htm?supCode="+code,
        type: "get",
        dataType: "json",
        success: function (data) {
        if(data !=''){
            var optionstring = "";
            for (var i in data) {
                var jsonObj =data[i];
            if(value !='' && value != null){
                if(jsonObj.name == value){
                    optionstring += "<option value=\"" + jsonObj.name + "\" selected =selected>" + jsonObj.name + "</option>";
                }else{
                    optionstring += "<option value=\"" + jsonObj.name + "\" >" + jsonObj.name + "</option>";
                }
            }else{
                if(i==0){
                    optionstring += "<option value=\"" + jsonObj.name + "\"  selected='selected'>" + jsonObj.name + "</option>";
                }else{
                    optionstring += "<option value=\"" + jsonObj.name + "\" >" + jsonObj.name + "</option>";
                }
            }
        }
                $("#" + inputId).html(optionstring);
            }
        }
    });
}
</script>

后台代码

 /**
     * 根据上级字典获取下级字典列表,以树节点数据格式返回。
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/tree.htm", method = RequestMethod.GET)
    @ResponseBody
    public JSONArray treeData(HttpServletRequest request,
                              @RequestParam(value = "id", required = false) String parentId,
                              @RequestParam(value = "supCode", required = false) String supCode,
                              @RequestParam(value = "state", required = false) String state) throws ServiceException {
        // 设置资源显示的分页和排序信息
        Page page = new Page();
        page.setPageAble(false); // 不需要进行分页查询
        page.setSortProperty(new String[][] {new String[] { "orderNum", "asc" }}); // 设置排序
        Where where = null;
        String hql = null;
        if (!Empty.isEmpty(supCode)) {
            hql = "select t from DataDict t left join t.parent parent ";
            where = Where.rootWhere("parent.uniqueCode", Where.EQ, supCode);
        } else if (!Empty.isEmpty(parentId)) {
            where = Where.rootWhere("parent.id", Where.EQ, parentId);
        } else {
            where = Where.rootWhere("parent.id", Where.IS, "NULL");
        }
        if (!Empty.isEmpty(state)) {
            where.childANDWhere("state", Where.EQ, state);
        } else {
            where.childANDWhere("state", Where.EQ, "1");
        }
        // 查询字典信息
        List<DataDict> list = (List<DataDict>) this.dataDictService.findList(page, where, hql);
        JSONArray data = new JSONArray();
        // 封装字典树节点。
        for (DataDict vo : list) {
            TreeNode node = new DataDictTreeNode(vo);
            if (Empty.isEmpty(vo.getChildren())) {
                node.setHasChildren(false);
            } else {
                node.setHasChildren(true);
            }
            inLanguageUtil.convertLanguage(request, node, new String[]{"name"}, false);
            data.add(node);
        }
        return data;
    }

猜你喜欢

转载自blog.csdn.net/supershuyun/article/details/86493389