简体   繁体   中英

jstree: How to select a node by id

I know there are many solutions for my problem, but no one is fine for me. Here is my situation: I want to select a node by node id programmatically, In the .ascx file a function has been defined:

function InitializeNodeState(nodeID) 
{
    $('#<%=ClientID %>').jstree("select_node", "#"+nodeID);
};

and in the vb file another function has been defined to call above function:

Public Sub SelectedRooteNodeToInitializeState(node As TreeNode)
    Dim rooteNode As TreeNode = Me.Nodes(0)
    If rooteNode.id = node.id Then
       Page.ClientScript.RegisterStartupScript(Me.GetType(),   "script","InitializeNodeState(" + rooteNode.id + ");", True)        
    End If
End Sub

My questions are:

  1. JavaScript runtime error: The value of the property 'InitializeNodeState' is null or undefined, not a Function object.
  2. The sentence $('#<%=ClientID %>').jstree("select_node", "#"+nodeID); seems does not work.

Please educate me, thanks.

Here is additional information about my problem:

the value of state parameter for all nodes in jstree is null until user click the jstree, so I want to select a node by its id to initialize node state before user click the jstree.

  1. Where do you place the javascript function. I think it needs to be in a script tag in the head (or in an included js file, in the head).
  2. If you view source, what do you see at the javascript? What do you sees as a client ID and what do you see as a nodeID (in the HTML source).

Additionally, you might need to change this:

Page.ClientScript.RegisterStartupScript(Me.GetType(),   "script","InitializeNodeState(" + rooteNode.id + ");", True) 

to this:

Page.ClientScript.RegisterStartupScript(Me.GetType(),   "script","InitializeNodeState('" + rooteNode.id + "');", True) 

set runat server for your node
for instance

<tr runat="server" id="myNode"></tr>


$('#<%=yourElement.ClientID %>').jstree("select_node", "#"+nodeID);

    Public Sub SelectedRooteNodeToInitializeState(node As TreeNode)
        Dim rooteNode As TreeNode = Me.Nodes(0)
        If rooteNode.ClientID = myNode.ClientID Then
           Page.ClientScript.RegisterStartupScript(Me.GetType(),   "script","InitializeNodeState(" + myNode.ClientID + ");", True)        
        End If
    End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM