javascript加jsp实现树型结构时如何从数据库中读取节点并显示(200分)

  • 主题发起人 主题发起人 刘牵手
  • 开始时间 开始时间

刘牵手

Unregistered / Unconfirmed
GUEST, unregistred user!
一定给分!
 
问题,说清楚一点
 
哦哦,刚写了一个,该你参考一下。我是使用treemaker的js脚本。
所以只要构造一个treearray就可以了。
package ×××;
import ×××;
import java.sql.SQLException;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import java.lang.StringBuffer;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.io.*;
import java.util.*;
public class Organ extends BaseClass{
private int organ_id, parent_id;
private String organ_name;
private StringBuffer htmlCode=new StringBuffer("");
private String urlprifix="list.jsp?organ_id=";
private ArrayList organArray;
final int MAXID=5000;
//因为标号不连续,所以开个大数组
private boolean parentorganArray[]=new boolean[MAXID];


/**
* 把纪录集存放在在一个arraylist中
* @param
* @return arraylist
*/


private void createOrganArrayList() throws Exception{

organArray=new ArrayList();

try{
getConnect();
st=conn.createStatement();
sql="select * from v_organ order by organ_id";
rs=st.executeQuery(sql);
while(rs.next()){
organ_id=rs.getInt("organ_id");
organ_name=rs.getString("name");
parent_id=rs.getInt("parent_id");
Depart dt=new Depart(organ_id,organ_name,parent_id);
organArray.add(dt);

}
//创建父节点连表


sql="select parent_id from v_organ group by parent_id";
rs=st.executeQuery(sql);
while(rs.next()){
int index=rs.getInt("parent_id");
parentorganArray[index]=true;

}




}catch(Exception e){
throw e;
}finally{
disConnect();
}



}


/**
* 取得孩子节点
* @param
* @return arraylist
*/



private ArrayList getChildren(int parent_id){

ArrayList children=new ArrayList();
for (int i=0;
i<organArray.size();
i++) {

int temp=((Depart)organArray.get(i)).getParent_id();
int temp2=((Depart)organArray.get(i)).getOrgan_id();

if(temp==parent_id&amp;&amp;temp!=temp2) {

children.add(organArray.get(i));

}

}


return children;
}


/**
* 判断是否为父节点
* @param organ_id
* @return
*/
private boolean isParent(int organ_id){


if(parentorganArray[organ_id]) {

return true;
}
else
{
return false;

}
}

/**
*使用递归算法
* 创建子树
* @param organ_id, layer
* throws exception
*/

private void subTree (int organ_id, String organ_name, int layer) throws Exception {


if(isParent(organ_id)){


htmlCode.append("0,/""+organ_name+"/","+"/"/",/"/",/"/",/"/",3,/"/","+"/""+urlprifix+organ_id+"/",/"/"");

//处理叶子节点

htmlCode.append(",-1,");
//叶子开始标记

ArrayList al=getChildren(organ_id);

if( al.size()>0) {

organ_id=((Depart)al.get(0)).getOrgan_id();
organ_name=((Depart)al.get(0)).getOrganName();
subTree(organ_id,organ_name,layer+1);

}

for(int i=1;
i<al.size();
i++){ //从1开始循环

htmlCode.append(",");

organ_id=((Depart)al.get(i)).getOrgan_id();
organ_name=((Depart)al.get(i)).getOrganName();
subTree(organ_id,organ_name,layer+1);

}



htmlCode.append(",1");
//叶子结束标记

return;

}else
{

htmlCode.append("2,/""+organ_name+"/","+"/"/",/"/",/"/",3,/"/","+"/""+urlprifix+organ_id+"/",/"/"");

return;


}




}



public String getTreeNodesArray() {

try{

createOrganArrayList();
//创建数组连表

int organ_id=1; //开始的父亲节点标号

ArrayList al=getChildren(organ_id);


htmlCode.append("var tree_node=new Array(");
//数组开始标记


if( al.size()>0) {

organ_id=((Depart)al.get(0)).getOrgan_id();
organ_name=((Depart)al.get(0)).getOrganName();
subTree(organ_id,organ_name,1);

}


for(int i=1;
i<al.size();
i++) {//从1开始循环

htmlCode.append(",");

organ_id=((Depart)al.get(i)).getOrgan_id();
organ_name=((Depart)al.get(i)).getOrganName();
subTree(organ_id,organ_name,1);

}

htmlCode.append(",5);");
//数组结束标记

}catch(Exception e){

e.printStackTrace();
}

return htmlCode.toString();
}





//javabean test
public static void main(String args[]){

try{


Organ og=new Organ();
og.startTime();
//开始时间

//System.out.println(og.getTreeNodesArray());

//save to disk

try {
BufferedWriter out = new BufferedWriter(new FileWriter("e://outfilename"));
out.write(og.getTreeNodesArray());
out.close();
}catch (IOException e) {

e.printStackTrace();
}


og.printTime("run time(毫秒):");

}catch(Exception e){

e.printStackTrace();
}

}

}
/**
*部门类
*
*/
class Depart{

private int organ_id, parent_id;
private String organ_name;


Depart(int organ_id ,String organ_name, int parent_id){

this.organ_id=organ_id;
this.organ_name=organ_name;
this.parent_id=parent_id;


}

/**
*设置部门的属性
* @param organ_id,organ_name,parent_id
* @return
*/


public void setDepart(int organ_id ,String organ_name, int parent_id){

this.organ_id=organ_id;
this.organ_name=organ_name;
this.parent_id=parent_id;

}

/**
*取得部门的名字
* @param
* @return
*/


public String getOrganName(){

return this.organ_name;
}

/**
*取得部门的id
* @param
* @return
*/
public int getOrgan_id(){

return this.organ_id;
}




/**
*取得部门父id
* @param
* @return
*/
public int getParent_id(){

return this.parent_id;
}

public String toString(){

String out="[ "+organ_id+" , "+organ_name+ " , "+ parent_id +" ]/n";
return out;
}


}

 
刘牵手
我有一个很简练的
告诉我你的信箱,给你代码
 
alaclp@263.sina.com
 
请给我一份,谢谢:hahong@163.com
 
我贴出来吧
<%@ page contentType="text/html;
charset=gb2312"%>
<%@ page language="java" import="java.sql.*"%>
<jsp:useBean id = "qry" scope = "request" class = "dbConn.ViewQueryBean"/>
<HTML>
<HEAD>
<script language="javascript" src=createtree.js></script>
</HEAD>
<H1>无限级树</H1>
<%
/*
create table NODESTABLE
(
A1 VARCHAR2(20),
ID VARCHAR2(10),
PID VARCHAR2(10),
NAME VARCHAR2(50)
)
最顶层的结点PID为0
*/
String sControl="";
String sOutput="";
StringBuffer sBuff = new StringBuffer();
qry.openConnection();
ResultSet rs=qry.openQuery("select * from nodestable ");
sBuff.append("<script LANGUAGE=/"JavaScript/">/n");
//下面加树的控件
sBuff.append(" var strStyle=/'"+"<style type=/"text/css/">"+"/'/n");
sBuff.append(" strStyle+=/'"+"span.SelectIng{color:#0000ff;border:solid 1pt #93BEE2;padding:0.5pt;height:4px;background-color:#93BEE2;}"+"/'/n");
sBuff.append(" strStyle+=/'"+"span.NoSelect{color:#000000;border:solid 1pt #93BEE2;padding:0.5pt;height:4px;background-color:#93BEE2;}"+"/'/n");
sBuff.append(" strStyle+=/'"+"span.SelectEd{color:#0000FF;border:solid 1pt #000000;padding:0.5pt;height:4px;background-color:#ffffff;}"+"/'/n");
sBuff.append(" strStyle+=/'"+"</style>"+"/'/n");
sBuff.append("do
cument.write(strStyle) /n /n");
sBuff.append(" var myRs=new Array(), selectItem;
/n");
sBuff.append(" function compare(a,b) {return parseInt(a[1]) - parseInt(b[1])} /n");
sBuff.append(" function InsertItem(s){ /n");
sBuff.append(" myRs[myRs.length++] = s.split(/,/);} /n /n");
sBuff.append(" function ExCloAll(n){ /n");
sBuff.append(" var d=document.all(/'"+"0/'), e=d.all.tags(/'"+"div/') /n");
sBuff.append(" for (i=0;i<e.length;i++)e.style.display=(n==1?/'"+"block/'"+":/'"+"none/') /n");
sBuff.append(" e=d.all.tags(/'"+"p/'"+") /n");
sBuff.append(" for (i=0;i<e.length;i++){if(ChkExist(e.children[1].value))e.children[0].innerText=(n==1?/'"+"- /'"+":/'"+"+ /')}} /n /n");
sBuff.append(" function ExCloItem(){ /n");
sBuff.append(" var c,e=event.srcElement, p=e.parentElement.children /n");
sBuff.append(" if(selectItem!=null)selectItem.className=/'"+"NoSelect/'"+";
/n");
sBuff.append(" e.className=/'"+"SelectEd/'"+" /n");
sBuff.append(" selectItem=event.srcElement;
/n");
sBuff.append(" if(c=document.all(e.value)){ /n");
sBuff.append(" pop[0].innerText=(p[0].innerText==/'"+"+ /'"+"?/'"+"- /'"+":/'"+"+ /'"+") /n");
sBuff.append(" }else
{p[0].innerText=/'"+"- /'"+"}} /n /n");
sBuff.append(" function ExCloItem2(){ /n");
sBuff.append(" var c,e=event.srcElement, p=e.parentElement.children /n");
sBuff.append(" if(c=document.all(p[1].value)){ /n");
sBuff.append(" e.innerText=(e.innerText==/'"+"+ /'"+"?/'"+"- /'"+":/'"+"+ /') /n");
sBuff.append(" c.style.display=(c.style.display==/'"+"block/'"+"?/'"+"none/'"+":/'"+"block/'"+") /n");
sBuff.append(" }else
e.innerText=/'"+"- /'} /n /n");
sBuff.append(" function OutItem(){ /n");
sBuff.append(" var e=event.srcElement /n");
sBuff.append(" if(selectItem != /'"+"/'"+")e.className=(selectItem==e?/'"+"SelectEd/'"+":/'"+"NoSelect/'"+") /n");
sBuff.append(" else
e.className=/'"+"NoSelect/'"+"} /n /n");
sBuff.append(" function ChkExist(n){ /n");
sBuff.append(" n=parseInt(n) /n");
sBuff.append(" if((n<myRs[0][1])||(n>myRs[myRs.length-1][1]))return false /n");
sBuff.append(" var a,b,c,x=0, y=parseInt(myRs.length/2), z=myRs.length-1 /n");
sBuff.append(" while((x!=y)&amp;&amp;(y!=z)){ /n");
sBuff.append(" a=myRs[x][1], b=myRs[y][1], c=myRs[z][1] /n");
sBuff.append(" if(n==a||n==b||n==c)return true /n");
sBuff.append(" if(n>b)x=y;
/n");
sBuff.append(" else
z=y;y=parseInt(x+z);
/n");
sBuff.append(" y=parseInt((x+z)/2);
/n");
sBuff.append(" }return false} /n /n");
sBuff.append(" function drawTree(n){ /n");
sBuff.append(" if(n==0){ /n");
sBuff.append(" myRs.sort(compare) /n");
sBuff.append(" do
cument.write(/'"+"<p style=/"font:10pt;cursor:hand;/"><span onclick=/"ExCloAll(1)/">全部展开</span>&amp;nbsp;&amp;nbsp;<span onclick=/"ExCloAll(0)/">全部合上</span></p>/'"+")} /n /n");
sBuff.append(" if (ChkExist(n)!=true) return /n");
sBuff.append(" do
cument.write(/'"+"<div id=/"/'"+"+n+/'"+"/" style=/"font:10pt;cursor:hand;/'"+"+(n!=0?/'"+"position:relative;left:20;display:none;/'"+":/'"+"/'"+")+/'"+"/">/'"+") /n");
sBuff.append(" for(var i=0;i<myRs.length;i++){ /n");
sBuff.append(" if (parseInt(myRs[1])>n)break;
/n");
sBuff.append(" if (myRs[1]==n){ /n");
sBuff.append(" do
cument.write(/'"+"<p style=/"margin:0pt;/"><span style=/"vertical-align:top;/" onclick=/"ExCloItem2()/">/'"+"+(ChkExist(myRs[0])?/'"+"+ /'"+":/'"+"- /'"+")+/'"+"</span>/'"+") /n");
sBuff.append(" do
cument.write(/'"+"<span onclick=/"ExCloItem()/" value=/"/'"+"+myRs[0]+/'"+"/" class=/"NoSelect/" onmouseout=/"OutItem()/" onmouseover=/"this.className="+"//'SelectIng"+"//'/">/'"+"+myRs[2]+/'"+"</span></p>/'"+") /n");
sBuff.append(" drawTree(myRs[0])}} /n");
sBuff.append(" do
cument.write(/'"+"</div>/'"+")} /n /n");
//加上数据库里的结点
while (rs.next()){
sOutput=rs.getString("id")+","+rs.getString("pid")+","+rs.getString("name");
sBuff.append(" InsertItem(/'"+sOutput+"/') /n");
}
sBuff.append(" drawTree(0) /n /n");
sBuff.append("</script>");
sControl = sBuff.toString();
out.println(sControl);
try{
qry.close_Resource(qry.getThisConn(),qry.getThisStatement());
}catch(Exception e){
out.println(e);
}

%>
<body >
</BODY>
</HTML>
 
连接你们自己设置
create table NODESTABLE
(
A1 VARCHAR2(20),
ID VARCHAR2(10),
PID VARCHAR2(10),
NAME VARCHAR2(50)
)
A1 id pid name
0 10 o 中国
1 1001 10 福建
2 1002 10 北京
3 100101 1001 福州
4 100201 1002 北京市
5 10010101 100101 台江区
可以吧这个jsp写成javabean,这样通过传几个参数达到公用
 
参考
http://www.java-cn.com
 
后退
顶部