- 树形网格(TreeGrid)可以展示有限空间上带有多列和复杂数据电子表
一、案例一:按tree的数据结构来生成
- 前台
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><% String path = request.getContextPath();%>Insert title here 树形网格treeGrid
- 后台
package servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanListHandler;import com.google.gson.Gson;import bean.Dept;import util.DBUtil;/** * Servlet implementation class DeptServlet */@WebServlet("/servlet/treeGrid")public class DeptServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter out=response.getWriter(); String treeGrid_json=""; String sql=""; Connection conn=null; try { conn=DBUtil.getConn(); QueryRunner queryRunner=new QueryRunner(); sql="select * from dept"; List
结果:
- 异步树请参考前几章。
二、案例二:按Grid的数据结构来生成+查询功能
- 数据库
- 定义数据库函数:用于用于根据某个节点的所有父节点
BEGIN DECLARE fid varchar(100) default ''; DECLARE str varchar(1000) default rootId; WHILE rootId is not null do SET fid =(SELECT parent_id FROM dept WHERE dept_id = rootId); IF fid is not null THEN SET str = concat(str, ',', fid); SET rootId = fid; ELSE SET rootId = fid; END IF; END WHILE; return str; END
- 前台
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><% String path = request.getContextPath();%>Insert title here 树形网格treeGrid
- 后台
package servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.MapListHandler;import com.google.gson.Gson;import util.DBUtil;/** * Servlet implementation class DeptServlet */@WebServlet("/servlet/treeGrid")public class DeptServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); Connection conn = null; Statement stat = null; ResultSet rs = null; String sql = ""; String keyWord = request.getParameter("name"); if (keyWord == null || keyWord.equals("")) { // 如果没有查询参数 sql = "select * from dept"; } else { /** * 如果游查询参数 1.先找出子菜单 */ String tempSQL = "select dept_id,dept_name from dept where dept_name like '%" + keyWord + "%' order by dept_id asc "; try { conn = DBUtil.getConn(); QueryRunner queryRunner = new QueryRunner(); List> temList = queryRunner.query(conn, tempSQL, new MapListHandler()); StringBuffer unionSQL = new StringBuffer(); int rowIndex = 0; for (Map temMap : temList) { String tem_dept_Id = String.valueOf(temMap.get("dept_id")); /** * 2.根据得到的tem_dept_Id,去寻找相应的子节点 * 请在数据库中定义一个函数,该函数用于根据某个节点的所有父节点 */ unionSQL.append( "select dept_id from dept where FIND_IN_SET(dept_id,getParentList('" + tem_dept_Id + "'))"); rowIndex++; if (rowIndex > 0 && rowIndex != temList.size()) { unionSQL.append(" union "); } } if (unionSQL.length() > 0) { sql = "select * from dept where dept_id in("; sql = sql + unionSQL.toString(); sql = sql + ")"; } } catch (Exception e) { e.printStackTrace(); } } try { conn = DBUtil.getConn(); stat = conn.createStatement(); rs = stat.executeQuery(sql); List > gridDataList = new ArrayList >(); Map gridDataMap = new HashMap (); Map columnMap = null; while (rs.next()) { String id = (String.valueOf(rs.getInt("dept_id"))); String parent_id = (String.valueOf(rs.getInt("parent_id"))); String dept_name = rs.getString("dept_name"); String salary = rs.getString("salary"); String address = rs.getString("address"); columnMap = new HashMap (); columnMap.put("dept_id", id); columnMap.put("dept_name", dept_name); columnMap.put("salary", salary); columnMap.put("address", address); /** * 加入树形表格的特殊属性,一定要加否则样式不成树状 */ if (parent_id.equals("0") == false) { columnMap.put("_parentId", parent_id); } gridDataList.add(columnMap); } gridDataMap.put("total", gridDataList.size()); gridDataMap.put("rows", gridDataList); Gson gson = new Gson(); String str_gridData = gson.toJson(gridDataMap); System.out.println(str_gridData); out.print(str_gridData); } catch (Exception e) { e.printStackTrace(); } out.flush(); out.close(); }}
结果: