看方力勋的javaWeb,采用左右值来表示树型结构(就是俺门的多级分类)
表结构

页面代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>树状结构</title>
<script src="${pageContext.request.contextPath }/js/xtree.js"></script>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath }/css/xtree.css">
</head> <body> <script type="text/javascript">
<c:forEach var="c" items="${list}">
<c:if test="${c.depth==1}">
var tree = new WebFXTree('${c.name}');
tree.target="right";
tree.action = "${pageContext.request.contextPath}/servlet/AddChildServlet?id=${c.id}";
</c:if>
<c:if test="${c.depth==2}">
var node${c.depth} = new WebFXTreeItem('${c.name}');
node${c.depth}.target="right";
node${c.depth}.action = "${pageContext.request.contextPath}/servlet/AddChildServlet?id=${c.id}";
tree.add(node${c.depth});
</c:if>
<c:if test="${c.depth>2}">
var node${c.depth} = new WebFXTreeItem('${c.name}');
node${c.depth}.target="right";
node${c.depth}.action = "${pageContext.request.contextPath}/servlet/AddChildServlet?id=${c.id}";
node${c.depth-1}.add(node${c.depth});
</c:if>
</c:forEach> document.write(tree);
</script> </body>
</html>

呈现后html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>树状结构</title>
<script src="/AAPlan/js/xtree.js"></script>
<link type="text/css" rel="stylesheet" href="/AAPlan/css/xtree.css">
</head> <body> <script type="text/javascript"> var tree = new WebFXTree('商品');
tree.target="right";
tree.action = "/AAPlan/servlet/AddChildServlet?id=1"; var node2 = new WebFXTreeItem('平板电视');
node2.target="right";
node2.action = "/AAPlan/servlet/AddChildServlet?id=2";
tree.add(node2); var node3 = new WebFXTreeItem('长虹');
node3.target="right";
node3.action = "/AAPlan/servlet/AddChildServlet?id=5";
node2.add(node3); var node3 = new WebFXTreeItem('索尼');
node3.target="right";
node3.action = "/AAPlan/servlet/AddChildServlet?id=6";
node2.add(node3); var node2 = new WebFXTreeItem('冰箱');
node2.target="right";
node2.action = "/AAPlan/servlet/AddChildServlet?id=3";
tree.add(node2); var node3 = new WebFXTreeItem('西门子');
node3.target="right";
node3.action = "/AAPlan/servlet/AddChildServlet?id=7";
node2.add(node3); var node2 = new WebFXTreeItem('笔记本');
node2.target="right";
node2.action = "/AAPlan/servlet/AddChildServlet?id=4";
tree.add(node2); var node3 = new WebFXTreeItem('thinkpad');
node3.target="right";
node3.action = "/AAPlan/servlet/AddChildServlet?id=8";
node2.add(node3); var node3 = new WebFXTreeItem('dell');
node3.target="right";
node3.action = "/AAPlan/servlet/AddChildServlet?id=9";
node2.add(node3); document.write(tree);
</script> </body>
</html>

说明:
1.根据上面的数据列表可以知道,某个node节点的父节点是最近一个depth比当前节点小一的节点,通过node${c.depth-1}.add(node${c.depth})可以将当前节点添加到最近一次定义的上层节点下(也就是父节点)。

2.可以看到呈现 ,有多个var  node2=... 或 var node3=... 这样的变量重复定义,由于javascript中出现变量重复定义时,当前行javascript代码会采用最近一次变量定义的值(如果最近一次只var xx; 没有附值,那么当前代码会采用次最近一次定义的值,以此类推),所以上面代码能正常运行.

关于Xtree
var node=new WebFXTreeItem("xxx") 类,如果有调用node.add( node_sub),那么改node显示成文件夹,如果没有那么该node显示成文件

一点改进意见:
虽然上面的代码运行正常但是多个var nodex=...总让人有点意见
方法一:
        上来先来一句定义,var node1,node2...,nodex 这样下面的代码只要写node=....就好了
        当然这样页面上会有大量的node=new xxxx 样子的代码,如果   分类条目很多的话。
方法二:
         将分类数据存在成一个javascript数组,并且独立到一个servletCategory中,采用js引用方式加到页面中
         然后使用js根据数组内容构建tree并输出.
         大致的js代码是,定义一个parentNode,记录最近一次的parent节点位置,如果当前节点的level小于或等于parentNode的level,
         就从parenNode的位置往回找到最近一个level小于当前节点level的节点(Xtree的节点有parentNode属性可以获取到父节点,
          另外level 属性可以直接在节点上扩展 var tree=new WebFXTree("xxx") ; tree.level=3 )。
         另外需要定义prevNode记录最近一个节点,当当前curLevel-ParentLevel>=2 时(进入隔代级别时),将prevNode附值给parentNode

参考:
http://www.cnblogs.com/wdfrog/archive/2008/09/26/1299549.html

JSP中的一个树型结构的更多相关文章

  1. 设计一个树型目录结构的文件系统,其根目录为 root,各分支可以是目录,也可以是文件,最后的叶子都是文件。

    设计一个树型目录结构的文件系统,其根目录为 root,各分支可以是目录,也可以是文件,最后的叶子都是文件. 我实现的功能是提供父目录(兄弟目录),输入文件名,创建树型目录结构,文本文件不可以再有子目录 ...

  2. java树型结构的数据展现设计

    在做一个需求管理的页面时,需求的展现是不限层级树型结构,需求下还可以分拆任务,页面要展现的字段有20多个,而且需求采用通用表单设计,db采用大宽表存储,有一百多个字段.目前数据量不大,第一版采用普通的 ...

  3. dzzoffice的树型结构用户管理设计

    在DzzOffice1.1的开发中,针对用户使用群体重新设计了,机构.部门.用户管理应用. 传统OA,企业相关程序,一般是设置机构-设置部门-设置职位-添加用户这样的步骤.每个步骤分为不同的管理界面. ...

  4. 20-Ubuntu-文件和目录命令-查看目录树型结构-tree

    tree 以树状图列出当前目录下的文件目录结构 选项 含义 -d 只显示当前目录的子目录树型结构   显示当前目录的子目录和文件树型结构 例: 1.查看文档目录下的子目录和文件树型结构 2.查看文档目 ...

  5. Delphi实现树型结构

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  6. SQL Server 通过“with as”方法查询树型结构

    一.with as 公用表表达式 类似VIEW,但是不并没有创建对象,WITH  AS 公用表表达式不创建对象,只能被后随的SELECT语句,其作用: 1. 实现递归查询(树形结构) 2. 可以在一个 ...

  7. Delphi实现树型结构具体实例

    unit Unit1;interfaceuses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...

  8. java实现树型结构样式

    import javax.swing.*; import javax.swing.event.*; import javax.swing.tree.*; public class Root exten ...

  9. web api+递归树型结构

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...

随机推荐

  1. Git资料

    git作为流行的分布式版本管理系统,虽然入门有点难,不过学会还是很有用的,尤其是针对技术人员.它从2005年Linux作者两周用C++写的一个版本发展到现在这么流行也是有原因的. Downloads  ...

  2. tomcat自启动脚本

    1.#cd /etc/rc.d/init.d2.#vi tomcat3.把下面的代码保存为tomcat文件,并让它成为可执行文件 chmod 755 tomcat. #!/bin/sh # # /et ...

  3. TimeHelp 获取时间戳转换类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Shar ...

  4. (转)ASP与sql存储过程

    本文转载自:http://www.cnblogs.com/Spring/archive/2006/10/18/532817.aspx ASP与存储过程(Stored Procedures)的文章不少, ...

  5. Java--不可覆盖的方法

    私有方法不能被覆盖: 因为private被自动认为final,对子类是屏蔽的,那么子类中的相同方法就是一个新的方法,编译器不会报错但也不会按期望运行: public class ClassA { pr ...

  6. js将UTC时间转化为当地时区时间(UTC转GMT)

    我们在进行网站开发的时候有可能会涉及到国外的用户或者用户身在国外,这时就会存在时差问题,比如说我们在中国的时间是08:00,但是此时韩国的时间是09:00,如果在网页上需要进行相关显示的话就会出现问题 ...

  7. Linux 性能分析的前 60 秒

    编译自:http://techblog.netflix.com/2015/11/linux-performance-analysis-in-60s.html作者: Brendan Gregg转载自:h ...

  8. Ansible 系列之 Inventory 资源清单介绍

    一.Inventory 库存清单文件 1.Inventory 作用 Ansible 可以在同一时间针对多个系统设施进行管理工作.它通过选择Ansible 资源清单文件中列出的系统,该清单文件默认是在/ ...

  9. C++ - 容器概述

    一 迭代器iterator 5种类别 常用的迭代器 常用的迭代器 二 分配算符Allocators 三 容器简介 STL标准容器类简介 标准容器类 说明 顺序性容器 关联容器 容器适配器 所有标准库共 ...

  10. 实战zabbix3.0.2 使用percona mysql插件监控mysql5.7

    1.系统环境 [root@shard0 templates]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.2 ...