Php jqgrid和服务器端分页

Php jqgrid和服务器端分页,php,jquery,mysql,jqgrid,pagination,Php,Jquery,Mysql,Jqgrid,Pagination,我意识到有一些相关的问题,但我找不到我想要的。过去我多次使用jqgrid,但忘记了如何实现服务器端分页 这是我的javascript $("#list").jqGrid({ url: "index.php?loadData=test", datatype: "json", mtype: "GET", colNames: ["id", "eNodeB_unique", "enodeB_type", "radio_freq_mod", "macroEnbId_dec

我意识到有一些相关的问题,但我找不到我想要的。过去我多次使用jqgrid,但忘记了如何实现服务器端分页

这是我的javascript

$("#list").jqGrid({
    url: "index.php?loadData=test",
    datatype: "json",
    mtype: "GET",
    colNames: ["id", "eNodeB_unique", "enodeB_type", "radio_freq_mod", "macroEnbId_dec representation", "num_cells"],
    colModel: [
        { name: "id", width: 55 },
        { name: "enodeB_unique", width: 90 },
        { name: "enodeB_type", width: 80, align: "right" },
        { name: "radio_freq_mod", width: 80, align: "right" },
        { name: "macroEnbId_dec_rep", width: 80, align: "right" },
        { name: "num_cells", width: 150, sortable: false }
    ],
    pager: "#pager",
    rowNum: 10,
    rowList: [10, 20, 30],
    sortname: "id",
    sortorder: "desc",
    viewrecords: true,
    gridview: true,
    autoencode: true,
    caption: "My first grid",
    loadonce:false
}); 
还有我的服务器端代码

public function getData($page, $limit, $sidx, $sord){

    $query_str = "SELECT COUNT(*) AS count FROM tbl";

    $prepState = $this->DBi->prepare($query_str);             
    $result = $this->DBi->query($prepState);

    $count = $result[0]['count'];


    if( $count > 0 && $limit > 0) { 
        $total_pages = ceil($count/$limit); 
    } else { 
        $total_pages = 0; 
    } 

    if ($page > $total_pages){
        $page = $total_pages;
    }

    $start = $limit * $page - $limit;

    if($start < 0){
        $start = 0;
    }

$query_str = "SELECT * FROM tbl ORDER BY {$sidx} {$sord} LIMIT {$start}, {$limit}";

    $prepState = $this->DBi->prepare($query_str);             
    $result = $this->DBi->query($prepState);        

    return $result;     


}
publicstaticdynamictojson(此IEnumerable集合、字符串sidx、字符串sord、字符串页、字符串行、列表列)
{
返回到JSON(sidx、sord、页面、行、集合、列);
}
私有静态动态ToJson(字符串sidx、字符串sord、字符串页、字符串行、IEnumerable集合、列表列)
{
page=page.NotNull(“1”);rows=rows.NotNull(“100”);sidx=sidx.NotNull(“Id”);sord=sord.NotNull(“asc”);
int pageIndex=Convert.ToInt32(第页)-1;
int pageSize=Convert.ToInt32(行);
int totalRecords=Collection.Count();
int totalPages=(int)数学上限((float)totalRecords/(float)pageSize);
如果(!Collection.IsNull())
{
Collection=Collection.ToList().OrderWith(x=>x.GetPropertyValue(sidx),sord).Skip(pageSize*pageIndex).Take(pageSize);
返回JsonData(集合、总计页面、页面、总计记录、列);
}
返回BlankJson();
}
私有静态动态JsonData(IEnumerable集合、int totalPages、字符串页、int totalRecords、列表列)
{
var colsExpr=Columns.ConvertAll(x=>extensions.GetProperty(x));
var jsonData=new
{
总计=总页数,
第页,
记录=总记录,
rows=collection.Select(row=>new{id=row.GetPropertyValue(“id”)??Guid.NewGuid(),cell=GetValues(colsExpr,row)}),
};
返回jsonData;
}
公共静态动态BlankJson()
{
还新
{
总计=0,
第页=0,
记录=0,
行数=”,
};
}
私有静态字符串[]GetValues(列表列,T obj)
{
var值=新列表();
尝试
{
foreach(列中的变量x)
{
var temp=x.GetValue(obj,null);
Add(temp!=null?temp.ToString():string.Empty);
}
返回值。ToArray();
}
接住
{
返回值。ToArray();
}
}
$query_str = "SELECT * FROM enodeB ORDER BY {$sidx} {$sord} LIMIT {$start}, {$limit}";

    $prepState = $this->DBi->prepare($query_str);             
    $result = $this->DBi->query($prepState);        

    $finalRows = array();

    foreach($result as $row){
        $finalRows[] = array('cell'=> $row);

    }
    return array('page' => $page, 'total' => $total_pages, 'records' => $count, 'rows' => $finalRows);
    public static dynamic ToJson<T>(this IEnumerable<T> Collection, string sidx, string sord, string page, string rows, List<string> Columns)
    {
        return ToJson<T>(sidx, sord, page, rows, Collection, Columns);
    }



    private static dynamic ToJson<T>(string sidx, string sord, string page, string rows, IEnumerable<T> Collection, List<string> Columns)
    {
        page = page.NotNull("1"); rows = rows.NotNull("100"); sidx = sidx.NotNull("Id"); sord = sord.NotNull("asc");
        int pageIndex = Convert.ToInt32(page) - 1;
        int pageSize = Convert.ToInt32(rows);
        int totalRecords = Collection.Count();
        int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
        if (!Collection.IsNull())
        {
            Collection = Collection.ToList().OrderWith(x => x.GetPropertyValue(sidx), sord).Skip(pageSize * pageIndex).Take(pageSize);
            return JsonData<T>(Collection, totalPages, page, totalRecords, Columns);
        }
        return BlankJson();
    }


    private static dynamic JsonData<T>(IEnumerable<T> collection, int totalPages, string page, int totalRecords, List<string> Columns)
    {

        var colsExpr = Columns.ConvertAll<PropertyInfo>(x => Extentions.GetProperty<T>(x));
        var jsonData = new
        {
            total = totalPages,
            page = page,
            records = totalRecords,
            rows = collection.Select(row => new { id = row.GetPropertyValue("Id") ?? Guid.NewGuid(), cell = GetValues<T>(colsExpr, row) }),
        };
        return jsonData;
    }

    public static dynamic BlankJson()
    {
        return new
        {
            total = 0,
            page = 0,
            records = 0,
            rows = "",
        };
    }

    private static string[] GetValues<T>(List<PropertyInfo> columns, T obj)
    {

        var values = new List<string>();
        try
        {
            foreach (var x in columns)
            {

                var temp = x.GetValue(obj, null);
                values.Add(temp != null ? temp.ToString() : string.Empty);
            }
            return values.ToArray();
        }
        catch
        {
            return values.ToArray();
        }
    }