[b]1、效果图[/b]
【图1】
[img]http://dl2.iteye.com/upload/attachment/0117/7873/7233d104-c9f5-3b8c-ae25-357f3aa5011e.png[/img]
【图2】
[img]http://dl2.iteye.com/upload/attachment/0117/7875/184ba11c-df46-3acb-b132-5467150e6750.png[/img]
[b]2、js代码[/b]
[b]3、java代码[/b]
【图1】
[img]http://dl2.iteye.com/upload/attachment/0117/7873/7233d104-c9f5-3b8c-ae25-357f3aa5011e.png[/img]
【图2】
[img]http://dl2.iteye.com/upload/attachment/0117/7875/184ba11c-df46-3acb-b132-5467150e6750.png[/img]
[b]2、js代码[/b]
$(function(){
$('#treeId').tree({
url: 'myController.do?loadtreeFun&projectId=${projectId}',
checkbox:true,
animate:true,
lines:true,
onlyLeafCheck:true,//只选择联系人
onClick:function(node){
},
onBeforeExpand:function(node,param){
$('#treeId').tree('options').url='myController.do?loadtreeFun&projectId=${projectId}';
},
onContextMenu: function(e,node){//右键菜单
e.preventDefault();//阻止默认的右键菜单行为
$(this).tree('select',node.target);//选中
var b = $(this).tree('isLeaf', node.target);
if(b) {
//联系人
$('#treeMenu1').menu('show', {
left: e.pageX,
top: e.pageY
});
} else {
//联系组
$('#treeMenu').menu('show', {
left: e.pageX,
top: e.pageY
});
}
},
onLoadSuccess: function(node, data){
//$('#treeId').tree('expandAll');//展开所有节点
var rootNode = $("#treeId").tree('getRoot');
$('#treeId').tree('expand',rootNode.target);
}
});
});
/*确定:添加联系人手机号码到父窗口‘联系人电话’*/
function addMobile(){
var nodes = $('#treeId').tree('getChecked');
var str = '';
$.each(nodes,function(n,node){
if(node){
var data = node.text.split(':');//中文冒号
if(data[1])
{
if (str != '') {
str += (";"+data[1]);
} else {
str = data[1];
}
}
}
});
//alert(str);
parent.addMobile(str);
parent.closeChooseDlg();
}
[b]3、java代码[/b]
@RequestMapping(params={"loadtreeFun"})
public void loadtreeFun(HttpServletRequest request,HttpServletResponse response) {
Map condition = convertParameters(request.getParameterMap());
//System.out.println(UtilStr.dumpMap(condition));
SessionContext sc=new SessionContext(request);
String id=(String)condition.get("id");//当前选中节点Id
String projectId=(String)condition.get("projectId");//项目Id
JSONArray rtnJsonArray = new JSONArray();
if(UtilStr.isNullOrBlank(id)){
JSONObject jsonObject= new JSONObject();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if (UtilStr.isNotNullAndBlank(projectId)) {
list = service.getFun(id,Integer.valueOf(projectId));
}
if (null!=list&&list.size()>0) {
Map<String, Object> map=list.get(0);
ClassA root=(ClassA)map.get("root");//根节点
List<ClassA> ClassAs=(List<ClassA>) map.get("ClassAs");//二级组
List<ClassB> ClassBs=(List<ClassB>) map.get("ClassBs");//二级联系人
JSONObject jsonobj1=JSONObject.parseObject(root.toString());//根节点JSONObject
JSONArray jsonObject2=JSONArray.parseArray(ClassAs.toString());//二级组JSONObject
JSONArray jsonObject3=JSONArray.parseArray(ClassBs.toString());//二级联系人JSONObject
jsonObject2.addAll(jsonObject3);
jsonobj1.put("children", jsonObject2);
rtnJsonArray.add(jsonobj1);
}else{
//只有1个初始根节点
jsonObject.put("id", "");
jsonObject.put("text", "通信录");
jsonObject.put("state", "closed");
jsonObject.put("children", null);
rtnJsonArray.add(jsonObject);
}
renderJson(response,rtnJsonArray.toJSONString());
}else{
//非叶子节点的nodeId
JSONObject jsonObject= new JSONObject();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if (UtilStr.isNotNullAndBlank(projectId)) {
list = service.getFun(id,Integer.valueOf(projectId));
}
if (null!=list&&list.size()>0) {
//list可能为[{ClassAs=[], ClassBs=[]}]
Map<String, Object> map=list.get(0);
List<ClassA> ClassAs=(List<ClassA>) map.get("ClassAs");//超过二级组
List<ClassB> ClassBs=(List<ClassB>) map.get("ClassBs");//超过二级联系人
//
JSONArray jsonArray2=null;
if (null!=ClassAs&&ClassAs.size()>0) {
jsonArray2=JSONArray.parseArray(ClassAs.toString());//二级组JSONObject
}
JSONArray jsonArray3=null;
if (null!=ClassBs&&ClassBs.size()>0) {
jsonArray3=JSONArray.parseArray(ClassBs.toString());//二级联系人JSONObject
}
//
if (null!=jsonArray2) {
if (null!=jsonArray3) {
jsonArray2.addAll(jsonArray3);
}
renderJson(response,jsonArray2.toJSONString());
}else{
if (null!=jsonArray3) {
renderJson(response,jsonArray3.toJSONString());
}else{
renderJson(response,"false");//onBeforeExpand:Fires before node is expanded, return false to cancel this expand action.
}
}
}
}
}