JSP分頁檔分類樹源碼(遞迴演算法)

來源:互聯網
上載者:User
js|遞迴|演算法|頁面 <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312"; >
<script language="JavaScript">
<!--
function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>
<link href="css/ip.css" rel="stylesheet" type="text/css">
</head>

<body topmargin = "0" leftmargin = "0" marginheight = "0" marginwidth = "0" bgcolor="#FFFFFF">
<table border="0" cellspacing="0" cellpadding="0" height="80%">
<tr><td>
</td>
</tr>
<tr valign="top">
<td height="223"> <span id="menus"></span>
<span id="view"></span>

</td>
</tr>
<tr valign="bottom">
<td> </td>
</tr>
</table>
<div align="right"></div>
</body>
<script>
/**
* 構造樹,初值為0
*/
function isbrother()
{
var is=flase;

}
function tree(n) {
var id = new Array("bar","pad","<IMG src='http://www.webjx.com/htmldata/2005-08-02/images/midnodeline.gif'border=0 align='absmiddle'><IMG src='http://www.webjx.com/htmldata/2005-08-02/images/close.gif 'border=0 align='absmiddle'>","<IMG src='http://www.webjx.com/htmldata/2005-08-02/images/midclosedfolder.gif 'border=0 align='absmiddle'><IMG src='http://www.webjx.com/htmldata/2005-08-02/images/close.gif 'border=0 align='absmiddle'>","<IMG src='http://www.webjx.com/htmldata/2005-08-02/images/lastnodeline.gif'border=0 align='absmiddle'><IMG src='http://www.webjx.com/htmldata/2005-08-02/images/close.gif 'border=0 align='absmiddle'>");
if(n == 0) { // 初始設定變數
n = 1;
i = 0;
s = "";
}
s += "<table border=0 cellspacing=0 cellpadding=0 >";
for(;i<tree_ar.length-1;i++) {
var k = (n >= tree_ar[i+1][0])?0:1;
if(tree_ar[i][2]==false)
{
id[2]="<IMG src='http://www.webjx.com/htmldata/2005-08-02/images/lastnodeline.gif'border=0 align='absmiddle'><IMG src='http://www.webjx.com/htmldata/2005-08-02/images/close.gif 'border=0 align='absmiddle'>";
}
s += "<tr id='"+id[k]+"' value="+i+"><td >"+id[k+2]+"</td><td>"+tree_ar[i][1]+"</td></tr>"; // // 構造節點,注意這裡的自訂屬性value。作用是簡化構造節點的描述,共用參數數組資訊。
if(n > tree_ar[i+1][0]) { // 若期望層次大於當前層次,結束本層次返回上一層次。
s += "</td></tr></table>";
return tree_ar[i+1][0];
}
if(n < tree_ar[i+1][0]) { // 若期望層次小於當前層次,遞迴進入下一層次。
s += "<tr style='display:none' ><td ";
if(tree_ar[i][2]==true)
{ //如果有下個兄弟節點,則畫兄弟節點間的連線
s+="background='images/line.gif' border=0 align='absmiddle'";
}

s+="></td><td>";
var m = tree(tree_ar[++i][0]);
s += "</td></tr>";
if(m < n) { // 當遞迴傳回值小於當前層次期望值時,將產生連續的返回動作
s += "</table>";
return m;
}
}
}
s += "</table>";
return s;
}
</script>

<script for=pad event=onclick>
// 分枝節點的點擊響應
v = this.parentElement.rows[this.rowIndex+1].style;
if(v.display == 'block') {
v.display = 'none';
this.cells[0].innerHTML = "<IMG src='http://www.webjx.com/htmldata/2005-08-02/images/midclosedfolder.gif 'border=0 align='absmiddle'><IMG src='http://www.webjx.com/htmldata/2005-08-02/images/close.gif 'border=0 align='absmiddle'>";
view.innerHTML = "";// 自行修改為參數數組定義的閉合動作
}else {
v.display = 'block';
this.cells[0].innerHTML = "<IMG src='http://www.webjx.com/htmldata/2005-08-02/images/midopenedfolder.gif 'border=0 align='absmiddle'><IMG src='http://www.webjx.com/htmldata/2005-08-02/images/open.gif 'border=0 align='absmiddle'>";
// view.innerHTML = "<b>"+tree_ar[this.value][1]+"</b>"; // 自行修改為參數數組定義的展開動作
}

/**
* 以下代碼用於關閉已展開的其他分枝
* 如需自行關閉展開的分枝則從這裡直接返回或刪去這段代碼
*/
if(! tree_ar[this.value].type) //如該節點為首次進入,則記錄所在層次資訊
genTreeInfo(this);
var n = 1*this.value+1;
for(i=n;i<tree_ar.length-1;i++) { // 關閉排列在當前節點之後的樹
if(tree_ar[i].type == "pad") {
tree_ar[i].obj2.style.display = 'none';
tree_ar[i].obj1.cells[0].innerHTML = "<IMG src='http://www.webjx.com/htmldata/2005-08-02/images/midclosedfolder.gif 'border=0 align='absmiddle'><IMG src='http://www.webjx.com/htmldata/2005-08-02/images/close.gif 'border=0 align='absmiddle'>";
}
}
while(tree_ar[--n][0] > 1); //回溯到當前樹的起點
while(--n >= 0) // 關閉排列在當前樹的起點之前的樹
if(tree_ar[n].type == "pad") {
tree_ar[n].obj2.style.display = 'none';
tree_ar[n].obj1.cells[0].innerHTML = "<IMG src='http://www.webjx.com/htmldata/2005-08-02/images/midclosedfolder.gif 'border=0 align='absmiddle'><IMG src='http://www.webjx.com/htmldata/2005-08-02/images/open.gif 'border=0 align='absmiddle'>";
}


/** 記錄層次資訊,用以簡化遍曆樹時的複雜的節點描述 **/
function genTreeInfo(o) {
var el = o.parentElement;
for(var i=0;i<el.rows.length;i++) {
if(el.rows[i].id != "") {
tree_ar[el.rows[i].value].type = el.rows[i].id;
}
if(el.rows[i].id == "pad") {
tree_ar[el.rows[i].value].obj1 = el.rows[i];
tree_ar[el.rows[i].value].obj2 = el.rows[i+1];
}
}
}
</script>

<script for=bar event=onclick>
// 無分枝節點的點擊響應
//view.innerHTML = "<b>"+tree_ar[this.value][1]+"</b>"; // // 自行修改為參數數組定義的點擊動作
// 自行修改為參數數組定義的點擊動作

</script>


<script for=bar event=onclick>
// 無分枝節點的點擊響應
//view.innerHTML = "<b>"+tree_ar[this.value][1]+"</b>"; // \uFFFD\uFFFD\uFFFDs\uFFFDC\uFFFD\uFFFD?\uFFFDQ\uFFFD\uFFFD\uFFFD\uFFFD?\uFFFD\uFFFD?\uFFFDI\uFFFD_??\uFFFD\uFFFD
</script>

<script>
/**
* 基本參數數組,根據具體應用自行擴充
* 資料可較簡單的由伺服器端提供
* 列1:節點層次
* 列2:節點標題
* 列3: 有無下個兄弟節點
* 其餘自行擴充
*/
tree_ar = new Array(

<%

FileTree tree=new FileTree();
String array=tree.showFileTree("E:\\");

%>
<%=array%>
new Array(1,"",false) // 為簡化終止判斷附加的空資料項目
);


/*** 建立菜單 ***/
menus.innerHTML =tree(0);
</script>
</html>





import java.io.File;

public class FileTree {
public FileTree() {
}

/**
*
* 組建檔案分類樹
*
* @param path
* 目錄路徑
* @return 返回jsp 頁面所需的目錄數組(JavaScript 數組)
*
*/
/*
* 有無兄弟節點
*/
private boolean hasNexetBrother = false;
/*
* 節點層次
*/
private int index=1;

public String showFileTree(String path) {

String s = "";
File dir = new File(path);
if (dir != null && dir.isDirectory()) {
String absolutePath = dir.getAbsolutePath();


absolutePath = absolutePath.replaceAll("\\\\", "\\\\\\\\");//對'\'的處理,在JSP參數傳遞時'\'和空格會出現錯誤
absolutePath = absolutePath.replaceAll(" ", "@"); //對空格的處理 ,這時用'@'替代
s += "new Array(" + index + ",'<a href=fileList.jsp?path="
+ absolutePath + "\\\\ target=mainFrame>" + dir.getName()
+ "</a>'," + hasNexetBrother + "), ";
hasNexetBrother = false;
index++;
File[] dirs = dir.listFiles();
java.util.List list = new java.util.ArrayList();
for (int i = 0; i < dirs.length; i++) {
if (dirs[i].isDirectory()) {//去除檔案.得到資料夾清單

list.add(dirs[i]);

}
}
for (int i = 0; i < list.size(); i++) {
if (i != list.size() - 1)
hasNexetBrother = true;
String dirpath = ((File) list.get(i)).getAbsolutePath();
s += showFileTree(dirpath);//對子檔案夾遞迴處理

}
index--;
}

return s;
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.