استفاده از Kendo UI TreeView به همراه یک منبع داده راه دور
نویسنده: وحید نصیری
تاریخ: ۱۳۹۴/۰۱/۱۵ ۲۱:۵۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace KendoUI11.Models
{
public class BlogComment
{
public int Id { set; get; }
public string Body { set; get; }
public int? ParentId { get; set; }
// مخصوص کندو یو آی هستند
public bool HasChildren { get; set; }
public string imageUrl { get; set; }
}
} using System.Collections.Generic;
namespace KendoUI11.Models
{
/// <summary>
/// منبع داده فرضی جهت سهولت دموی برنامه
/// </summary>
public static class BlogCommentsDataSource
{
private static readonly IList<BlogComment> _cachedItems;
static BlogCommentsDataSource()
{
_cachedItems = createBlogCommentsDataSource();
}
public static IList<BlogComment> LatestComments
{
get { return _cachedItems; }
}
/// <summary>
/// هدف صرفا تهیه یک منبع داده آزمایشی ساده تشکیل شده در حافظه است
/// </summary>
private static IList<BlogComment> createBlogCommentsDataSource()
{
var list = new List<BlogComment>();
var comment1 = new BlogComment
{
Id = 1, Body = "نظر من این است که", HasChildren = true, ParentId = null
};
list.Add(comment1);
var comment12 = new BlogComment
{
Id = 2, Body = "پاسخی به نظر اول", HasChildren = true, ParentId = 1
};
list.Add(comment12);
var comment12A = new BlogComment
{
Id = 3, Body = "پاسخی دیگری به نظر اول", HasChildren = false, ParentId = 1
};
list.Add(comment12A);
var comment121 = new BlogComment
{
Id = 4, Body = "پاسخی به پاسخ به نظر اول", HasChildren = false, ParentId = 2
};
list.Add(comment121);
var comment2 = new BlogComment
{
Id = 5, Body = "نظر 2", HasChildren = true, ParentId = null, imageUrl= "images/search.png"
};
list.Add(comment2);
var comment21 = new BlogComment
{
Id = 6, Body = "پاسخ به نظر 2", HasChildren = false, ParentId = 5
};
list.Add(comment21);
return list;
}
}
} using System.Linq;
using System.Web.Mvc;
using KendoUI11.Models;
namespace KendoUI11.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View(); // shows the page.
}
[HttpGet]
public ActionResult GetBlogComments(int? id)
{
if (id == null)
{
//دریافت ریشهها
return Json(
BlogCommentsDataSource.LatestComments
.Where(x => x.ParentId == null) // ریشهها
.ToList(),
JsonRequestBehavior.AllowGet);
}
else
{
//دریافت فرزندهای یک ریشه
return Json(
BlogCommentsDataSource.LatestComments
.Where(x => x.ParentId == id)
.ToList(),
JsonRequestBehavior.AllowGet);
}
}
}
} <!--نحوهی راست به چپ سازی -->
<div class="k-rtl k-header demo-section">
<div id="my-treeview"></div>
</div>
@section JavaScript
{
<script type="text/javascript">
$(function () {
var dataSource = new kendo.data.HierarchicalDataSource({
transport: {
read: {
url: "@Url.Action("GetBlogComments", "Home")",
dataType: "json",
contentType: 'application/json; charset=utf-8',
type: 'GET'
}
},
schema: {
model: {
id: "Id",
hasChildren: "HasChildren"
}
}
});
$("#my-treeview").kendoTreeView({
//استفاده از قالب در صورت نیاز
template: kendo.template($("#treeview-template").html()),
checkboxes: {
checkChildren: false
},
dataSource: dataSource,
dataTextField: "Body",
//رخدادها
select: function (e) { console.log("Selecting: " + this.text(e.node)); },
check: function (e) { console.log("Checkbox changed :: " + this.text(e.node)); },
change: function (e) { console.log("Selection changed"); },
collapse: function (e) { console.log("Collapsing " + this.text(e.node)); },
expand: function (e) { console.log("Expanding " + this.text(e.node)); }
});
});
</script>
<script id="treeview-template" type="text/kendo-ui-template">
<strong> #: item.Body # </strong>
</script>
<style scoped>
.demo-section {
width: 100%;
height: 300px;
}
</style>
} <ul id="treeview">
<li>
Parent 1
<ul>
<li>Child</li>
<li>Child</li>
<li>Child</li>
</ul>
</li>
<li>Parent 2</li>
<li>
Parent 3
<ul>
<li>Child</li>
<li>Child</li>
</ul>
</li>
<li>Parent 4</li>
<li>Parent 5</li>
<ul> <link href="~/Content/kendo.common.min.css" rel="stylesheet"/>
<link href="~/Content/kendo.default.min.css" rel="stylesheet"/>
<link href="~/Content/kendo.dataviz.min.css" rel="stylesheet" />
<link href="~/Content/kendo.dataviz.default.min.css" rel="stylesheet"/>
<link href="~/Content/kendo.rtl.min.css" rel="stylesheet"/>
<script src="~/Scripts/kendo.all.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#treeView").kendoTreeView({
checkboxes: {
checkChildren: true
}
});
});
</script> <ul id="treeview" data-role="treeview">
<li data-expanded="true">
<span class="k-sprite folder"></span>
My Web Site
<ul>
<li data-expanded="true">
<span class="k-sprite folder"></span>images
<ul>
<li><span class="k-sprite image"></span>logo.png</li>
<li><span class="k-sprite image"></span>body-back.png</li>
<li><span class="k-sprite image"></span>my-photo.jpg</li>
</ul>
</li>
<li data-expanded="true">
<span class="k-sprite folder"></span>resources
<ul>
<li data-expanded="true">
<span class="k-sprite folder"></span>pdf
<ul>
<li><span class="k-sprite pdf"></span>brochure.pdf</li>
<li><span class="k-sprite pdf"></span>prices.pdf</li>
</ul>
</li>
<li><span class="k-sprite folder"></span>zip</li>
</ul>
</li>
</ul>
</li>
</ul>