سؤال

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

هل كانت مفيدة؟

المحلول

You're sorting the wrong query.

Change your code to:

        var query = from host in dataContext.LKTYPE
                    select new
                    {
                        TYPE_ID = host.TYPE_ID,
                        TYPE = host.TYPE,
                        CR_ACTIVE = host.CR_ACTIVE,
                        description = host.VWEPICORCATEGORY.description
                    };

        ////sorting
        query = query.OrderBy(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 = data.ToArray()
        };

Note that the Include() is completely unnecessary in your example since you're projecting.

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top