开发平台:Windows XP SP2
测试平台:FreeBSD 7.0
开发工具:Netbeans 6.1
使用框架:Zend Framework 1.5.2
数据库: MySQL 5.0.51a
**********************************
所需的数据库表和ZF相关目录以及文件:
一、表:(可以按MZ中的数据库来设计,视情况而定)
字段名 字段的具体说明
id 节点ID(不可为0,可以是数字或字符) parentId 本节点的父节点ID(若本节点已为根节点,此处填0)
text 节点的显示文本(一般不允许为空,不过有一种情况例外,即根节点,若根节点文本为空,这个根节点将不会在页面里显示)
hint 节点的说明注解
icon 节点的图标,MzTreeView 1.0允许每个节点拥有不同的图标(对应的名字在类里的icons和iconsExpand定义)
data 节点挂的数据,格式是 param=value¶m=value&... url里?后的那串字符串格式,
url 每个节点允许拥有不同的链接,它为空或者为#时,树里这个节点的链接,点击将无反应
target 每个节点的链接允许在不同的target里打开,为空时取类里的默认值
method 点击该链接时所想触发的脚本语句
特注:每个字段值中不可有冒号: 不可以换行 引号酌情考虑应不与节点字符串的引号相冲突
我的为:
mysql> select * from module_tree;
+----+-----+----------+------+------+------+------+--------+--------+
| id | pid | text | hint | icon | data | url | target | method |
+----+-----+----------+------+------+------+------+--------+--------+
| 1 | 0 | aaa | aaaa | a | a | b | main | |
| 2 | 1 | computer | aaaa | a | a | b | main | |
| 3 | 1 | pet | aaaa | a | a | b | main | |
| 4 | 1 | ant | aaaa | a | a | b | main | |
| 5 | 2 | ketty | aaaa | a | a | b | main | |
| 6 | 2 | fav | aaaa | a | a | b | main | |
| 7 | 3 | pig | aaaa | a | a | b | main | |
| 8 | 3 | dog | aaaa | a | a | b | main | |
| 9 | 6 | cat | aaaa | a | a | b | main | |
| 10 | 6 | apache | aaaa | a | a | b | main | |
| 11 | 9 | tomcat | aaaa | a | a | b | main | |
| 12 | 10 | pet | aaaa | a | a | b | main | |
| 13 | 11 | vert | aaaa | a | a | b | main | |
| 13 | 11 | vert | aaaa | a | a | b | main | |
+----+-----+----------+------+------+------+------+--------+--------+
二、目录:
三、相关文件:
1.index.php //入口文件
2.TestDbCon.phhp //数据库连接文件
3.ModuleTree //抽象出来的数据库表文件
4.TestController.php //控制器
5.mz-tree.phtml //树状菜单生成页面,包含mzTree.js对象,并根据服务器取回的数据生成的树状菜单。
相关文件内容:
1.index.php //入口文件
1. <?php
2.
3. set_include_path('.' . PATH_SEPARATOR .'../library' . PATH_SEPARATOR . get_include_path() . PATH_SEPARATOR . '../application/modules/default/models' . PATH_SEPARATOR . '../application/modules/admin/models');
4.
5. require_once 'Zend/Controller/Front.php';
6.
7. require_once 'Zend/Controller/Router/Route.php';
8.
9.
10.
11. $ctrl=Zend_Controller_Front::getInstance();
12.
13. $ctrl->addModuleDirectory('../application/modules');
14.
15. $ctrl->throwExceptions(true);
16.
17. $ctrl->dispatch();
18.
19. ?>
2.TestDbCon.phhp //数据库连接文件
1. <?php
2.
3. require_once 'Zend/Db.php';
4.
5. require_once 'Zend/Registry.php';
6.
7.
8.
9. class TestDbCon{
10.
11. public static function getTestDbCon(){
12.
13. $params=array(
14.
15. 'host'=>'localhost',
16.
17. 'username'=>'root',
18.
19. 'password'=>'123456',
20.
21. 'dbname'=>'test'
22.
23. );
24.
25. $con=Zend_Db::factory('Pdo_Mysql',$params);
26.
27. return $con;
28.
29. }
30.
31. }
32.
33. ?>
3.ModuleTree //抽象出来的数据库表文件
<?php
/**
* PHP Template.
*/
require_once 'Zend/Db/Table/Abstract.php';
class ModuleTree extends Zend_Db_Table_Abstract
{
protected $_name='module_tree';
protected $_primary='id';
protected $_sequence=TRUE;
}
?>
4.TestController.php //控制器
<?php
require_once 'Zend/Controller/Action.php';
require_once 'Zend/View.php';
require_once 'ModuleTree.php';
require_once 'TestDbCon.php';
class TestController extends Zend_Controller_Action{
public function mzTreeAction(){
$dbCon=DbCon::getDbCon();
$mt_tb=new ModuleTree(array('db'=>$dbCon));
$rowSet=$mt_tb->fetchAll();
$this->view->rowSet = $rowSet;
$this->render();
}
}
?>
5.mz-tree.phtml //树状菜单生成页面,包含mzTree.js对象,并根据服务器取回的数据生成的树状菜单。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
<head>
<title>MzTree test</title>
<script language="JavaScript" src="/public/scripts/MzTreeView10.js"></script>
</head>
<body>
<div >
<script language="JavaScript">
var tree = new MzTreeView("tree");
tree.setIconPath("/public/images/"); //注意这里的路经
tree.icons["property"] = "property.gif";
tree.icons["css"] = "collection.gif";
tree.icons["book"] = "book.gif";
tree.iconsExpand["book"] = "bookopen.gif"; //展开时对应的图片
<?php
foreach ($this->rowSet as $row){
echo "tree.nodes[/"" . $row->pid . "_" . $row->id . "/"] = /"text:" . $row->text . "; url:" . $row->url . "; target:" . $row->target . "/";";
}
?>
tree.setURL("Catalog.asp");
tree.setTarget("MzMain");
document.write(tree.toString());
</script>
</div>
</body>
</head>