MVC 中使用TreeView

为了灵活性,本例中实现是在View中完成递归逻辑,因此可以在递归过程中控制样式,内容的render

也可以选择在后端完成递归逻辑返回partial 然后在前端jquery中加class





1. 从这里下载
http://bassistance.de/jquery-plugins/jquery-plugin-treeview/


2. 将以下3个文件copy到项目中,如下所示:

技术分享


技术分享

3. 准备view model


public class JqTreeViewNode
    {
        public string Name { get; set; }
        public IList<JqTreeViewNode> Children { get; set; }


        public JqTreeViewNode()
        {
            Children = new List<JqTreeViewNode>();
        }
        public static JqTreeViewNode CreateTest()
        {
            return new JqTreeViewNode()
            {
                Name = "Root",
                Children = new List<JqTreeViewNode>()
                {
                    new JqTreeViewNode()
                    {
                        Name = "ChildLvl1-1",
                        Children = new List<JqTreeViewNode>
                        {
                            new JqTreeViewNode()
                            {
                                Name = "ChildLvl2-1",
                                Children = new List<JqTreeViewNode>()
                                {
                                    new JqTreeViewNode()
                                    {
                                        Name = "ChildLvl3-1",
                                    },
                                    new JqTreeViewNode(){Name = "ChildLv3-2"},
                                    new JqTreeViewNode(){Name = "ChildLv3-3"}
                                }
                            },
                            new JqTreeViewNode(){Name = "ChildLv2-2"}
                        }
                    },
                    new JqTreeViewNode(){Name = "ChildLv1-2"}
                }
            };
        }
    }




4. controller 
public ActionResult Index()
        {


            return View(JqTreeViewNode.CreateTest());
        }




5. view 
@model WebApplication1.Controllers.JqTreeViewNode


<link href="~/Content/External/jquery.treeview.css" rel="stylesheet" />
@section scripts{
    <script src="~/Content/External/jquery.treeview.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".treeview-gray").treeview({ collapsed: true });
    });


</script>


}


<div>
    JqTreeView
</div>
<div>
    @Html.Partial("JqTreeView", Model)
</div>




6. partial view (也可用display template)


@using WebApplication1.Controllers
@model WebApplication1.Controllers.JqTreeViewNode


<ul style="margin: 5px" class="treeview-gray">
    @{
        if (Model.Children.Any())
        {
            foreach (JqTreeViewNode item in Model.Children)
            {


                <li style="margin-left: 20px">
                    @item.Name
                    @{
                if (item.Children.Any())
                {
                    @Html.Partial("JqTreeView", item)
                }
                    }


                </li>
            }
        }
    }
</ul>




7.查看结果


技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。