model
public class Areas {
private Integer id;
private String areano;
private String address;
private String addrdemo;
private String parentareano;
private List<Areas> children;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAreano() {
return areano;
}
public void setAreano(String areano) {
this.areano = areano == null ? null : areano.trim();
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address == null ? null : address.trim();
}
public String getAddrdemo() {
return addrdemo;
}
public void setAddrdemo(String addrdemo) {
this.addrdemo = addrdemo == null ? null : addrdemo.trim();
}
public String getParentareano() {
return parentareano;
}
public void setParentareano(String parentareano) {
this.parentareano = parentareano == null ? null : parentareano.trim();
}
public List<Areas> getChildren() {
return children;
}
public void setChildren(List<Areas> children) {
this.children = children;
}
public Areas(Integer id, String areano, String address, String addrdemo, String parentareano) {
super();
this.id = id;
this.areano = areano;
this.address = address;
this.addrdemo = addrdemo;
this.parentareano = parentareano;
}
public Areas() {
super();
}
@Override
public String toString() {
return "Areas [id=" + id + ", areano=" + areano + ", address=" + address + ", addrdemo=" + addrdemo
+ ", parentareano=" + parentareano + ", children=" + children + "]";
}
}
serviceimpl
public List<Areas> findArea() throws Exception {
AreasExample examplen = new AreasExample();
// TODO Auto-generated method stub
return areasMapper.selectByExample(examplen);
}
LIst<map>转树形结构的json
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* list<Map>转树形json
**/
public class DownTree {
public static JSONArray listToTree(JSONArray arr, String id, String pid, String child) {
JSONArray r = new JSONArray();
JSONObject hash = new JSONObject();
// 将数组转为Object的形式,key为数组中的id
for (int i = 0; i < arr.size(); i++) {
JSONObject json = (JSONObject) arr.get(i);
hash.put(json.getString(id), json);
}
// 遍历结果集
for (int j = 0; j < arr.size(); j++) {
// 单条记录
JSONObject aVal = (JSONObject) arr.get(j);
// 在hash中取出key为单条记录中pid的值
JSONObject hashVP = (JSONObject) hash.get(aVal.get(pid).toString());
// 如果记录的pid存在,则说明它有父节点,将她添加到孩子节点的集合中
if (hashVP != null) {
// 检查是否有child属性
if (hashVP.get(child) != null) {
JSONArray ch = (JSONArray) hashVP.get(child);
ch.add(aVal);
hashVP.put(child, ch);
} else {
JSONArray ch = new JSONArray();
ch.add(aVal);
hashVP.put(child, ch);
}
} else {
r.add(aVal);
}
}
return r;
}
}
Controller
@Controller
@RequestMapping("areas")
public class AreasController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private AreasServlet areasServlet;
@RequestMapping("findAreas")
@ResponseBody
public JSONArray findAreas() throws Exception {
List<Object> listZTree = new ArrayList<Object>();
List<Areas> list = areasServlet.findArea();
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
for (int i = 0; i < list.size(); i++) {
Areas area = list.get(i);// 遍历
Map<String, Object> map = new HashMap<String, Object>();
map.put("pid", area.getParentareano());
map.put("id", area.getAreano());
map.put("text", area.getAddress());
data.add(map);
listZTree.add(map);
}
JSONArray result = DownTree.listToTree(JSONArray.parseArray(JSON.toJSONString(data)), "id", "pid", "children");
return result;
}
html
<select class="easyui-combobox" id="cmbModule_add_mm"
data-options="required: true," style="width: 150px;">
</select>
js
$(function() {
$("#cmbModule_add_mm").combotree({
width : '100%',
animate : true,
url : 'areas/findAreas',
labelPosition : 'top',
onBeforeSelect : function(node) {
// var data1=treeData(node,"id","pid","children");
// alert(data1.children);
if (node.children) {
// 先封装成一个tree
var t = $('#cmbModule_add_mm').combotree('tree');
// 然后使用toggle
t.tree('toggle', node.target);// 注意和下一行的顺序
t.tree("unselect");
// 判断node节点下是不是有childred? 有:点击的是一个父级菜单
return;
}
}
})
})
json
如果有什么不懂 可以加qq一起讨论 QQ:2476594635