<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Tree.aspx.cs" Inherits="MyTeachers.web.Tree" %>
<!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" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server" Height="257px" ImageSet="WindowsHelp"
Width="142px" Target="rightFrame">
</asp:TreeView>
</div>
</form>
</body>
</html>
后台
using System
using System.Collections.Generic
using System.Linq
using System.Web
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.Security
using System.Web.UI.WebControls.WebParts
using System.Web.UI.HtmlControls
using System.Data
namespace MyTeachers.web
{
public partial class Tree : System.Web.UI.Page
{
DataView dv
DataTable dt
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
int id =Convert.ToInt32(Session["userID"])
//数据库执行查询的方法就不写了。
//Operator op = new Operator()
//这里执行的是:select * from tabmenu
dt = BLL.tabMenuBLL.GetAlltabMenut(id)
//第一次加载时调用方法传参
CreateTree(0, null, dt, this.TreeView1)
}
}
/// <summary>
/// 创建一个树
/// </summary>
/// <param name="parentID">父ID</param>
/// <param name="node">节点</param>
/// <param name="dt">DataTable</param>
/// <param name="treeView">TreeView的名称</param>
public void CreateTree(int parentID, TreeNode node, DataTable dt, TreeView treeView)
{
//实例化一个DataView dt = 传入的DataTable
dv = new DataView(dt)
//筛选(相当于select * from tabMenu where menuParentID = 传入的 parentID)
dv.RowFilter = "[pid]=" + parentID
//用foreach遍历dv
foreach (DataRowView row in dv)
{
//第一次加载时为空
if (node == null)
{
//创建根节点
TreeNode root = new TreeNode()
//root.Target = "rightFrame"
//必须与数据库的对应
root.Text = row["menuName"].ToString()
root.Value = row["menuID"].ToString()
//添加根节点
this.TreeView1.Nodes.Add(root)
//递归调用方法本身
CreateTree(int.Parse(row["menuID"].ToString()), root, dt, treeView)
}
else
{
//添加子节点
TreeNode childNode = new TreeNode()
childNode.Text = row["menuName"].ToString()
childNode.Value = row["menuID"].ToString()
childNode.NavigateUrl = row["menuUrl"].ToString()
node.ChildNodes.Add(childNode)
CreateTree(int.Parse(row["menuID"].ToString()), childNode, dt, treeView)
}
}
}
}
}
jsp动态树形菜单须用到递归算法,比如在数据库有张表,parent表,parent的字段有id,name,depth,leve,ID自增,depth设置为级数,如这条数据最大,为0,如为字菜单就为1,而leve就指定它父节点的id,给段代码自己可以摸索下 public Vector getModuleTree(){
Vector pclass = new Vector()
try
{
stmt =con.createStatement()
String sql = "select * from Module where parentid = 0"
rs = stmt.executeQuery(sql)
Module cvo = null
while(rs.next())
{
cvo = new Module()
cvo.setModule_id(rs.getInt("Module_id"))
cvo.setModule_name(rs.getString("Module_name"))
cvo.setModule_url(rs.getString("Module_url"))
cvo.setParentid(rs.getInt("parentid"))cvo.setRootid(rs.getInt("rootid"))cvo.setDepth(rs.getInt("depth"))pclass.add(cvo)
}
for (int i = 0i <pclass.size()i++)
{
Module pcvo = (Module) pclass.get(i)
ShowTreeMenu(pcvo)
}
con.commit() } catch (SQLException e)
{
e.printStackTrace()
} finally
{
try
{
if(rs!=null)
{
rs.close()
}
if(stmt!=null)
{
stmt.close()
}
if(con!=null)
{
con.close()
}
}
catch (SQLException e)
{
e.printStackTrace()
}
}
return classList
}
public void ShowTreeMenu(Module c)
{
Module ccvo = null
String sql = "select * from Module where parentid = " + c.getModule_id()
Vector cclass = new Vector()
try
{
Module cvotemp
stmt =con.createStatement()
rs = stmt.executeQuery(sql)
while(rs.next())
{
cvotemp = new Module()
cvotemp.setModule_id(rs.getInt("Module_id"))
cvotemp.setModule_name(rs.getString("Module_name"))
cvotemp.setModule_url(rs.getString("Module_url"))
cvotemp.setParentid(rs.getInt("parentid"))cvotemp.setRootid(rs.getInt("rootid"))cvotemp.setDepth(rs.getInt("depth"))cclass.add(cvotemp)
}
System.out.println(cclass.size()+"(((((((((((((((((((((((((9")
if (cclass.size() >0)
{
c.setHasChild("have")
classList.add(c)
for (int j = 0j <cclass.size()j++)
{
ccvo = (Module) cclass.get(j)
ShowTreeMenu(ccvo)
}} else
{
classList.add(c)
}
} catch (SQLException e)
{
e.printStackTrace()
}
}
jsp从mysql数据库读取数据,并填充到树形结构菜单并展现出来的实现方法:
1、引入jquery.treeview.js树控件
<script type="text/javascript" src="jquery/easyui/jquery.min.js"></script>
<script type="text/javascript" src="jquery/easyui/jquery.easyui.min.js"></script>
2、jsp页面中获取后台mysql数据,并传到jsp页面来
<%
// 数据库的名字
String dbName = "zap"
// 登录数据库的用户名
String username = "sa"
// 登录数据库的密码
String password = "123"
// 数据库的IP地址,本机可以用 localhost 或者 127.0.0.1
String host = "127.0.0.1"
// 数据库的端口,一般不会修改,默认为1433
int port = 1433
String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + "databaseName=" + dbName + "user=" + username
+ "password=" + password
//
//声明需要使用的资源
// 数据库连接,记得用完了一定要关闭
Connection con = null
// Statement 记得用完了一定要关闭
Statement stmt = null
// 结果集,记得用完了一定要关闭
ResultSet rs = null
try {
// 注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
// 获得一个数据库连接
con = DriverManager.getConnection(connectionUrl)
String SQL = "SELECT * from note"
// 创建查询
stmt = con.createStatement()
// 执行查询,拿到结果集
rs = stmt.executeQuery(SQL)
while (rs.next()) {
%>
<tr>
3、填充树形菜单:
{
id : "string" // will be autogenerated if omitted
text : "string" // node text
icon : "string" // string for custom
state : {
opened : boolean // is the node open
disabled : boolean // is the node disabled
selected : boolean // is the node selected
},
children : [] // array of strings or objects
li_attr : {} // attributes for the generated LI node
a_attr : {} // attributes for the generated A node
}
$('#tree').jstree({
'core' : {
'data' : function (obj, cb) {
cb.call(this,
['Root 1', 'Root 2'])
}
}})
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)