jqGrid data viewing

Hi there,

I am trying to work with the jqGrid for PHP.

In my SiteController index:




public function actionIndex() {

		

		$conn = new PDO ( DB_DSN, DB_USER, DB_PASSWORD );

		$conn->query ( "SET NAMES utf8" );

		

		// Create the jqGrid instance

		$grid = new jqGridRender ( $conn );

		// Write the SQL Query

		$grid->SelectCommand = 'SELECT OrderID, OrderDate, CustomerID, Freight, ShipName FROM orders';

		// set the ouput format to json

		$grid->dataType = 'json';

		// Let the grid create the model

		$grid->setColModel ();

		// renders the view file 'protected/views/site/index.php'

		

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

		$grid->setGridOptions ( array ("caption" => "This is custom Caption", "rowNum" => 10, "sortname" => "OrderID", "hoverrows" => true, "rowList" => array (10, 20, 50 ) ) );

		// Change some property of the field(s)

		$grid->setColProperty ( "OrderID", array ("label" => "ID", "width" => 60 ) );

		$grid->setColProperty ( "OrderDate", array ("formatter" => "date", "formatoptions" => array ("srcformat" => "Y-m-d H:i:s", "newformat" => "m/d/Y" ) ) );

		// Enjoy

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

		$conn = null;

		

		// using the default layout 'protected/views/layouts/main.php'

		$this->render ( 'index', $grid );

	}



In my index view-file, doing print_r($data) gives me the result of the table. How can I render the data so that it will be in the expected result, which is in grid format?

I changed what I’ve done. Instead of putting the codes in SiteController, I created _index.php under /views/site/ and put there the codes, then in the index I put there




<?php $this->renderPartial('_index') ;?>



Still,without success.

Help please :(

Updates!

I was able to render the grid now, I just forgot to register the js and css of jqGri…silly me :D

The problem left now is that it has no content…the table is there, but its blank :(


<?php $this->renderPartial('_index') ;?> 

is returning the complete page source, instead of just


{"records":"830","page":1,"total":83,"rows":[{"OrderID":"10248","OrderDate":"1996-07-04 00:00:00","CustomerID":"WILMK","Freight":"32.3800","ShipName":"Vins et alcools Chevalier"},{"OrderID":"10249","OrderDate":"1996-07-05 00:00:00","CustomerID":"TRADH","Freight":"11.6100","ShipName":"Toms Spezialit"},{"OrderID":"10250","OrderDate":"1996-07-08 00:00:00","CustomerID":"HANAR","Freight":"65.8300","ShipName":"Hanari Carnes"},{"OrderID":"10251","OrderDate":"1996-07-08 00:00:00","CustomerID":"VICTE","Freight":"41.3400","ShipName":"Victuailles en stock"},{"OrderID":"10252","OrderDate":"1996-07-09 00:00:00","CustomerID":"SUPRD","Freight":"51.3000","ShipName":"Supr"},{"OrderID":"10253","OrderDate":"1996-07-10 00:00:00","CustomerID":"HANAR","Freight":"58.1700","ShipName":"Hanari Carnes"},{"OrderID":"10254","OrderDate":"1996-07-11 00:00:00","CustomerID":"CHOPS","Freight":"22.9800","ShipName":"Chop-suey Chinese"},{"OrderID":"10255","OrderDate":"1996-07-12 00:00:00","CustomerID":"RICSU","Freight":"148.3300","ShipName":"Richter Supermarkt"},{"OrderID":"10256","OrderDate":"1996-07-15 00:00:00","CustomerID":"WELLI","Freight":"13.9700","ShipName":"Wellington Importadora"},{"OrderID":"10257","OrderDate":"1996-07-16 00:00:00","CustomerID":"HILAA","Freight":"81.9100","ShipName":"HILARI"}]}



How can I ask renderPartial to return just what I need?

Fixed the problem. It was ugly, but it was fixed.

Okay, I can’t sleep with my codes this dirty.

This is my view file:




//protected/views/site/index.php

<?php

$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);

// Tell the db that we use utf-8

$conn->query("SET NAMES utf8");


// Create the jqGrid instance

$grid = new jqGridRender($conn);

// Write the SQL Query

$grid->SelectCommand = 'SELECT OrderID, OrderDate, CustomerID, Freight, ShipName FROM orders';

// set the ouput format to json

$grid->dataType = 'json';

// Let the grid create the model

$grid->setColModel();

// Set the url from where we obtain the data

$grid->setUrl('output');

// Set grid caption using the option caption

$grid->setGridOptions(array(

    "caption"=>"This is custom Caption",

    "rowNum"=>10,

    "sortname"=>"OrderID",

    "hoverrows"=>true,

    "rowList"=>array(10,20,50),

    ));

// Change some property of the field(s)

$grid->setColProperty("OrderID", array("label"=>"ID", "width"=>60));

$grid->setColProperty("OrderDate", array(

    "formatter"=>"date",

    "formatoptions"=>array("srcformat"=>"Y-m-d H:i:s","newformat"=>"m/d/Y")

    )

);

// Enjoy

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

$conn = null;

?>



And then in my SiteController, I have created the ‘output’ action:




//SiteController.php

public function actionOutput() {

		regs()->registerScriptFile(app()->theme->baseUrl.'/js/jqGrid/i18n/grid.locale-en.js');

		regs()->registerScriptFile(app()->theme->baseUrl.'/js/jqGrid/jquery.jqGrid.min.js');


		regs()->registerCssFile(app()->theme->baseUrl.'/css/jqGrid/redmond/jquery-ui-1.8.2.custom.css');

		regs()->registerCssFile(app()->theme->baseUrl.'/css/jqGrid/ui.jqgrid.css');

		$conn = new PDO ( DB_DSN, DB_USER, DB_PASSWORD );

		$conn->query ( "SET NAMES utf8" );

		

		// Create the jqGrid instance

		$grid = new jqGridRender ( $conn );

		// Write the SQL Query

		$grid->SelectCommand = 'SELECT OrderID, OrderDate, CustomerID, Freight, ShipName FROM orders';

		// set the ouput format to json

		$grid->dataType = 'json';

		// Let the grid create the model

		$grid->setColModel ();

		// renders the view file 'protected/views/site/index.php'

		

		$grid->setUrl('output');

		$grid->setGridOptions ( array ("caption" => "This is custom Caption", "rowNum" => 10, "sortname" => "OrderID", "hoverrows" => true, "rowList" => array (10, 20, 50 ) ) );

		// Change some property of the field(s)

		$grid->setColProperty ( "OrderID", array ("label" => "ID", "width" => 60 ) );

		$grid->setColProperty ( "OrderDate", array ("formatter" => "date", "formatoptions" => array ("srcformat" => "Y-m-d H:i:s", "newformat" => "m/d/Y" ) ) );

		// Enjoy

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

		$conn = null;

	}



Though it is rendering the jqGrid, now complete with data, I can’t seem to find a way so that I don’t need to declare everything twice. It bothers me because I intend to use this grid most of the time.

If you have any suggestion on how I can simplify this, please let me know.

I really hope someone will reply now. :(

You can use jqgrid extension to achieve this