如何在通过 Ajax.ActionLink 调用的动态加载的 ASP.NET MVC 部分视图上初始化 jsTree
How can I initialize jsTree on a dynamically loaded ASP.NET MVC Partial View that is called via an Ajax.ActionLink
我想创建一个使用 jsTree (http://www.jstree.com) 的 ASP.NET MVC 5 应用程序。我正在使用 ASP.NET AJAX 在运行时动态加载页面。在动态加载的分部视图上哪里可以初始化jsTree?
当我启动网络应用程序时,一切正常。我看到这样的树:
但现在我单击 'Change Tree View' 并通过 Ajax.ActionLink 加载了部分视图。现在树将不会呈现为 TreeView,而只会呈现为普通列表:
你可以像这样初始化树:
$('#treeView').jstree();
如何为动态加载的分部视图初始化TreeView?我尝试了 Action Link 的 OnComplete 回调函数,但没有用。
TreeTest\Controllers\HomeController.cs:
using System.Web.Mvc;
namespace TreeTest.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public PartialViewResult ChangeTreeView()
{
return PartialView();
}
}
}
TreeTest\Views\Shared_Layout.cshtml:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
<link rel="stylesheet" href="/Content/vakata-jstree-a0767ce/dist/themes/default/style.min.css" />
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
</ul>
<p class="nav navbar-text navbar-right">Hello, @User.Identity.Name!</p>
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery",
"~/bundles/jqueryajax")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/scripts")
@RenderSection("scripts", required: false)
<script src="/Content/vakata-jstree-a0767ce/dist/jstree.min.js"></script>
</body>
</html>
TreeTest\Views\Home\Index.cshtml:
@{
ViewBag.Title = "Home Page";
}
<div id="treeView">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>
Item 4
<ul>
<li>Item 4.1</li>
</ul>
</li>
</ul>
</div>
@Ajax.ActionLink("Change Tree View", "ChangeTreeView", new RouteValueDictionary(), new AjaxOptions { UpdateTargetId = "treeView", OnComplete = "TreeViewChangeOnComplete" })
TreeTest\Views\Home\ChangeTreeView.cshtml:
<ul>
<li>New Item 1</li>
<li>New Item 2</li>
<li>New Item 3</li>
<li>
New Item 4
<ul>
<li>New Item 4.1</li>
</ul>
</li>
</ul>
TreeTest\Scripts\JavaScript.js:
$(document).ready(function () {
$('#treeView').jstree();
});
function TreeViewChangeOnComplete() {
$('#treeView').jstree();
}
我不熟悉 .NET 的工作方式,但我猜它只是将指定元素的 HTML 内容与从 AJAX 调用加载的内容进行交换。
因为实例已经在节点上创建 (#treeView
),所以第二个 jstree
调用什么都不做(实际上它只是检索已经存在的实例)。
我只是猜测,还没有测试过,但试一试:
function TreeViewChangeOnComplete() {
$('#treeView').jstree(true).destroy(true);
$('#treeView').jstree();
}
我想创建一个使用 jsTree (http://www.jstree.com) 的 ASP.NET MVC 5 应用程序。我正在使用 ASP.NET AJAX 在运行时动态加载页面。在动态加载的分部视图上哪里可以初始化jsTree?
当我启动网络应用程序时,一切正常。我看到这样的树:
但现在我单击 'Change Tree View' 并通过 Ajax.ActionLink 加载了部分视图。现在树将不会呈现为 TreeView,而只会呈现为普通列表:
你可以像这样初始化树:
$('#treeView').jstree();
如何为动态加载的分部视图初始化TreeView?我尝试了 Action Link 的 OnComplete 回调函数,但没有用。
TreeTest\Controllers\HomeController.cs:
using System.Web.Mvc;
namespace TreeTest.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public PartialViewResult ChangeTreeView()
{
return PartialView();
}
}
}
TreeTest\Views\Shared_Layout.cshtml:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
<link rel="stylesheet" href="/Content/vakata-jstree-a0767ce/dist/themes/default/style.min.css" />
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
</ul>
<p class="nav navbar-text navbar-right">Hello, @User.Identity.Name!</p>
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
@Scripts.Render("~/bundles/jquery",
"~/bundles/jqueryajax")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/scripts")
@RenderSection("scripts", required: false)
<script src="/Content/vakata-jstree-a0767ce/dist/jstree.min.js"></script>
</body>
</html>
TreeTest\Views\Home\Index.cshtml:
@{
ViewBag.Title = "Home Page";
}
<div id="treeView">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>
Item 4
<ul>
<li>Item 4.1</li>
</ul>
</li>
</ul>
</div>
@Ajax.ActionLink("Change Tree View", "ChangeTreeView", new RouteValueDictionary(), new AjaxOptions { UpdateTargetId = "treeView", OnComplete = "TreeViewChangeOnComplete" })
TreeTest\Views\Home\ChangeTreeView.cshtml:
<ul>
<li>New Item 1</li>
<li>New Item 2</li>
<li>New Item 3</li>
<li>
New Item 4
<ul>
<li>New Item 4.1</li>
</ul>
</li>
</ul>
TreeTest\Scripts\JavaScript.js:
$(document).ready(function () {
$('#treeView').jstree();
});
function TreeViewChangeOnComplete() {
$('#treeView').jstree();
}
我不熟悉 .NET 的工作方式,但我猜它只是将指定元素的 HTML 内容与从 AJAX 调用加载的内容进行交换。
因为实例已经在节点上创建 (#treeView
),所以第二个 jstree
调用什么都不做(实际上它只是检索已经存在的实例)。
我只是猜测,还没有测试过,但试一试:
function TreeViewChangeOnComplete() {
$('#treeView').jstree(true).destroy(true);
$('#treeView').jstree();
}