MVC 2.0 - JqGrid Sorting with Mulitple Tables
Posted
by
Billy Logan
on Stack Overflow
See other posts from Stack Overflow
or by Billy Logan
Published on 2011-01-31T15:17:09Z
Indexed on
2011/01/31
15:25 UTC
Read the original article
Hit count: 486
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
© Stack Overflow or respective owner