easyui combobox动态绑定数据

来源:互联网 发布:怎样推广淘宝网店 编辑:程序博客网 时间:2024/06/05 17:04

1.jsp上的写法

  <input class="easyui-combobox" id="first_catalogue"  style="width:30%;" value="--请选择所属分类--" >  

2.js

    $(function(){      // $("#first_catalogue").parent(".l").show();        $('#first_catalogue').combobox({     url:'<%=basePath%>getDic/getFirstCategory.do?userCategory=${pd.userCategory}',    valueField:'id',    textField:'text',     required: true,    editable:false,                   /*  onLoadSuccess: function () {  //加载完成后,设置选中第一项                      var val = $(this).combobox("getData");                    //设置第一个值为选中值                                     $(this).combobox("select",val[0].text);                  } , */        onChange: function (){        //getValue,getText分别获取value值,text        var val =  $('#first_catalogue').combobox("getText");        alert(val);            }         });        //下方是在前端绑定数据     /*  var data,json;      data = [];      data.push({ "text": "项目积累", "id":"xmjl" },{ "text": "最佳实践", "id":"xmjl" });      $("#first_catalogue").combobox("loadData", data); */}) 

3.Colltroller中的写法

    //获取所属分类的一级分类    @RequestMapping(value="/getFirstCategory",produces="application/json;charset=utf-8")    @ResponseBody    //@ControllerInterceptor(Description="查询主题分类字典",Type=enumCtrlType.select)    public JSONArray getFirstCategory()throws Exception{        String returnCategory="";        JSONArray JsonArray = new JSONArray();          PageData pd = this.getPageData();        String userCategory = pd.getString("userCategory");        List<String> firstCategory=ResoDictFacade.getFirstCategory(userCategory);        for(int i=0;i<firstCategory.size();i++){            String Category=firstCategory.get(i);            /*Map params =  new HashMap();                params.put("text", Category);                params.put("id", "option_"+i);           JSONArray array = JSONArray.fromObject(params);           returnCategory+=array;*/            JSONObject Json = new JSONObject();                //JSONObject对象中添加键值对            Json.put("id", "option_"+i);             Json.put("text", Category);            //将JSONObject对象添加到Json数组中             JsonArray.add(Json);        }               return JsonArray;    }