0

我有这个 php 代码,我使用 getdocuments 方法连接到 Web 服务,我传递了 limit 、 pageNumber 和可选的过滤器数组,除了记录总数和页码之外,它还返回一个数据数组

我想在 jqgrid 中显示结果,我想带上例如 20 条记录,当用户转到下一页时,带上接下来的 20 条记录。

<?php

//ini_set("display_errors","1");
require_once 'grid/jq-config.php';
// include the jqGrid Class
require_once "grid/jqGrid.php";
// include the driver class
require_once "grid/jqGridArray.php";

// include the calendar
require_once "grid/jqCalendar.php"; 

// include the document class 
require_once "lib/document.php";
// include heler.php which contain some helper functions
require_once "lib/helper.php";
// create the array connection
$conn = new jqGridArray();
// Create the jqGrid instance
$grid = new jqGridRender($conn);
// prepare array that contains fileds name which user can filter on 
$fields = array("BatchNumber", "SenderCode","ReceiverCode","DocumentNumber","DocumentType","InResponse","SubmitDate");


if(get_magic_quotes_gpc()){
  $d = stripslashes($_REQUEST["filters"]);
}else{
  $d = $_REQUEST["filters"];
}
$d = json_decode($d);

for($i = 0; $i < count($d->rules); $i++){
    foreach ($fields as $value) {
        if($d->rules[$i]->field == $value){
            $option[$value] == $d->rules[$i]->data;
        }
    }
}


if(isset($_GET["page"])){
 $option["PageNumber"] = $_GET["page"];
}
else{
    $option["PageNumber"] = 1;
}

if(isset($_GET["rows"])){
    $option["Limit"]  = $_GET["rows"];
}   
else{
    $option["Limit"] = LIMIT_DOCUMENT;
}



 $results = Document::getDocuments($option);
 $arrResult = _object_to_array($results);


$documents = $arrResult["Documents"]["DocumentDataModel"];
$totalCount = $arrResult["TotalCount"] ;
$totalPages = ceil($totalCount/ $option["Limit"]);
// Always you can use SELECT * FROM data1
$grid->SelectCommand = "SELECT BatchNumber, SenderCode ,ReceiverCode , DocumentNumber, DocumentType, InResponse, SubmitDate,LastModifiedDate FROM documents";
$grid->dataType = 'json';
$grid->setPrimaryKeyId('BatchNumber');

$grid->setColModel();

//enable subgrid
// Set the parameters for the subgrid
$grid->setSubGrid("subgrid_document_attachments.php",
        array('File Name'),
        array(60),
        array('left')); 




// Enable toolbar searching 
$grid->toolbarfilter = true;
$grid->setFilterOptions(array("stringResult"=>TRUE)); 

$grid->setUrl('grid_documents.php');



$grid->setGridOptions(array(
    "width" => 1124,
    "height" => 400,
    "rowList"=>array(10,20,30,40,50,100),
    "sortname"=>"id",
    "caption" => "Documents", 
    "total" => $arrResult["TotalPages"],
    "records" => $arrResult['TotalCount'],
    "page" => $arrResult['PageNumber'],
));

$grid->setColProperty("BatchNumber", array("label"=>"Batch Number"));
$grid->setColProperty("SubmitDate", array(
    "formatter"=>"date",
    "formatoptions"=>array("srcformat"=>"Y-m-d H:i:s","newformat"=>"m/d/Y")
    )
); 
// format the last modified date 
$grid->setColProperty("LastModifiedDate", array(
    "formatter"=>"date",
    "formatoptions"=>array("srcformat"=>"Y-m-d H:i:s","newformat"=>"m/d/Y")
    )
); 

// add date picker to submit date
$grid->setDatepicker("SubmitDate",array("buttonOnly"=>FALSE));
$grid->datearray = array('SubmitDate');




// Enable navigator
$grid->navigator = true;
// Enable search
$grid->setNavOptions('navigator', array("excel"=>TRUE,"add"=>false,"edit"=>false,"del"=>false,"view"=>false,"csv"=>FALSE, "pdf"=>false));
// Activate single search
$grid->setNavOptions('search',array("multipleSearch"=>false));
// Enjoy

$grid->renderGrid('#grid','#pager',true, null, null, true,true);

?>

我检查了firebug的http请求,{“records”:20,“page”:1,“total”:1},网格只显示20条记录和1页。我想让它显示 20 条记录并启用分页,这样我就可以按下一步并带来接下来的 20 条记录。我希望这些值类似于 {"total":"56","page":"1","re​​cords":"560"

4

1 回答 1

0

jqGrid 会将所有信息传递给您的 Web 服务,您需要检索需要显示的正确数据页面。排序索引(sidx)、排序顺序(sord)、页(page)和行(rows)。

您的控制器/Web 服务可以获取此信息,然后从您的数据集中获取适当的数据。

        public ActionResult getGridData(string sidx, string sord, int page, int rows, bool _search, string filters) {
   ...
   var pagedDataset = fullDataset.OrderBy(sidx + " " + sord).Skip((page - 1) * rows).Take(rows);

  //then format the pagedDataset for the jqGrid and pass it back.
于 2013-03-30T15:52:24.267 回答