原理:用ajax获取选中的值,把选中的值放入指定select下即可。
第一步,确定传回前台的Json,代码如下:
获取下拉框选中的值:
@RequestMapping("/test")
@ResponseBody
public String atest(HttpServletRequest request,
HttpServletResponse response) {
List<String> list = new ArrayList<String>();
list.add("Java");
list.add("Perl");
return MyJSON.obj2JSON(list);
}
获取下拉框所有值:
@RequestMapping("/testall")
@ResponseBody
public String atestall(HttpServletRequest request,
HttpServletResponse response) {
String[] stringss = {"Java","Perl","Ruby"};
List<Map<String, String>> strings = new ArrayList<Map<String, String>>();
for (int i = 0; i < stringss.length; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("label", stringss[i]);
map.put("value", stringss[i]);
strings.add(map);
}
return MyJSON.obj2JSON(strings);
}
其中,MyJSON为obj转JSON工具类,详情见下方。
import java.text.SimpleDateFormat;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
/**
* @projectName:mysis_v2013.3
* @className:MyJSON
* @description:JSON类工具
* @author:LeoLee
* @date:2013-11-11 下午09:35:30
* @version:
*/
public class MyJSON {
/**
* @title: obj2JSON
* @description: 将Object转换成JSON
* @param obj
* @throws Exception
* @return String
* @throws
*/
public static String obj2JSON( Object obj ) {
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
objectMapper.setDateFormat(formatter);
return objectMapper.writeValueAsString(obj);
} catch (Exception e) {
return "";
}
}
}
第二步,前台页面写好对应select
<select id="ccas" class="easyui-combobox" name="dept1" style="width:200px;">
</select>
第三步,写对应js
获取下拉框选中的值:
$.post("/test",function(data,textStatus,jqxhr){
abc(data);
},"json");
获取下拉框中所有值并显示的方法:
function abc(vvalue){
$('#ccas').combobox({
url:"selecttest/testall",
valueField:'label',
textField:'value',
multiple:true,
panelHeight:'auto',
value:vvalue
});
}
效果图:
注:obj转json非原创