Jquery ComboTree树的绑定-数据源JSON格式-操作

(转载)http://blog.csdn.net/nupt123456789/article/details/12715155

前言

ComboTree也是表单中一种常见组件,如:有些输入框,限定只能选取一些特征的数据,而且这些数据时需要动态从数据库中读取的。我这里就演示一下这个过程(数据库就不涉及了,后台能产生Combotree所需的Json格式数据就行了)。以下是我写的一个Demo。前台的操作有:1.绑定树的url,设置是否多选 2.获取用户所选的值 3.设置特定的值 4.Disable和Enable

页面(index.jsp)

[html]  view plain  copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3.     String path = request.getContextPath();  
  4.     String basePath = request.getScheme() + "://"  
  5.             + request.getServerName() + ":" + request.getServerPort()  
  6.             + path + "/";  
  7. %>  
  8. <!DOCTYPE html>  
  9. <html>  
  10. <head>  
  11.     <title>ComboTree Actions - jQuery EasyUI Demo</title>  
  12.     <link rel="stylesheet" type="text/css" href="js/themes/default/easyui.css">  
  13.     <link rel="stylesheet" type="text/css" href="js/themes/icon.css">  
  14.     <link rel="stylesheet" type="text/css" href="js/demo/demo.css">  
  15.     <script type="text/javascript" src="js/jquery.min.js"></script>  
  16.     <script type="text/javascript" src="js/jquery.easyui.min.js"></script>  
  17.     <script type="text/javascript">  
  18.        $(function(){  
  19.            $("#cc").combotree({  
  20.                url:'getTreeData',//Action,获取树的信息  
  21.                multiple:true//设置是否多选  
  22.            });  
  23.         });  
  24.     </script>  
  25. </head>  
  26. <body>  
  27.     <h2>ComboTree Actions</h2>  
  28.     <div class="demo-info">  
  29.         <div class="demo-tip icon-tip"></div>  
  30.         <div>Click the buttons below to perform actions</div>  
  31.     </div>  
  32.     <div style="margin:10px 0">  
  33.         <a href="javascript:void(0)" class="easyui-linkbutton" onclick="getValue()">GetValue</a>  
  34.         <a href="javascript:void(0)" class="easyui-linkbutton" onclick="setValue()">SetValue</a>  
  35.         <a href="javascript:void(0)" class="easyui-linkbutton" onclick="disable()">Disable</a>  
  36.         <a href="javascript:void(0)" class="easyui-linkbutton" onclick="enable()">Enable</a>  
  37.     </div>  
  38.     <input id="cc" class="easyui-combotree"  style="width:200px;">  
  39.     <script type="text/javascript">  
  40.         function getValue(){  
  41.             var val = $('#cc').combotree('getValue');  
  42.             var text = $('#cc').combotree('getText');  
  43.             alert("val="+val+",text="+text);  
  44.         }  
  45.         function setValue(){  
  46.             $('#cc').combotree('setValue', '这是设定的值');  
  47.         }  
  48.         function disable(){  
  49.             $('#cc').combotree('disable');  
  50.         }  
  51.         function enable(){  
  52.             $('#cc').combotree('enable');  
  53.         }  
  54.     </script>  
  55.   
  56. </body>  
  57. </html>  

2.ComboTree的数据源类

[java]  view plain  copy
  1. /* 
  2.  * $filename: ComboTreeModel.java,v $ 
  3.  * $Date: 2013-10-14  $ 
  4.  * Copyright (C) ZhengHaibo, Inc. All rights reserved. 
  5.  * This software is Made by Zhenghaibo. 
  6.  */  
  7. package edu.njupt.zhb.model;  
  8.   
  9. import java.util.List;  
  10.   
  11. /* 
  12.  *@author: ZhengHaibo   
  13.  *web:     http://blog.csdn.net/nuptboyzhb 
  14.  *mail:    zhb931706659@126.com 
  15.  *2013-10-14  Nanjing,njupt,China 
  16.  */  
  17. public class ComboTreeModel {  
  18.     private int id;  
  19.     private String text;  
  20.     private List<ComboTreeModel> children;  
  21.     public int getId() {  
  22.         return id;  
  23.     }  
  24.     public void setId(int id) {  
  25.         this.id = id;  
  26.     }  
  27.     public String getText() {  
  28.         return text;  
  29.     }  
  30.     public void setText(String text) {  
  31.         this.text = text;  
  32.     }  
  33.     public List<ComboTreeModel> getChildren() {  
  34.         return children;  
  35.     }  
  36.     public void setChildren(List<ComboTreeModel> children) {  
  37.         this.children = children;  
  38.     }  
  39. }  

3.Action类

[java]  view plain  copy
  1. /* 
  2.  * $filename: ZTreeDemoAction.java,v $ 
  3.  * $Date: Sep 27, 2013  $ 
  4.  * Copyright (C) ZhengHaibo, Inc. All rights reserved. 
  5.  * This software is Made by Zhenghaibo. 
  6.  */  
  7. package edu.njupt.zhb.action;  
  8.   
  9. import java.io.IOException;  
  10. import java.io.PrintWriter;  
  11. import java.util.ArrayList;  
  12. import java.util.List;  
  13.   
  14. import javax.servlet.http.HttpServletResponse;  
  15. import org.apache.struts2.ServletActionContext;  
  16. import com.opensymphony.xwork2.ActionSupport;  
  17.   
  18. import edu.njupt.zhb.model.ComboTreeModel;  
  19. import net.sf.json.JSONArray;  
  20.   
  21. /* 
  22.  *@author: ZhengHaibo   
  23.  *web:     http://blog.csdn.net/nuptboyzhb 
  24.  *mail:    zhb931706659@126.com 
  25.  *Sep 27, 2013  Nanjing,njupt,China 
  26.  */  
  27. public class CombotreeDemoAction extends ActionSupport {  
  28.     /** 
  29.      *  
  30.      */  
  31.     private static final long serialVersionUID = -3318989776253565435L;  
  32.     /** 
  33.      * 模拟从数据库读取数据 
  34.      *  
  35.      * @return 
  36.      */  
  37.     public void getTreeData(){  
  38.         List<ComboTreeModel> list = new ArrayList<ComboTreeModel>();  
  39.         for(int i = 1;i<10;i++){  
  40.             ComboTreeModel ctm = new ComboTreeModel();  
  41.             ctm.setId(i);  
  42.             ctm.setText("树节点"+i);  
  43.             if(i == 2){  
  44.                 List<ComboTreeModel> children = new ArrayList<ComboTreeModel>();  
  45.                 for (int j = 1; j < 6; j++) {  
  46.                     ComboTreeModel comboTreeModel = new ComboTreeModel();  
  47.                     comboTreeModel.setId(j);  
  48.                     comboTreeModel.setText("子节点"+i+j);  
  49.                     children.add(comboTreeModel);  
  50.                 }  
  51.                 ctm.setChildren(children);  
  52.             }  
  53.             list.add(ctm);  
  54.         }  
  55.         System.out.println("----json--");  
  56.         String json = JSONArray.fromObject(list).toString();//转化为JSON  
  57.         getPrintWriter().write(json);//返回前台  
  58.     }  
  59.     /** 
  60.      * 获得HttpServletResponse对象 
  61.      *  
  62.      * @return 
  63.      */  
  64.     public static HttpServletResponse getResponse() {  
  65.         HttpServletResponse response = ServletActionContext.getResponse();  
  66.         response.setContentType("text/html;charset=UTF-8");  
  67.         return response;  
  68.     }  
  69.   
  70.     public PrintWriter getPrintWriter() {  
  71.         PrintWriter pw = null;  
  72.         try {  
  73.             pw = getResponse().getWriter();  
  74.         } catch (IOException e) {  
  75.             e.printStackTrace();  
  76.         }  
  77.         return pw;  
  78.     }  
  79.   
  80. }  

4.struts2的配置

[html]  view plain  copy
  1. <!DOCTYPE struts PUBLIC  
  2. "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"  
  3. "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4.   
  5.   
  6. <struts>  
  7.     <constant name="struts.server.static.browserCache" value="false" />  
  8.     <constant name="struts.ui.theme" value="simple" />  
  9.     <constant name="struts.devMode" value="true" />  
  10.     <constant name="struts.i18n.encoding" value="UTF-8" />  
  11.     <constant name="struts.configuration.xml.reload" value="true" />  
  12.     <package name="zhenghaiboTest" extends="struts-default">  
  13.         <action name="getTreeData" class="edu.njupt.zhb.action.CombotreeDemoAction" method="getTreeData">  
  14.         </action>  
  15.     </package>  
  16. </struts>  

5.效果

1.点击组件,树的加载效果


2.获取组件的值


3.设定值,Disable和Enable功能亦可

项目完整源代码:http://download.csdn.net/detail/nuptboyzhb/6398741

未经允许不得用户商业目的


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值