问题
I'm trying to load jstree checkboxes on load using ASP. The code behind takes a tree node and populates a list item for the jstree to use. In this process, there are nodes that are identified as "Checked", in which I add the class attribute jstree-clicked to the list item. However when the page loads, it doesn't have any effect. Please let me know what to the appropriate way to populate these check boxes with the check boxes pre-checked. I have the following in my back end
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim tNode As TreeNode
Dim treeView As New TreeView
Dim tNodeCollection As New TreeNodeCollection
tNodeCollection = treeView.Nodes
' Code to generate and store within
' a System.Web.UI.WebControls.TreeView object
' ...
' ...
' ...
repeater.DataSource = tNodeCollection
repeater.DataBind()
End Sub
Protected Sub repeater_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
Dim tNode As TreeNode
Dim li As New HtmlGenericControl
Dim ul As New HtmlGenericControl("ul")
tNode = e.Item.DataItem
If (tNode Is Nothing) Then
Return
End If
li = e.Item.FindControl("listItem")
li.ID = tNode.Value
li.InnerHtml = tNode.Text
If tNode.Checked Then
li.Attributes.Add("rel", "true")
End If
If tNode.ChildNodes.Count > 0 Then
li.Controls.Add(ul)
searchChildNodes(tNode.ChildNodes, ul)
End If
End Sub
Private Sub searchChildNodes(childNodes As TreeNodeCollection, ul As HtmlGenericControl)
Dim tNode As TreeNode
For Each tNode In childNodes
Dim li As New HtmlGenericControl("li")
li.ID = tNode.Value
li.InnerHtml = tNode.Text
ul.Controls.Add(li)
If tNode.ChildNodes.Count > 0 Then
Dim unorderedList As New HtmlGenericControl("ul")
li.Controls.Add(unorderedList)
searchChildNodes(tNode.ChildNodes, unorderedList)
End If
Next
End Sub
Below is the aspx portion of the code.
<div id="myTreeNode" >
<asp:Repeater ID="rptr" runat="server" EnableViewState="False" OnItemDataBound="repeater_ItemDataBound" >
<headerTemplate>
<ul>
</headerTemplate>
<ItemTemplate>
<li id="listItem" runat="server"></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</div>
回答1:
It is not the li
element where you should add jstree-clicked
class (I see it in this line: li.Attributes.Add("class", "jstree-clicked")
) but the a
element inside that li
.
Make sure you set that class for an immediate a
child of the li
.
回答2:
In the javascript file, the jsTree then checks for the attribute and preselects the check box once everything has been loaded. This appropriately selects the check box. As mentioned in the comment above, this avoids only handling the CSS but rather handles the entire node.
$("#myTreeNode").bind('ready.jstree', function (event, data) {
var $tree = $(this);
$($tree.jstree().get_json($tree, {
flat: true
})).each(function () {
var checked = $(this).attr('rel');
var node;
if( checked == "true"){
node = $("#myTreeNode").jstree().select_node(this.id);
}
});
});
来源:https://stackoverflow.com/questions/34816102/how-to-load-jstree-checkbox-on-load-vb-asp