1.ajax 后台向前台传值,转json时有多层集合问题
四处找了一下,发现如下解决方法:
a.通过配置JsonCconfig来过滤特定字段
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreDefaultExcludes(false); //设置默认忽略
jsonConfig.setExcludes(new String[]{"过滤字段名","过滤字段名"});
String string = JSONObject.fromObject(rs).toString();
b.
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.setJavaPropertyFilter(new PropertyFilter(){
public boolean apply(Object source, String name, Object value) {
if(name.equals("过滤字段名") || name.equals("过滤字段名")) {
return true;
} else {
return fase;
}
}
});
jsonConfig.setIgnoreDefaultExcludes(false); //设置默认忽略
String string = JSONObject.fromObject(rs).toString();
c.
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
jsonConfig.setIgnoreDefaultExcludes(false); //设置默认忽略
String string = JSONObject.fromObject(rs).toString();
当然,最后的结果是我没用起,只好建了一个辅助类,终于完成了!宝宝心里苦啊。
2.动态添加option
先写两个我用成功了的:
a.用原生js添加
var sel= document.getElementById("select的ID");
sel.options.add(new Option("请选择",""));
sel.options.add(new Option(要显示的数据,需要传递的数据));
b.用jq添加
var op = document.createElement('option');
op.value = countys[i].id;
op.innerHTML = countys[i].name;
$("#select的ID").append(op);
或者简单粗暴一点
$("#select的ID").append("<option value='"+需要传递的数据+"'>"+要显示的数据+"</option>");