我正在使用 Jqtree 插件在我的 mvc4 应用程序中加载树视图,该视图包含按字母顺序排序的名称列表。一切正常,但页面加载需要很多时间。
控制器代码:
public class HomeController : BaseController<Home, HomeModel>
{`
int i = 1;
[Authorize]
public override ActionResult Index()
{
ViewBag.DOM_TreeView = PopulateTreeView();
return View();
}
private string PopulateTreeView()
{
string DOM = "";
DataTable table = new DataTable();
//using (var connection = new SqlConnection("Data Source = 192.168.1.200; Initial Catalog=NHFP; User Id= sa; Password = server+123"))
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["VMS.Demo"].ConnectionString))
{
connection.Open();
using (var command = new SqlCommand("SELECT * FROM ", connection))
{
table.Load(command.ExecuteReader());
}
connection.Close();
}
DOM = GetTreeView(table);
return DOM;
}
private string GetTreeView(DataTable dt)
{
string DOMTreeView = "";
string[] alphabetArray = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
foreach (string key in alphabetArray)
{
DOMTreeView += CreateTreeView(dt, key);
}
return DOMTreeView;
}
private string CreateTreeView(DataTable dt, string ParentKey)
{
string DOMDataList = "";
string ProviderName = "";
var result = dt.AsEnumerable().Where(dr => dr.Field<string>("Key") == ParentKey);
DOMDataList += "<tr class='treegrid-" + i + "'>";
DOMDataList += "<td>" + ParentKey + "</td>";
DOMDataList += "</tr>";
foreach (DataRow row in result)
{
ProviderName = row[1].ToString();
DOMDataList += "<tr class='treegrid-parent-" + i + "'>";
DOMDataList += "<td>" + ProviderName + "</td>";
DOMDataList += "</tr>";
}
i = i + 1;
return DOMDataList;
}
}
}
查看代码:
<script type="text/javascript">
$(document).ready(function () {
$('.tree').treegrid();
$('.tree-2').treegrid({
expanderExpandedClass: 'glyphicon glyphicon-minus',
expanderCollapsedClass: 'glyphicon glyphicon-plus',
autoOpen: 0
});
$('#tree').treegrid('collapseAll');
});
</script>
<script>
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-43342702-1', 'maxazan.github.io');
ga('send', 'pageview');
</script>
</head>
<body >
<div class="container" >
<br><br>
<table class="table tree-2 table-bordered table-striped table-condensed table-scrollable" style="display:block;width:25%; overflow-y:scroll;height:500px" id="tree" >
@Html.Raw(ViewBag.DOM_TreeView)
</table>
</div>
</body>
</html>