نمایش ساختارهای درختی توسط jqGrid
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۵/۰۹ ۱۵:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
namespace jqGrid13.Models
{
public class BlogComment
{
// Other properties
public int Id { set; get; }
public string Body { set; get; }
public DateTime AddDateTime { set; get; }
// for treeGridModel: 'adjacency'
public int? ParentId { get; set; }
public bool IsNotExpandable { get; set; }
public bool IsExpanded { get; set; }
}
} using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using jqGrid13.Models;
using JqGridHelper.DynamicSearch; // for dynamic OrderBy
using JqGridHelper.Models;
using JqGridHelper.Utils;
namespace jqGrid13.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult GetComments(JqGridRequest request, int? nodeid, int? parentid, int? n_level)
{
var list = BlogCommentsDataSource.LatestBlogComments;
// در این حالت خاص فعلا در نگارش جای جیکیوگرید صفحه بندی کار نمیکند و فعال نیست و محاسبات ذیل اهمیتی ندارند
var pageIndex = request.page - 1;
var pageSize = request.rows;
var totalRecords = list.Count;
var totalPages = (int)Math.Ceiling(totalRecords / (float)pageSize);
var productsQuery = list.AsQueryable();
if (nodeid == null)
{
productsQuery = productsQuery.Where(x => x.ParentId == null);
}
else
{
productsQuery = productsQuery.Where(x => x.ParentId == nodeid.Value);
}
var products = productsQuery.OrderBy(request.sidx + " " + request.sord)
.Skip(pageIndex * pageSize)
.Take(pageSize)
.ToList();
var newLevel = n_level == null ? 0 : n_level.Value + 1;
var productsData = new JqGridData
{
Total = totalPages,
Page = request.page,
Records = totalRecords,
Rows = (products.Select(comment => new JqGridRowData
{
Id = comment.Id,
RowCells = new List<object>
{
comment.Id,
comment.Body,
comment.AddDateTime.ToPersianDate(),
// اطلاعات خاص نمایش درختی به ترتیب
newLevel,
comment.ParentId == null ? "" : comment.ParentId.Value.ToString(CultureInfo.InvariantCulture),
comment.IsNotExpandable,
comment.IsExpanded
}
})).ToList()
};
return Json(productsData, JsonRequestBehavior.AllowGet);
}
}
}
$('#list').jqGrid({
caption: "آزمایش سیزدهم",
// .... مانند قبل
treeGrid: true,
treeGridModel: 'adjacency',
ExpandColumn: '@(StronglyTyped.PropertyName<BlogComment>(x => x.Body))'
}).jqGrid('gridResize', { minWidth: 400 });
});
public ActionResult GetComments(JqGridRequest request, int? nodeid, int? parentid, int? n_level)
if (nodeid == null)
{
productsQuery = productsQuery.Where(x => x.ParentId == null);
} else
{
// آی دی یک گره میتواند کلید خارجی یک جدول دیگر باشد
productsQuery = productsQuery.Where(x => x.ParentId == nodeid.Value);
}