Bom dia a todos.
Estou desenvolvendo um aplicativo onde tenho uma tabela, em que uso o jqGrid.
O único problema é que ele não está trazendo os dados.
Ele mostra que existem x dados no banco, porém não os exibe na tabela.
Estou postando o código para que possam me ajudar.
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JQuery Grid Demos TESTE</title>
<!-- JS -->
<script src="JS/JQueryUI/js/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="JS/JQueryUI/js/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
<script src="JS/JQGrid/js/i18n/grid.locale-pt-br.js" type="text/javascript"></script>
<script type="text/javascript">
$.jgrid.no_legacy_api = true;
$.jgrid.useJSON = true;
</script>
<script src="JS/JQGrid/plugins/ui.multiselect.js" type="text/javascript"></script>
<script src="JS/JQGrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
<!-- CSS -->
<link rel="stylesheet" type="text/css" media="screen" href="JS/JQueryUI/css/cupertino/jquery-ui-1.8.16.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="JS/JQGrid/css/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" media="screen" href="JS/JQGrid/plugins/ui.multiselect.css" />
</head>
<body>
<table id="tblPesquisa"></table>
<div id="divPesquisa"></div>
<script>
jQuery('#tblPesquisa').jqGrid({
url:'php/rAJAX.php', //Aqui o arquivo de retorno
datatype: 'xml', //Aqui o tipo de retorno
colNames:['Código','Nome'],
colModel:[
{name:'codigo',index:'codigo', width:150},
{name:'nome',index:'nome', width:150}
],
rowNum:5,
rowList:[5,10,20],
height: 'auto',
pager: '#divPesquisa',
sortname: 'nome',
viewrecords: true,
sortorder: 'asc',
caption:'Usando jqGrid com PHP e AJAX'
});
</script>
</body>
</html>
rAJAX.php
<?php
include_once 'conecta.php';
$page = $_REQUEST['page']; // página solicitada
$limit = $_REQUEST['rows']; // obter quantas linhas nós queremos ter no grid
$sidx = $_REQUEST['sidx']; // obter linha de índice - ou seja, o clique do usuário para classificar
$sord = $_REQUEST['sord']; // obter a direção
if(!$sidx){
$sidx =1;
}
$totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows']: false;
if($totalrows){
$limit = $totalrows;
}
try{
// conecantdo no banco de dados
$db = new PDO($dsn, $user, $pass);
$stmt = $db->prepare("SELECT COUNT(*) AS count FROM teste");
$stmt->execute();
}catch(PDOException $error){
echo $error->getMessage();
}
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($row as $resultado){
$count = $resultado['count'];
}
if( $count > 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;
$SQL = "SELECT * FROM teste ORDER BY :sidx :sord LIMIT :start , :limit";
try{
$stmt = $db->prepare($SQL);
$stmt->bindValue(':sidx',$sidx,PDO::PARAM_STR);
$stmt->bindValue(':sord',$sord,PDO::PARAM_STR);
$stmt->bindValue(':start',$start,PDO::PARAM_STR);
$stmt->bindValue(':limit',$limit,PDO::PARAM_STR);
$stmt->execute();
}catch(PDOException $error){
echo $error->getMessage();
}
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
header("Content-type: application/xhtml+xml;charset=iso-8859-1");
} else {
header("Content-type: text/xml;charset=iso-8859-1");
}
//retorno xml
$et = ">";
echo "<?xml version='1.0' encoding='iso-8859-1'?$et\n";
echo "<rows>";
echo "<page>".$page."</page>";
echo "<total>".$total_pages."</total>";
echo "<records>".$count."</records>";
// não se esqueça de colocar os dados em texto no CDATA
foreach($row as $linha){
echo "<row id='". $linha[codigo]."'>";
echo "<cell>". $linha[codigo]."</cell>";
echo "<cell>". $linha[nome]."</cell>";
echo "<cell><![CDATA[". $linha[nome]."]]></cell>";
echo "</row>";
}
echo "</rows>";
?>
Desde já agradeço a todos que responderem.