【问题标题】:Lazy-loading TreeView with JsTree in Asp.Net MVC在 Asp.Net MVC 中使用 JsTree 延迟加载 TreeView
【发布时间】:2015-08-29 20:53:37
【问题描述】:

我在我的项目中使用 JsTree。我想这样做:

我想在单击根节点 (+) 或子节点时显示子节点后,第一次加载树时只显示根节点。我的意思是,当我单击每个节点时,我想从数据库中获取并添加到子节点。

如何在 Asp.Net MVC 中做到这一点?我查看了几乎所有 JsTree Ajax 示例。但我不能那样做。我应该从行动中返回什么?我该怎么做请帮忙!

JsTree:https://www.jstree.com/

样品:

【问题讨论】:

    标签: c# asp.net-mvc dynamic jstree


    【解决方案1】:

    终于找到问题了!

    我创建了一个模型:

    public class JsTreeModel
    {
        public string id { get; set; }
        public string parent { get; set; } 
        public string text { get; set; }
        public bool children { get; set; } // if node has sub-nodes set true or not set false
    }
    

    我创建了如下控制器:

    public class TreeviewController : Controller
    {
        public JsonResult GetRoot()
        {
            List<JsTreeModel> items = GetTree();
    
            return new JsonResult { Data = items, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
    
        public JsonResult GetChildren(string id)
        {
            List<JsTreeModel> items = GetTree(id);
    
            return new JsonResult { Data = items, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
    
        static List<JsTreeModel> GetTree()
        {
            var items = new List<JsTreeModel>();
    
            // set items in here
    
            return items;
        }
    
        static List<JsTreeModel> GetTree(string id)
        {
            var items = new List<JsTreeModel>();
    
            // set items in here
    
            return items;
        }
    
    }
    

    HTML:

    &lt;div id='treeview'&gt;&lt;/div&gt;

    脚本:

    $('#treeview').jstree({
        "plugins": ["search", "wholerow"],
        'core': {
            'data': {
                'url': function (node) {
                    return node.id === '#' ? "/Treeview/GetRoot" : "/Treeview/GetChildren/" + node.id;
    
                },
                'data': function (node) {
                    return { 'id': node.id };
                }
            }
        }
    });
    
    $('#treeview').on('changed.jstree', function (e, data) {
        console.log("=> selected node: " + data.node.id);
    });
    

    【讨论】:

    • 多么优雅的解决方案!
    • 我只是在寻找一个树视图库,偶然发现了这篇文章。非常好的解决方案。我会提到,如果父字符串为空白/顶级,则需要将其设置为 '#' 以避免 JS 错误。
    【解决方案2】:

    仅具有一种操作方法和延迟加载节点的示例。以防万一有人需要将 jsTree 与 Asp.Net Mvc 一起使用。

    cshtml 视图:

    <div data-key="@Model.Key" id="object-children-tree">
        @* Content will be populated by jsTree *@
    </div>
    
    <script type="text/javascript">
        $(function() {
            var $children = $("#object-children-tree");
    
            $children.jstree({
                "core": {
                    "animation": 0,
                    "data": {
                        "url": function(node) {
                            return '@Url.Action("GetChildren", "Object")';
                        },
                        "data": function (node) {
                            // Each time jstree needs to make an AJAX call this function will be called.
                            // It adds 'key' and 'isRoot' as parameter to ajax call. See signature of 'GetChildren' method.
                            // # is the special ID that the function receives when jstree needs to load the root nodes.
                            var isRoot = false;
                            var key = node.id;
                            if (key === "#") {
                                isRoot = true;
                                key = $("#object-children-tree").data("key");
                            }
                            return { "key": key, "isRoot": isRoot };
                        }
                    }
                },
                "plugins": ["wholerow"]
            });
        });
    </script>
    

    C#,“对象”控制器:

    [HttpGet]
    public JsonResult GetChildren(string key, bool isRoot)
    {
        // Populates the tree using AJAX and lazy loading nodes.
        // Lazy loading makes it possible to load nodes on the fly.
        // jstree will perform AJAX requests as the user browses the tree.
        //  
        // children = true, this special value indicated to jstree, that it has to lazy load the child node node.
        // https://github.com/vakata/jstree#populating-the-tree-using-ajax
    
        if (isRoot)
        {
            var first = new[]
            {
                new
                {
                    id = "root-id",
                    text = "Selected object in the list",
                    state = new
                    {
                        opened = true,
                    },
                    children = new[]
                    {
                        new
                        {
                            id = "child-1",
                            text = "Child 1",
                            children = true
                        },
                        new
                        {
                            id = "child-2",
                            text = "Child 2",
                            children = true
                        }
                    }
                }
            }
            .ToList(); 
    
            return new JsonResult { Data = first, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
    
        var g1 = Guid.NewGuid().ToString();
        var g2 = Guid.NewGuid().ToString();
        var next = new[]
        {
            new
            {
                id = "child-" + g1,
                text = "Child " + g1,
                children = true
            },
            new {
                id = "child-" + g2,
                text = "Child " + g2,
                children = true
            }
        }
        .ToList();
    
        return new JsonResult { Data = next, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }
    

    第一次通话后:

    子节点被点击后:

    【讨论】:

    • 这是一个非常简单且很棒的解决方案。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-07-13
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    相关资源
    最近更新 更多