(经典)关于 使用递归从数据库读取数据来动态建立菜单 的问题
表结构与内容
MenBh MenText Bhparent
0001 文件
0002 编辑
0005 打开 0001
0006 新建 0001
0011 access数据库 0006
0012 VFP的Dbf 0006
0013 剪切 0002
0014 复制 0002
0015 完全复制 0014
Private m As New MainMenu()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlConnection( "server=localhost;uid=sa;pwd=;database=jiang ")
Dim cmd As New SqlCommand( "select * from a_menu ", conn)
Dim ds As New DataSet()
Dim sqldpr As New SqlDataAdapter(cmd)
sqldpr.Fill(ds, "menu ")
Me.DataGrid1.DataSource = ds.Tables( "menu ")
'创建关系
Dim dr As New DataRelation( "self_menu ", ds.Tables( "menu ").Columns( "MenBh "), ds.Tables( "menu ").Columns( "Bhparent "))
ds.Relations.Add(dr)
Dim r1 As DataRow
'查找最高的菜单级别,也就是Bhparent列为空的行
For Each r1 In ds.Tables( "menu ").Rows
If r1.IsNull( "Bhparent ") Then
addmenuitem(r1, Nothing)
End If
Next
Me.Menu = m '指定主窗体菜单
End Sub
'递归函数
Private Sub addmenuitem(ByVal r As DataRow, ByVal item As MenuItem)
Dim mi As MenuItem
'如果是最外层菜单,要直接添加
If item Is Nothing Then
mi = m.MenuItems.Add(r.Item( "MenText "))
Else '如果是下级菜单要在菜单项的上级添加
mi = item.MenuItems.Add(r.Item( "MenText "))
End If
Dim r2 As DataRow
For Each r2 In r.GetChildRows( "self_menu ")
addmenuitem(r2, mi)
Next
End Sub
'--------------------一种方法的改进
‘/////////////////////////////////////////////////////////////////////////////////////
‘继承自menuitem的类
‘/////////////////////////////////////////////////////////////////////////////////////
Public Class mymenuitem
Inherits System.Windows.Forms.MenuItem
Public Sub New(ByVal s As String, ByVal tag As String)
MyBase.New()
Me.Text = s
m_tag = tag
End Sub
Private m_tag As String
Public Property tag() As String
Get
Return m_tag
End Get
Set(ByVal Value As String)
m_tag = Value
End Set
End Property
End class
‘////////////////////////////////////////////////////////////////////////////////////
‘在代码中实现这种菜单项的添加和菜单的建立
‘//////////////////////////////////////////////////////////////////////////////////
Private m As New MainMenu()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conn As New SqlConnection( "server=localhost;uid=sa;pwd=;database=jiang ")
Dim cmd As New SqlCommand( "select * from a_menu ", conn)
Dim ds As New DataSet()
Dim sqldpr As New SqlDataAdapter(cmd)
sqldpr.Fill(ds, "menu ")
'Me.DataGrid1.DataSource = ds.Tables( "menu ")
Me.DataGrid1.DataSource = ds
Me.DataGrid1.DataMember = "menu "
'创建关系
Dim dr As New DataRelation( "self_menu ", ds.Tables( "menu ").Columns( "MenBh "), ds.Tables( "menu ").Columns( "Bhparent "))
ds.Relations.Add(dr)
Dim r1 As DataRow
'查找最高的菜单级别,也就是Bhparent列为空的行
For Each r1 In ds.Tables( "menu ").Rows
If r1.IsNull( "Bhparent ") Then
addmenuitem(r1, Nothing)
End If
Next
Me.Menu = m '指定主窗体菜单
End Sub
'递归函数
Private Sub addmenuitem(ByVal r As DataRow, ByVal item As MenuItem)
Dim mi As MenuItem
'如果是最外层菜单,要直接添加
If item Is Nothing Then
Try
mi = New mymenuitem(r.Item( "MenText "), "1 ")
m.MenuItems.Add(mi)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Else '如果是下级菜单要在菜单项的上级添加
mi = New mymenuitem(r.Item( "MenText "), "2 ")
item.MenuItems.Add(mi)
End If
Dim r2 As DataRow
For Each r2 In r.GetChildRows( "self_menu ")
addmenuitem(r2, mi)
Next
End Sub
运行时有错误,
Dim dr As New DataRelation( "self_menu ", ds.Tables( "menu ").Columns( "MenBh "), ds.Tables( "menu ").Columns( "Bhparent ")) ,这里有错误,后来我改为
Dim dr As New DataRelation( "self_menu ", ds1.Tables( "menu ").Columns( "MenBh "), ds1.Tables( "menu ").Columns( "Bhparent "), False),也不正确。
麻烦各位兄弟,帮我一下,谢谢!
QQ:88981904
[解决办法]
DataRelation对象将两个 DataTable 对象相互关联, 不是将一个DataTable的两个字段建立关联
[解决办法]
给你一个 JavaScript 的代码
var rows = listData.split( "| ");
function InitTree(s,pid,tid)
{
var l = rows.length;
while(s < l)
{
var cell = rows[s++].split( ", ");
if(pid == cell[1])
{
AddChildItem(pid,tid,cell);
}
InitTree(s,cell[0],cell[0]);
}
}
function AddChildItem(pid,tid,cell)
{
tree.nodes[tid + "_ " + cell[0]] = "text: " + cell[2] + ";url:edit.aspx?id= " + cell[0];
}
InitTree(0,0,1);