目前,我使用AJAX處理程序來填充JSTree:ASP.net/jQuery:JSTree,選擇節點,似乎無法獲得ID
$(function() {
$("#jstree").jstree({
"json_data": {
"ajax": {
"url": "AJAXHandler.aspx?action=GetMenu"
}
},
"plugins": ["themes", "json_data", "dnd"]
})
.bind("move_node.jstree", function (node, ref, position, is_copy, is_prepared, skip_check) {
console.log(node); });
});
處理程序實際上使數據庫調用,通過菜單項循環,創建一個JSON對象序列化,發回,並呈現:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Select Case Request("action")
Case "GetMenu"
GetMasterMenu()
Case "UpdateMenuHiearchy"
UpdateMenuHiearchy()
End Select
End Sub
Private Sub GetMasterMenu()
Dim dt As DataTable = GetMenu()
Dim nodesList As New List(Of JsTreeNode)()
PopulateNodes(dt, nodesList)
Dim ser As New JavaScriptSerializer()
Dim res As String = ser.Serialize(nodesList)
Response.ContentType = "application/json"
Response.Write(res)
Response.[End]()
End Sub
Private Sub PopulateNodes(ByRef dt As DataTable, ByVal nodes As List(Of JsTreeNode))
Dim parents() As DataRow = dt.Select("PARENT_MENU_ID = 0")
'Root Nodes
For Each dr As DataRow In parents
Dim node As New JsTreeNode()
node.attributes = New Attributes()
node.attributes.id = dr("APPLICATION_MENU_ID").ToString
node.attributes.rel = "root" & dr("APPLICATION_MENU_ID").ToString
node.data = New Data()
node.data.title = dr("DESCRIPTION")
node.state = "open"
'Check for Children
Dim strSQL As New StringBuilder
With strSQL
.Append(" SELECT * FROM APPLICATION_MENU WHERE PARENT_MENU_ID = " & dr("APPLICATION_MENU_ID") & "")
End With
Dim dtChildren As DataTable = DatabaseManager.Query(strSQL.ToString)
If dtChildren.Rows.Count > 0 And dtChildren IsNot Nothing Then
For Each drChild As DataRow In dtChildren.Rows
AddChildNodes(dt, dr("APPLICATION_MENU_ID"), node)
Next
End If
node.attributes.mdata = "{draggable : true}"
nodes.Add(node)
Next
End Sub
Private Sub AddChildNodes(ByRef dt As DataTable, ByVal parentID As Integer, ByVal node As JsTreeNode)
Dim strSQL As New StringBuilder
With strSQL
.Append(" SELECT * FROM APPLICATION_MENU WHERE PARENT_MENU_ID = " & parentID.ToString & "")
End With
Dim dtChildren As DataTable = DatabaseManager.Query(strSQL.ToString)
node.children = New List(Of JsTreeNode)()
For Each drChild As DataRow In dtChildren.Rows
Dim cnode As New JsTreeNode()
cnode.attributes = New Attributes()
cnode.attributes.id = drChild("APPLICATION_MENU_ID").ToString
node.attributes.rel = "folder"
cnode.data = New Data()
cnode.data.title = drChild("DESCRIPTION")
cnode.attributes.mdata = "{draggable : true }"
strSQL = New StringBuilder
With strSQL
.Append(" SELECT * FROM APPLICATION_MENU WHERE PARENT_MENU_ID = " & drChild("APPLICATION_MENU_ID") & "")
End With
Dim dtChildren2 As DataTable = DatabaseManager.Query(strSQL.ToString)
If dtChildren.Rows.Count > 0 And dtChildren IsNot Nothing Then
AddChildNodes(dt, drChild("APPLICATION_MENU_ID"), cnode)
End If
node.children.Add(cnode)
Next
End Sub
這裏的想法是將move_node綁定到會打擊處理程序和更新數據庫,以其中一個功能我移動了這個對象。我已經能夠創建綁定來做到這一點。但問題是,我似乎無法獲得身份證。我將它設置在JSON對象的總體屬性中,但是當我通過console.log監視NODE和REF對象時,id字段爲空。
什麼給?有任何想法嗎?我錯過了一些重要的東西?