我正在尝试使用ASP加载jstree复选框。后面的代码采用树节点并填充要使用的jstree的列表项。在此过程中,有一些节点被标识为“已检查”,其中我将类属性 jstree-clicked 添加到列表项中。但是,当页面加载时,它没有任何影响。请通过预先选中的复选框让我知道填充这些复选框的适当方法是什么。我的后端有以下内容
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
下面是代码的aspx部分。
<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>
答案 0 :(得分:1)
不应该在li
元素中添加jstree-clicked
类(我在此行中看到它:li.Attributes.Add("class", "jstree-clicked")
),而a
元素在li
内}}
确保为a
的直接li
孩子设置该课程。
答案 1 :(得分:0)
在javascript文件中,jsTree然后检查属性,并在加载完所有内容后预先选中复选框。这适当地选中了复选框。正如上面的评论中所提到的,这避免了只处理CSS而是处理整个节点。
$("#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);
}
});
});