Jquery JqGrid与JSON空网格
我读了很多以前的问题,但我不能解决。我真的很感激你的帮助。 我曾经使用带有xml的jqGrid,它工作得很好,现在我需要使用JSON,但是网格总是空的。我和WAMP一起工作 服务器端回答:Jquery JqGrid与JSON空网格,jquery,json,jqgrid,Jquery,Json,Jqgrid,我读了很多以前的问题,但我不能解决。我真的很感激你的帮助。 我曾经使用带有xml的jqGrid,它工作得很好,现在我需要使用JSON,但是网格总是空的。我和WAMP一起工作 服务器端回答: { "page":"1", "total":"1", "records":"2", "rows":[ {"id":"campo1","cell":["campo1","campo3"]}, {"id":"campo11","cell":["cam
{
"page":"1",
"total":"1",
"records":"2",
"rows":[
{"id":"campo1","cell":["campo1","campo3"]},
{"id":"campo11","cell":["campo11","campo33"]}
]
}
第一个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">
<head>
<meta http-equiv="Content-Type" content="application/json; charset=ISO-8859-1"/>
<title>jqGrid Ejemplo 1: Cargar datos de una tabla MySql </title>
<link rel="stylesheet" type="text/css" media="screen" href="css/flick/jquery-ui-1.8.16.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="jqgrid/css/ui.jqgrid.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
<script src="jqgrid/js/i18n/grid.locale-es.js" type="text/javascript"></script>
<script src="jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
jQuery("#tblclientes").jqGrid({
url:'clientes2.php',
datatype: 'json',
mtype: 'POST',
colNames:['ID','NOMBRE'],
colModel:[
{name:'idCliente', index:'idCliente', width:50 },
{name:'nombre', index:'nombre', width:160 }
],
pager: '#paginacion',
rowNum:10,
sortname: 'idCliente',
sortorder: 'asc',
viewrecords: true
});
});
</script>
</head>
<body>
<table id="tblclientes"></table>
<div id="paginacion"> </div>
</body>
</html>
jqGrid eJompo 1:Cargar datos de una tabla MySql
$(文档).ready(函数(){
jQuery(“#tblclients”).jqGrid({
url:'clientes2.php',
数据类型:“json”,
mtype:“POST”,
colNames:['ID','NOMBRE'],
colModel:[
{名称:'idCliente',索引:'idCliente',宽度:50},
{名称:'nombre',索引:'nombre',宽度:160}
],
寻呼机:“#页码”,
rowNum:10,
sortname:“idCliente”,
排序器:“asc”,
viewrecords:正确
});
});
服务器端
<?php
$page = $_POST['page'];
$limit = $_POST['rows'];
$sidx = $_POST['sidx'];
$sord = $_POST['sord'];
if(!$sidx) $sidx =1;
$conexion = new mysqli("127.0.0.1","root","","deyertdb");
$result = $conexion->query("SELECT COUNT(*) AS count FROM code2");
$fila = $result->fetch_array();
$count = $fila['count'];
if( $count >0 ) $total_pages = ceil($count/$limit);else $total_pages = 0;
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit;
$consulta = "SELECT StockCode as idCliente, Mfr as nombre FROM code2 ORDER BY $sidx $sord LIMIT $start , $limit;";
$result = $conexion->query($consulta);
$respuesta->page = $page;
$respuesta->total = "$total_pages";
$respuesta->records = $count;
$i=0;
while( $fila = $result->fetch_assoc() ) {
$respuesta->rows[$i]['id']=$fila["idCliente"];
$respuesta->rows[$i]['cell']=array($fila["idCliente"],$fila["nombre"]);
$i++;
}
echo json_encode($respuesta);
?>
我无法重现您描述的问题。实际使用您的代码(只需很少的修改),并使用我保存在文件中的JSON数据。所有的工作都没有任何问题。我不是PHP开发人员,但可能您应该添加这一行
header("Content-Type: application/json; charset=utf-8");
在
echo json_encode($respuesta)之前代码>亲爱的奥列格,我真的非常感谢你的帮助。我和你的演示一起在文件中加载JSON字符串,就像你给我的“演示”一样,这让我有了更多的想法,最后我在stackoverflow.com找到了答案。
这可能很有趣,但只要一行PHP就可以解决所有问题。只放
$response = new stdClass();
以前
$response->page = $page;
谢谢Oleg和Mariusz
详细的解决方案在本文的最后一部分
如果您复制HTML页面的确切代码,效果会更好。你真的使用
而不是
?你真的使用http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“
而不是”http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“
?为什么在代码的5个位置包含…
?你真的使用了jsonreader})代码>在您的代码中?亲爱的Oleg,我更新了确切的代码,感谢您的观察。亲爱的Oleg我真的感谢您的帮助。我尝试了“演示”,这打开了我的思路和更多的想法,最后我在stackoverflow.com找到了答案,这可能很有趣,但只要一行PHP就可以解决所有问题。仅放置$response=new stdClass();在$response->page=$page之前;谢谢。@user3078388:不客气!如果我能帮助你,我很高兴,但正如我写的,我不是PHP开发人员,所以只能猜测你问题的原因。