【问题标题】:set initial state for checkboxes in jstree为 jstree 中的复选框设置初始状态
【发布时间】:2015-06-07 01:54:06
【问题描述】:

我目前正在制作一个 C# MVC 应用程序,其中我使用 jsTree 作为 UI 元素之一。我在 jsTree 中使用复选框。我已经可以检索所有选定的值并将它们传递给我的控制器,让它做它的事情,没有问题。

但是我在加载网页时无法填充 jsTree。目前我有这个代码:

        @foreach (var obj in listServers)
        {
            <li id="root_@obj.DBServerID">
                @obj.DBServerName
                <ul>
                    @{
                        System.Data.DataSet myDS = Model.findDataBasesByServerID(obj.DBServerID);
                        foreach (System.Data.DataRow row in myDS.Tables[0].Rows)
                        {

                                if (dbRepos.isThisDatabaseSelectedForThisUser(Model.LoginID, obj.DBServerID, nameOfDB) == true) //true -> user has selected this earlier and needs to be selected
                                {
                                    <li class="jstree-clicked get_checked" id="@(obj.DBServerID)_@nameOfDB">@nameOfDB </li>
                                }
                                else //false -> user hasn't selected this earlier. Make a normal, unchecked checkbox
                                {
                                    <li id="@(obj.DBServerID)_@nameOfDB">@nameOfDB </li>
                                }
                        }
                    }
                </ul>
            </li>
        }

代码只能部分工作。自然地,我的 jsTree 做得很好,但没有检查默认值。问题不在于我的 isThisDatabaseSelectedForThisUser 方法,因为我调试并测试了它。它按预期工作。我猜

  • 元素的类有问题,但我不知道是什么问题。 get_checked 属性在运行时给出,以指示复选框已被单击并因此被选中。请注意,我在这里使用 jsTree 的复选框值, 元素的文本的选择并不重要。
  • 【问题讨论】:

    • 根据我从演示中看到的,li 中的anchor 标记得到jstree-clicked 类而不是li 标记。请参阅以下 HTML &lt;a class="jstree-anchor jstree-clicked" href="#" tabindex="-1" id="j2_2_anchor"&gt;&lt;i class="jstree-icon jstree-checkbox" role="presentation"&gt;&lt;/i&gt;&lt;i class="jstree-icon jstree-themeicon" role="presentation"&gt;&lt;/i&gt;initially selected&lt;/a&gt; 所以我认为您需要相应地重组您的 HTML..

    标签: c# jquery html checkbox jstree


    【解决方案1】:

    好的,我决定尝试使用数据属性方法,而不是 Class 方法,结果成功了。我使用以下代码,并在加载页面时检查我的复选框:

    < li data-jstree='{"opened":false,"selected":true}' id="@(obj.DBServerID)_@nameOfDB">@nameOfDB </li>
    

    我觉得自己很愚蠢。

    【讨论】:

      猜你喜欢
      • 2017-12-08
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 2016-02-17
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多