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

原创 2013年10月14日 20:13:45

前言

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

页面(index.jsp)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
	<title>ComboTree Actions - jQuery EasyUI Demo</title>
	<link rel="stylesheet" type="text/css" href="http://blog.csdn.net/nupt123456789/article/details/js/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="http://blog.csdn.net/js/themes/icon.css">
	<link rel="stylesheet" type="text/css" href="http://blog.csdn.net/js/demo/demo.css">
	<script type="text/javascript" src="http://blog.csdn.net/js/jquery.min.js"></script>
	<script type="text/javascript" src="http://blog.csdn.net/js/jquery.easyui.min.js"></script>
	<script type="text/javascript">
	   $(function(){
		   $("#cc").combotree({
			   url:'getTreeData',//Action,获取树的信息
			   multiple:true//设置是否多选
		   });
		});
	</script>
</head>
<body>
	<h2>ComboTree Actions</h2>
	<div class="demo-info">
		<div class="demo-tip icon-tip"></div>
		<div>Click the buttons below to perform actions</div>
	</div>
	<div style="margin:10px 0">
		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="getValue()">GetValue</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="setValue()">SetValue</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="disable()">Disable</a>
		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="enable()">Enable</a>
	</div>
	<input id="cc" class="easyui-combotree"  style="width:200px;">
	<script type="text/javascript">
		function getValue(){
			var val = $('#cc').combotree('getValue');
			var text = $('#cc').combotree('getText');
			alert("val="+val+",text="+text);
		}
		function setValue(){
			$('#cc').combotree('setValue', '这是设定的值');
		}
		function disable(){
			$('#cc').combotree('disable');
		}
		function enable(){
			$('#cc').combotree('enable');
		}
	</script>

</body>
</html>

2.ComboTree的数据源类

/*
 * $filename: ComboTreeModel.java,v $
 * $Date: 2013-10-14  $
 * Copyright (C) ZhengHaibo, Inc. All rights reserved.
 * This software is Made by Zhenghaibo.
 */
package edu.njupt.zhb.model;

import java.util.List;

/*
 *@author: ZhengHaibo  
 *web:     /nuptboyzhb
 *mail:    zhb931706659@126.com
 *2013-10-14  Nanjing,njupt,China
 */
public class ComboTreeModel {
	private int id;
	private String text;
	private List<ComboTreeModel> children;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public List<ComboTreeModel> getChildren() {
		return children;
	}
	public void setChildren(List<ComboTreeModel> children) {
		this.children = children;
	}
}

3.Action类

/*
 * $filename: ZTreeDemoAction.java,v $
 * $Date: Sep 27, 2013  $
 * Copyright (C) ZhengHaibo, Inc. All rights reserved.
 * This software is Made by Zhenghaibo.
 */
package edu.njupt.zhb.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.njupt.zhb.model.ComboTreeModel;
import net.sf.json.JSONArray;

/*
 *@author: ZhengHaibo  
 *web:     /nuptboyzhb
 *mail:    zhb931706659@126.com
 *Sep 27, 2013  Nanjing,njupt,China
 */
public class CombotreeDemoAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3318989776253565435L;
	/**
	 * 模拟从数据库读取数据
	 * 
	 * @return
	 */
    public void getTreeData(){
    	List<ComboTreeModel> list = new ArrayList<ComboTreeModel>();
    	for(int i = 1;i<10;i++){
    		ComboTreeModel ctm = new ComboTreeModel();
    		ctm.setId(i);
    		ctm.setText("树节点"+i);
    		if(i == 2){
    			List<ComboTreeModel> children = new ArrayList<ComboTreeModel>();
    			for (int j = 1; j < 6; j++) {
    				ComboTreeModel comboTreeModel = new ComboTreeModel();
    				comboTreeModel.setId(j);
    				comboTreeModel.setText("子节点"+i+j);
    				children.add(comboTreeModel);
				}
    			ctm.setChildren(children);
    		}
    		list.add(ctm);
    	}
    	System.out.println("----json--");
    	String json = JSONArray.fromObject(list).toString();//转化为JSON
    	getPrintWriter().write(json);//返回前台
    }
	/**
	 * 获得HttpServletResponse对象
	 * 
	 * @return
	 */
	public static HttpServletResponse getResponse() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		return response;
	}

	public PrintWriter getPrintWriter() {
		PrintWriter pw = null;
		try {
			pw = getResponse().getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return pw;
	}

}

4.struts2的配置

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">


<struts>
    <constant name="struts.server.static.browserCache" value="false" />
	<constant name="struts.ui.theme" value="simple" />
	<constant name="struts.devMode" value="true" />
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<constant name="struts.configuration.xml.reload" value="true" />
	<package name="zhenghaiboTest" extends="struts-default">
		<action name="getTreeData" class="edu.njupt.zhb.action.CombotreeDemoAction" method="getTreeData">
		</action>
	</package>
</struts>

5.效果

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


2.获取组件的值


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

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

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




版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/NUPTboyZHB/article/details/12715155

Jquery ui ComboTree绑定

function QueryXXX(){ $.post("JqueryUI.asmx/GetMaterial1",{},function(dt){ ...
  • hutao1101175783
  • hutao1101175783
  • 2014-10-09 17:39:18
  • 1437

easyUi的combotree绑定后台返回的json字符串(EasyUI+spring mvc+mybatis)

1. 先展示图片: 2. json的数据格式,id 和 text ,children是固定的,不能变,还有记得都要用双引号的,你如果觉得你字符串没有问题,但是就是显示不出来,多半就是json格式...
  • zhaoyingjiao
  • zhaoyingjiao
  • 2015-06-09 21:36:54
  • 3977

Jquery UI Tree组件的json格式,java递归拼接demo

Jquery UI 中Tree组件的json格式,java递归拼接demo
  • xiaokui_wingfly
  • xiaokui_wingfly
  • 2013-08-06 11:12:10
  • 2467

树控件绑定数据源.rar

  • 2010年07月01日 12:24
  • 40KB
  • 下载

SSH+Easyui之combotree ,TreeGrid 树形展现数据

本文介绍Struts2、Spring、Hibernate与easyui的TreeGrid结合,以树的形式展现数据库中数据。在开发中会碰到很多需要以树的形式展现数据,如导航条、权限管理模块中的资源管理,...
  • NRlovestudy
  • NRlovestudy
  • 2016-02-22 17:44:57
  • 2153

jQuery EasyUI Combotree 实现ComboBox的内容过滤

ComboBox一样可以过滤就好了。         但是,貌似官方却没有提供类似ComboBox一样的过滤功能,没办法了,翻看了API后发了新大陆,哈哈。。         是的,就是图中的...
  • zhpengfei0915
  • zhpengfei0915
  • 2015-11-12 17:43:45
  • 1451

easyui-combobox、combotree后台数据数据组装与前台绑定实例

  • 2015年09月18日 14:34
  • 2KB
  • 下载

jquery easyui combotree 树实例简单应用

在使用jquery easyui之前 要先引入js: 定义一个input 作为树显示表单,并初始化,调用action: $(funct...
  • caisenbinbeida2009
  • caisenbinbeida2009
  • 2014-03-04 14:26:23
  • 3196

easyui的combotree同步树和异步树实现方法

1 异步树 $("#"+treeId).combotree({ url : treeUrl, panelWidth:'200', onClick : function(node) {    onBef...
  • qq_28643437
  • qq_28643437
  • 2017-04-21 14:22:41
  • 1773

jquery.dataTable 绑定数据源

var oTable=$('#sample_editable_1').dataTable({                                         "iDisplayLen...
  • ejinxian
  • ejinxian
  • 2014-05-08 18:54:26
  • 3014
收藏助手
不良信息举报
您举报文章:Jquery ComboTree树的绑定-数据源JSON格式-操作
举报原因:
原因补充:

(最多只允许输入30个字)