在使用jquery easyui之前 要先引入js:
<script type="text/javascript" src="/js/jquery-easyui-1.3.2/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="/js/jquery-easyui-1.3.2/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/js/jquery-easyui-1.3.2/locale/easyui-lang-zh_CN.js"></script>
定义一个input 作为树显示表单,并初始化,调用action:
<input id="tree" class="easyui-combotree"/>
<script>
$(function(){
$("#tree").combotree({
url:'combotree.action',
});
});
</script>
action请求,编写树结构,返回json数据:
public void combotree() throws IOException{
List<ComboTreeModel> list=new ArrayList<ComboTreeModel>();
HttpServletResponse response=ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
List<Organization> organizationList=wfs.getOrganizationList();
for(Organization orz : organizationList){
ComboTreeModel ctm = new ComboTreeModel();
ctm.setId(orz.getOrgName());
ctm.setText(orz.getOrgName());
List<ComboTreeModel> children = new ArrayList<ComboTreeModel>();
for(Organization organization : organizationList){
if(orz.getOrgName().equals(organization.getOrgName())){
ComboTreeModel comboTreeModel = new ComboTreeModel();
comboTreeModel.setId(organization.getAdminName());
comboTreeModel.setText(organization.getAdminName());
children.add(comboTreeModel);
}
}
ctm.setChildren(children);
list.add(ctm);
}
String json = JSONArray.fromObject(list).toString();
response.getWriter().print(json);
}
如果需要只选中子节点,选中父节点清除:
$(function(){
$("#tree").combotree({
url:'combotree.action',
onSelect:function(node){
var tree = $(this).tree; //返回树对象
// 选中的节点是否为叶子节点,如果不是叶子节点,清除选中
var isLeaf = tree('isLeaf', node.target);
if(!isLeaf){
$("#tree").combotree('clear'); //清除选中
}
}
});
});