ASP.net/jQuery:JSTree,选择Node,似乎无法获取ID

时间:2010-09-10 00:19:06

标签: asp.net jquery vb.net jstree

目前,我正在使用AJAX处理程序来填充JSTree:

$(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绑定到一个函数,该函数将命中处理程序并更新数据库以确定我移动对象的位置。我已经能够创建绑定来做到这一点。然而,问题是我似乎无法获得ID。我在JSON对象的填充中的属性中设置它,但是当我通过console.log监视NODE和REF对象时,id字段为空。

是什么给出的?有任何想法吗?我错过了一些重要的东西吗?

2 个答案:

答案 0 :(得分:1)

再次摆弄它之后,我找到了答案:

cnode.attributes
node.attributes

它必须是特定于名称的,这些必须是cnode.attr和node.attr才能工作。

答案 1 :(得分:0)

你确实是对的 -

JSTree V1 +使用jquery绑定等。因此,您需要使用attr来获取对象属性 - 在侧面注释中,IE7-与节点数据区分大小写,例如:

$("#node").attr("id")!=$("#node").attr("ID")