Tuesday, April 30, 2013

A class returning data in the format required by jqgrid

I got following excellent class from http://blog.prabir.me/?tag=/jqgrid
This class returns data exactly in format required by jqgrid

I have only changed the ToString method, prabir's original method uses newtonsoft lib, i have used .net's native javascript serializer.



public class PagedList
{
    IEnumerable _rows;
    int _totalRecords;
    int _pageIndex;
    int _pageSize;
    object _userData;

    public PagedList(IEnumerable rows, int totalRecords, int pageIndex, int pageSize, object userData)
    {
        _rows = rows;
        _totalRecords = totalRecords;
        _pageIndex = pageIndex;
        _pageSize = pageSize;
        _userData = userData;
    }

    public PagedList(IEnumerable rows, int totalRecords, int pageIndex, int pageSize)
        : this(rows, totalRecords, pageIndex, pageSize, null)
    {
    }

    public int total { get { return (int)Math.Ceiling((decimal)_totalRecords / (decimal)_pageSize); } }

    public int page { get { return _pageIndex; } }

    public int records { get { return _totalRecords; } }

    public IEnumerable rows { get { return _rows; } }

    public object userData { get { return _userData; } }

    public override string ToString()
    {
        JavaScriptSerializer js = new JavaScriptSerializer();
        return js.Serialize(this);
       
    }
}

You will require  following namespaces for this to work :

using System.Collections;
using System.Web.Script.Serialization;


No comments:

Post a Comment