【问题标题】:jqgrid and server side paginationjqgrid 和服务器端分页
【发布时间】:2014-05-17 09:53:15
【问题描述】:

我知道有一些相关的问题,但我找不到我要找的东西。以前用过很多次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;     


}

如果我在查询中保留 $start 和 $limit,那么我只会得到最初的十个结果。如果我把它们拿出来......那么我的网格会显示我所有的结果......但只有一页可用。我可以选择点击下一页。

编辑:

好的,我现在意识到我必须返回此信息。我对返回行的方式感到困惑。 JQgrid 一直都是这样吗?

$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);

【问题讨论】:

    标签: php jquery mysql jqgrid pagination


    【解决方案1】:
        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();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2011-06-16
      • 2015-09-07
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多