MVC 2.0 - JqGrid 多表排序
我正在实现 jqGrid 并希望能够使用排序功能。我在对与基表相关的列进行排序时遇到了一些问题。
下面是加载网格的脚本:
public JsonResult GetData(GridSettings grid)
{
try {
using (IWE dataContext = new IWE())
{
var query = dataContext.LKTYPE.Include("VWEPICORCATEGORY").AsQueryable();
////sorting
query = query.OrderBy<LKTYPE>(grid.SortColumn,
grid.SortOrder);
//count
var count = query.Count();
//paging
var data = query.Skip((grid.PageIndex - 1) * grid.PageSize).Take(grid.PageSize).ToArray();
//converting in grid format
var result = new
{
total = (int)Math.Ceiling((double)count / grid.PageSize),
page = grid.PageIndex,
records = count,
rows = (from host in data
select new
{
TYPE_ID = host.TYPE_ID,
TYPE = host.TYPE,
CR_ACTIVE = host.CR_ACTIVE,
description = host.VWEPICORCATEGORY.description
}).ToArray()
};
return Json(result, JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
//send the error email
ExceptionPolicy.HandleException(ex, "Exception Policy");
}
//have to return something if there is an issue
return Json("");
}
如您所见,描述字段是相关表(“VWEPICORCATEGORY”)的一部分,并且排序依据针对的是 LKTYPE。我试图弄清楚如何对特定字段进行排序,或者甚至可能是一种使用多个表及其排序功能来实现此网格的更好方法。
提前致谢, 比利
I am in the process of implementing the jqGrid and would like to be able to use the sorting functionality. I have run into some issues with sorting columns that are related to the base table.
Here is the script to load the grid:
public JsonResult GetData(GridSettings grid)
{
try {
using (IWE dataContext = new IWE())
{
var query = dataContext.LKTYPE.Include("VWEPICORCATEGORY").AsQueryable();
////sorting
query = query.OrderBy<LKTYPE>(grid.SortColumn,
grid.SortOrder);
//count
var count = query.Count();
//paging
var data = query.Skip((grid.PageIndex - 1) * grid.PageSize).Take(grid.PageSize).ToArray();
//converting in grid format
var result = new
{
total = (int)Math.Ceiling((double)count / grid.PageSize),
page = grid.PageIndex,
records = count,
rows = (from host in data
select new
{
TYPE_ID = host.TYPE_ID,
TYPE = host.TYPE,
CR_ACTIVE = host.CR_ACTIVE,
description = host.VWEPICORCATEGORY.description
}).ToArray()
};
return Json(result, JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
//send the error email
ExceptionPolicy.HandleException(ex, "Exception Policy");
}
//have to return something if there is an issue
return Json("");
}
As you can see the description field is a part of the related table("VWEPICORCATEGORY") and the order by is targeted at LKTYPE. I am trying to figure out how exactly one goes about sorting that particular field or maybe even a better way to implement this grid using multiple tables and it's sorting functionality.
Thanks in advance,
Billy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在对错误的查询进行排序。
将代码更改为:
请注意,由于您正在投影,因此在您的示例中完全不需要
Include()
。You're sorting the wrong query.
Change your code to:
Note that the
Include()
is completely unnecessary in your example since you're projecting.我的博客上有一篇文章,展示了如何使用 MVC 将 jqGrid 的排序和分页功能转化为非常可重用的模式。
我的帖子在这里:
http ://www.journeymandeveloper.com/Home/View/4fae1468-3e08-46e0-9208-9e7104d7956b/Server-side%20Paging%20and%20Sorting%20with%20jqGrid
它包含一个类,您可以将其作为操作方法上的参数,它会为您完成所有工作。当您想要结果时,您真正需要做的就是给它一个 IQueryable 。
I have a post on my blog that shows how I rolled sorting and paging functionality of jqGrid into a very reusable pattern with MVC.
My post is here:
http://www.journeymandeveloper.com/Home/View/4fae1468-3e08-46e0-9208-9e7104d7956b/Server-side%20Paging%20and%20Sorting%20with%20jqGrid
It contains a class that you take in as a parameter on the action method, and it does all the work for you. All you really need to do is give it an IQueryable when you want the results.