我意识到有一些相关的问题,但我无法找到我想要的东西。我过去多次使用过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);
答案 0 :(得分:0)
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();
}
}