|
Posted on 2011-01-22 09:36 viery 閱讀(1558) 評(píng)論(0) 編輯 收藏
要實(shí)現(xiàn)上面的導(dǎo)航樹我們不需要做太多的東西就可以完成
首先我們需要從官網(wǎng)上下載 js 文件 http://destroydrop.com/javascripts/tree/
之后新建一個(gè) html 文件 引入下載文件中的 css 樣式 和 js 文件 調(diào)用方法即可!
<html>
<head>
<title>js樹 ant</title>
<link rel="StyleSheet" href="dtree.css" type="text/css" />
<script type="text/javascript" src="dtree.js"></script>
</head>
<body>
<div class="dtree">
<p><a href="javascript: d.openAll();">open all</a> | <a href="javascript: d.closeAll();">close all</a></p>
<script type="text/javascript">
d = new dTree('d');
d.add(0,-1,'客戶關(guān)系管理系統(tǒng)');
d.add(1,0,'營銷管理','example01.html');
d.add(2,1,'銷售機(jī)會(huì)管理','example01.html');
d.add(3,1,'客戶開發(fā)計(jì)劃','example01.html');
d.add(4,0,'客戶管理','example01.html');
d.add(5,4,'客戶信息管理','example01.html');
d.add(6,4,'客戶流失管理','example01.html');
d.add(7,0,'服務(wù)管理','example01.html');
d.add(8,7,'服務(wù)創(chuàng)建','example01.html');
d.add(9,7,'服務(wù)分配','example01.html');
d.add(10,7,'服務(wù)處理','example01.html');
d.add(11,7,'服務(wù)反饋','example01.html');
d.add(12,7,'服務(wù)歸檔','example01.html');
d.add(13,0,'統(tǒng)計(jì)報(bào)表','example01.html');
d.add(14,13,'客戶貢獻(xiàn)分析','example01.html');
d.add(15,13,'客戶構(gòu)成分析','example01.html');
d.add(16,13,'客戶服務(wù)分析','example01.html');
d.add(17,13,'客戶流失分析','example01.html');
d.add(18,0,'基礎(chǔ)數(shù)據(jù)','example01.html');
d.add(19,18,'數(shù)據(jù)字典管理','example01.html');
d.add(20,18,'查詢產(chǎn)品信息','example01.html');
d.add(21,18,'查詢庫存','example01.html');
document.write(d);
</script>
</div>
</body>
</html>
|
這樣做的好處是我們不需要自己寫 js 代碼框架已經(jīng)為我們封裝好了!我們只需要調(diào)用存在的方法即可!
對(duì)于 tree.add(id,pid,name,url,title,target,icon,iconOpen,open); 方法中的參數(shù)這里有必要說一下
Parameters
Name |
Type |
Description |
id |
Number |
Unique identity number. |
pid |
Number |
Number refering to the parent node. The value for the root node has to be -1. |
name |
String |
Text label for the node. |
url |
String |
Url for the node. |
title |
String |
Title for the node. |
target |
String |
Target for the node. |
icon |
String |
Image file to use as the icon. Uses default if not specified. |
iconOpen |
String |
Image file to use as the open icon. Uses default if not specified |
open |
Boolean |
Is the node open. |
Example
mytree.add(1, 0, 'My node', 'node.html', 'node title', 'mainframe', 'img/musicfolder.gif');
上面是引自 dtree api 中的描述
下面是翻譯后的描述:
id :節(jié)點(diǎn)自身的id
pid :節(jié)點(diǎn)的父節(jié)點(diǎn)的id
name :節(jié)點(diǎn)顯示在頁面上的名稱
url :節(jié)點(diǎn)的鏈接地址
title :鼠標(biāo)放在節(jié)點(diǎn)上所出現(xiàn)的提示信息
target :節(jié)點(diǎn)鏈接所打開的目標(biāo)frame(如框架目標(biāo)mainFrame或是_blank,_self之類)
icon :節(jié)點(diǎn)關(guān)閉時(shí)的顯示圖片的路徑
iconOpen:節(jié)點(diǎn)打開時(shí)的顯示圖片的路徑
open :布爾型,節(jié)點(diǎn)是否打開(默認(rèn)為false)
===========================================================================================
Ok 按照上面的方式我們就可以很快的創(chuàng)建一個(gè)樹形目錄,但是如果管理員想增加一個(gè)功能或者移除一個(gè)功能的話,那么就需要更改頁面中 js 中的代碼。能不能將所有的節(jié)點(diǎn)信息存放在數(shù)據(jù)庫中,在加載這個(gè) jsp/html 頁面的時(shí)候讀取出數(shù)據(jù)呢 ?
為此我們需要設(shè)計(jì)一個(gè)數(shù)據(jù)庫和表,建一張 DTree 表,表中有九列沒分別為 add 方法中的參數(shù)
下面是數(shù)據(jù)庫的腳本 不想自己寫了 在網(wǎng)上找了一個(gè)
use master
go
if exists (select * from sysdatabases where name = 'tree')
drop database tree
go
create database tree
go
use tree
go
create table dtree
(
id int identity(1,1) primary key, --節(jié)點(diǎn)編號(hào)
pid int, --父節(jié)點(diǎn)
[name] varchar(20), --節(jié)點(diǎn)名稱
url varchar(20), --連接地址
title varchar(20), --節(jié)點(diǎn)描述
target varchar(20), --Target
icon varchar(20), --圖標(biāo)
iconOpen varchar(20), --展開狀態(tài)下的圖標(biāo)路徑
[open] varchar(20) --是否展開
)
insert into dtree values('-1','Y106班','index.jsp','班級(jí)','blank','','','')
insert into dtree values('1','教師','teacher.jsp','教師','blank','','','')
insert into dtree values('1','班干部','depater.jsp','班干','blank','','','')
insert into dtree values('1','組長','lady.jsp','組員','blank','','','')
insert into dtree values('1','學(xué)員','stu.jsp','學(xué)員','blank','','','')
insert into dtree values('1','班主任','leader.jsp','班主任','blank','','','')
insert into dtree values('1','教員','teacher.jsp','教員','blank','','','')
insert into dtree values('2','班長','index.jsp','班長','blank','','','')
insert into dtree values('2','學(xué)委','index.jsp','學(xué)委','blank','','','')
insert into dtree values('4','學(xué)員1','stu.jsp','學(xué)員','blank','','','')
insert into dtree values('4','學(xué)員2','stu.jsp','學(xué)員','blank','','','')
insert into dtree values('4','學(xué)員3','stu.jsp','學(xué)員','blank','','','')
insert into dtree values('4','學(xué)員4','stu.jsp','學(xué)員','blank','','','')
insert into dtree values('4','學(xué)員5','stu.jsp','學(xué)員','blank','','','')
insert into dtree values('4','學(xué)員6','stu.jsp','學(xué)員','blank','','','')
insert into dtree values('4','學(xué)員7','stu.jsp','學(xué)員','blank','','','')
Go
select id,pid,[name],url,title,target,icon,iconOpen,[open] from dtree
|
編寫一個(gè)數(shù)據(jù)庫訪問類 并返回 DTree 表中的數(shù)據(jù)
DBManager.java 數(shù)據(jù)庫訪問類 并取得 DTree 表中的所有數(shù)據(jù)
package com.ant.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.ant.po.DTree;
public class DBManager {
private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL="jdbc:sqlserver://localhost:1433;databaseName=tree";
private static final String SA="sa";
private static final String PWD="sqlpwd";
static private Connection conn;
public DBManager() {
getConnection();
}
//打開連接
public static void getConnection(){
try {
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL,SA,PWD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
//關(guān)閉連接
public static void closeConnection(){
if(conn!=null){
try {
if(!conn.isClosed()){
conn.close();
conn=null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//執(zhí)行查詢
public static ResultSet executeQuery(String sql){
getConnection();
Statement st;
try {
| |