Jqgrid Problem - El Forum - 03-01-2010
[eluser]phpserver[/eluser]
I'm in a situation where i am needed to choose a table(will be a form with a select box) and display the data on an editable grid.Let me explain that in code:Here is my jqgrid code,notice how we select all the data from the table 'stetbl':
Grid.php - Controller
Code: function ciGridTest()
{
$data['title'] = 'jQGrid Codeigniter Integration Demo'; //Page Title
$data['main'] = 'jqgcid'; //view script file name i.e. jqgcid.php in system/application/view folder
$this->load->vars($data);
$this->load->view('template1'); //the default template you are going to use
}
function gridServerPart()
{
$page = isset($_POST['page'])?$_POST['page']:1; // get the requested page
$limit = isset($_POST['rows'])?$_POST['rows']:10; // get how many rows we want to have into the grid
$sidx = isset($_POST['sidx'])?$_POST['sidx']:'state'; // get index row - i.e. user click to sort
$sord = isset($_POST['sord'])?$_POST['sord']:''; // get the direction
if(!$sidx) $sidx =1;
$SQL = "SELECT * FROM `stetbl`";
$result = $this->DB_Func->executeCustomQuery( $SQL );
$count = count($result);
if( $count > 0 ) {
$total_pages = ceil($count/$limit); //calculating total number of pages
} else {
$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
$start = ($start<0)?0:$start; // make sure that $start is not a negative value
$SQL = "SELECT * FROM `stetbl` ORDER BY {$sidx} {$sord} LIMIT {$start}, {$limit}";
$result = $this->DB_Func->executeCustomQuery( $SQL ); //here DB_Func is a model handling DB interaction
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
header("Content-type: application/xhtml+xml;charset=utf-8");
} else {
header("Content-type: text/xml;charset=utf-8");
}
$et = ">";
echo "<?xml version='1.0' encoding='utf-8'?$et\n";
echo "<rows>";
echo "<page>".$page."</page>";
echo "<total>".$total_pages."</total>";
echo "<records>".$count."</records>";
// be sure to put text data in CDATA
foreach($result as $row) {
echo "<row id='".$row['state']."'>";
echo "<cell><![CDATA[".$row['state']."]]></cell>";
echo "<cell><![CDATA[".$row['meaning']."]]></cell>";
echo "</row>";
}
echo "</rows>";
}
jqgcid.php - The View
Code: <?
$ci =& get_instance();
$base_url = base_url();
?>
<table id="list1"></table> <!--Grid table-->
<div id="pager1"></div> <!--pagination div-->
[removed]
jQuery().ready(function (){
jQuery("#list1").jqGrid({
url:'<?=$base_url.'gridServerPart'?>', //another controller function for generating XML data
mtype : "post", //Ajax request type. It also could be GET
datatype: "xml", //supported formats XML, JSON or Arrray
colNames:['State','Meaning'], //Grid column headings
colModel:[
{name:'state',index:'state', width:20, align:"left"},
{name:'meaning',index:'meaning', width:55, align:"left"},
],
rowNum:10,
width: 450,
height: 150,
rowList:[10,20,30],
pager: '#pager1',
sortname: 'state',
viewrecords: true,
caption:"jQGrid with Codeigniter"
}).navGrid('#pager1',{edit:false,add:false,del:false});
});
[removed]
Finally,template1.php - View
Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<title><?=$title?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="<?=base_url();?>css/ui.all.css" rel="stylesheet" type="text/css" />
<link href="<?=base_url();?>css/ui.jqgrid.css" rel="stylesheet" type="text/css" />
<link href="<?=base_url();?>css/redmond/jquery-ui-1.7.1.custom.css" rel="stylesheet" type="text/css" />
<link href="<?=base_url();?>css/ui.multiselect.css" rel="stylesheet" type="text/css" />
[removed]
// <![CDATA[
base_url = '<?=base_url()?>';
//]]>
[removed]
[removed][removed]
[removed][removed]
[removed][removed]
[removed][removed]
</head>
<body>
<?php $this->load->view($main);?>
</body>
</html>
I have tried using post values and it worked but did not work when i refreshed the page.I am thinking of disabling the refresh button when the grid is rendered but i am yet to know how to go about it.If you have ideas on how i can sort my problem,that'wld be nice.
|