0

I have this php code which I use getdocuments method which connect to a web service, I pass the limit , pageNumber and optional filter array and it returns an array of data in addition to the total count of records and page number

I want to display the result in jqgrid, I want to bring for example 20 records and when user go the next page, bring the next 20 records.

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

?>

I inspected the http request by firebug, {"records":20,"page":1,"total":1}, and grid just display 20 records with 1 page. I want to it to display 20 records and enable pagination so I can press next and bring the next 20 records. I want these values to be something like {"total":"56","page":"1","records":"560"

Mark
  • 3,123
  • 4
  • 20
  • 31
Kamal
  • 363
  • 8
  • 25
  • So you want to be able to page through records? – Mark Mar 30 '13 at 15:30
  • @Mark yes I want to be able to page through records, but without retrieving all records every request, I mean bring 20 records from web service function, enable pagination, when user click on next page, bring the next 20 records – Kamal Mar 30 '13 at 15:35
  • Did you fix the problem? – Mark Apr 16 '13 at 11:43

1 Answers1

0

jqGrid will pass all the information to your web service you will need to retrieve the correct page of data you need to display. The sort index (sidx), sort order (sord), page (page) and rows (rows).

You controller/web service can get this information and then grab the appropriate data from your dataset.

        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.
Mark
  • 3,123
  • 4
  • 20
  • 31