jquery jqgrid 没有显示结果取决于 json 对象中的参数
jquery jqgrid no results displayed depending on parameters in json object
我已经设置了一个 jqgrid,它通过 php 加载数据。没有显示结果
网格设置为
$(document).ready(function () {
$("#list_records").jqGrid({
url: "grid.php",
datatype: "json",
mtype: "GET",
colNames: ["ID", "Encuesta", "Fecha", "Quien","Opciones"],
colModel: [
{ name: "id_consulta"},
{ name: "texto"},
{ name: "fecha"},
{ name: "quien"},
{name:'act',index:'act', width:130,sortable:false,formatter:myFormatter, search:false}
],
pager: "#perpage",
rowNum: 8,
rowList: [10,20],
sortname: "id_consulta",
sortorder: "asc",
height: 'auto',
viewrecords: true,
gridview: true,
caption: ""
});
$('#list_records').jqGrid('filterToolbar', {stringResult: true, searchOnEnter: false, defaultSearch : "bw"});
});
function myFormatter (cellvalue, options, rowObject)
{
var idconsulta = rowObject[0];
return "</a><input type=button value='Enviar' onclick=\"loadingForm(" + idconsulta + ");\" />";
}
php 文件的相应 json 输出是
{"page":1,"total":2,"records":"18","rows":[{"id":"18","cell":["18","Camila quieres participar? ","2015-03-02","dwk"]},{"id":"17","cell":["17","Halo concepci\u00f3n wie gehts ","2015-03-02","idkl"]},{"id":"16","cell":["16","\u00bfAlguien quiere participar?","2015-03-02","dwk"]},{"id":"15","cell":["15","\u00bfEs \u00fatil una aplicaci\u00f3n para realizar encuestas instant\u00e1neas? ","2015-03-02","idkl"]},{"id":"14","cell":["14","Camila quieres participar? ","2015-03-02","idkl"]},{"id":"13","cell":["13","Halo concepci\u00f3n wie gehts ","2015-03-02","idkl"]},{"id":"12","cell":["12","\u00bfAlguien quiere participar?","2015-03-02","dwk"]},{"id":"11","cell":["11","\u00bfEs \u00fatil una aplicaci\u00f3n para realizar encuestas instant\u00e1neas? ","2015-03-02","idkl"]},{"id":"10","cell":["10","Halo concepci\u00f3n wie gehts ","2015-03-02","dwk"]},{"id":"9","cell":["9","\u00bfAlguien quiere participar?","2015-03-02","idkl"]}]}
如果我删除
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
我可以显示结果但不能使用分页
我找到了一个适合我的解决方案,因此通过 php mysql 设置 jqgrid,通过工具栏和分页进行过滤,我使用了以下
$(document).ready(function () {
jQuery('#rowed2').jqGrid({
url:'server.php',
datatype: 'json',
colNames:['ID','ENCUESTA', 'FECHA', 'QUIEN','ACTIONS'],
colModel:[
{ name: 'id_consulta',width:30,search:false,sortable:false},
{ name: 'texto', width:250},
{ name: 'fecha', width:65},
{ name: 'quien', width:100},
{name:'act',index:'act', width:100,sortable:false,formatter:myFormatter, search:false}
],
rowNum:10,
rowList:[10,20,30],
pager: '#prowed2',
sortname: 'ID',
viewrecords: true,
height: '400',
width: '600',
sortorder: 'asc'
});
$('#rowed2').jqGrid('filterToolbar', {stringResult: true, searchOnEnter: false, defaultSearch : "bw"});
});
function myFormatter (cellvalue, options, rowObject)
{
var idconsulta = rowObject[0];
return "</a><input type=button value='Ver Resultado' onclick=\"loadingForm(" + idconsulta + ");\" />";
}
使用的php文件如下
<?php
error_reporting(0);
ini_set('max_execution_time', 600);
require_once 'cliente/config.php';
$db = mysql_connect($server,$user,$pass);
mysql_select_db($dbname,$db);
$page = $_GET['page']; // get the requested page
$limit = $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction
if(!$sidx) $sidx =1; // connect to the database
$filterResultsJSON = json_decode($_GET['filters']);
$counter = 0;
$sql='';
if($filterResultsJSON != "") {
$filterArray = get_object_vars($filterResultsJSON);
while($counter < count($filterArray['rules']))
{
$filterRules = get_object_vars($filterArray['rules'][$counter]);
if($counter == 0){
$sql .= ' WHERE ' . $filterRules['field'] . ' LIKE "%' . $filterRules['data'] . '%"';
}
else {
$sql .= ' AND ' . $filterRules['field'] . ' LIKE "%' . $filterRules['data'] . '%"';
}
$counter++;
}
}
$result = mysql_query("SELECT COUNT(*) AS count FROM consulta $sql");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
if( $count >0 ) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
if ($limit<0) $limit = 0;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if ($start<0) $start = 0;
$SQL2 = "SELECT * from consulta $sql ORDER BY id_consulta desc LIMIT $start , $limit";
$result = mysql_query( $SQL2 ) or die("Couldn?t execute query.".mysql_error());
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$responce->rows[$i]['id']=$row[id_consulta];
$responce->rows[$i]['cell']=array($row['id_consulta'],utf8_encode($row['texto']),$row[fecha],$row[quien],""); $i++;
}
echo json_encode($responce);
?>
我已经设置了一个 jqgrid,它通过 php 加载数据。没有显示结果
网格设置为
$(document).ready(function () {
$("#list_records").jqGrid({
url: "grid.php",
datatype: "json",
mtype: "GET",
colNames: ["ID", "Encuesta", "Fecha", "Quien","Opciones"],
colModel: [
{ name: "id_consulta"},
{ name: "texto"},
{ name: "fecha"},
{ name: "quien"},
{name:'act',index:'act', width:130,sortable:false,formatter:myFormatter, search:false}
],
pager: "#perpage",
rowNum: 8,
rowList: [10,20],
sortname: "id_consulta",
sortorder: "asc",
height: 'auto',
viewrecords: true,
gridview: true,
caption: ""
});
$('#list_records').jqGrid('filterToolbar', {stringResult: true, searchOnEnter: false, defaultSearch : "bw"});
});
function myFormatter (cellvalue, options, rowObject)
{
var idconsulta = rowObject[0];
return "</a><input type=button value='Enviar' onclick=\"loadingForm(" + idconsulta + ");\" />";
}
php 文件的相应 json 输出是
{"page":1,"total":2,"records":"18","rows":[{"id":"18","cell":["18","Camila quieres participar? ","2015-03-02","dwk"]},{"id":"17","cell":["17","Halo concepci\u00f3n wie gehts ","2015-03-02","idkl"]},{"id":"16","cell":["16","\u00bfAlguien quiere participar?","2015-03-02","dwk"]},{"id":"15","cell":["15","\u00bfEs \u00fatil una aplicaci\u00f3n para realizar encuestas instant\u00e1neas? ","2015-03-02","idkl"]},{"id":"14","cell":["14","Camila quieres participar? ","2015-03-02","idkl"]},{"id":"13","cell":["13","Halo concepci\u00f3n wie gehts ","2015-03-02","idkl"]},{"id":"12","cell":["12","\u00bfAlguien quiere participar?","2015-03-02","dwk"]},{"id":"11","cell":["11","\u00bfEs \u00fatil una aplicaci\u00f3n para realizar encuestas instant\u00e1neas? ","2015-03-02","idkl"]},{"id":"10","cell":["10","Halo concepci\u00f3n wie gehts ","2015-03-02","dwk"]},{"id":"9","cell":["9","\u00bfAlguien quiere participar?","2015-03-02","idkl"]}]}
如果我删除
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
我可以显示结果但不能使用分页
我找到了一个适合我的解决方案,因此通过 php mysql 设置 jqgrid,通过工具栏和分页进行过滤,我使用了以下
$(document).ready(function () {
jQuery('#rowed2').jqGrid({
url:'server.php',
datatype: 'json',
colNames:['ID','ENCUESTA', 'FECHA', 'QUIEN','ACTIONS'],
colModel:[
{ name: 'id_consulta',width:30,search:false,sortable:false},
{ name: 'texto', width:250},
{ name: 'fecha', width:65},
{ name: 'quien', width:100},
{name:'act',index:'act', width:100,sortable:false,formatter:myFormatter, search:false}
],
rowNum:10,
rowList:[10,20,30],
pager: '#prowed2',
sortname: 'ID',
viewrecords: true,
height: '400',
width: '600',
sortorder: 'asc'
});
$('#rowed2').jqGrid('filterToolbar', {stringResult: true, searchOnEnter: false, defaultSearch : "bw"});
});
function myFormatter (cellvalue, options, rowObject)
{
var idconsulta = rowObject[0];
return "</a><input type=button value='Ver Resultado' onclick=\"loadingForm(" + idconsulta + ");\" />";
}
使用的php文件如下
<?php
error_reporting(0);
ini_set('max_execution_time', 600);
require_once 'cliente/config.php';
$db = mysql_connect($server,$user,$pass);
mysql_select_db($dbname,$db);
$page = $_GET['page']; // get the requested page
$limit = $_GET['rows']; // get how many rows we want to have into the grid
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort
$sord = $_GET['sord']; // get the direction
if(!$sidx) $sidx =1; // connect to the database
$filterResultsJSON = json_decode($_GET['filters']);
$counter = 0;
$sql='';
if($filterResultsJSON != "") {
$filterArray = get_object_vars($filterResultsJSON);
while($counter < count($filterArray['rules']))
{
$filterRules = get_object_vars($filterArray['rules'][$counter]);
if($counter == 0){
$sql .= ' WHERE ' . $filterRules['field'] . ' LIKE "%' . $filterRules['data'] . '%"';
}
else {
$sql .= ' AND ' . $filterRules['field'] . ' LIKE "%' . $filterRules['data'] . '%"';
}
$counter++;
}
}
$result = mysql_query("SELECT COUNT(*) AS count FROM consulta $sql");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
if( $count >0 ) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
if ($limit<0) $limit = 0;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if ($start<0) $start = 0;
$SQL2 = "SELECT * from consulta $sql ORDER BY id_consulta desc LIMIT $start , $limit";
$result = mysql_query( $SQL2 ) or die("Couldn?t execute query.".mysql_error());
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$responce->rows[$i]['id']=$row[id_consulta];
$responce->rows[$i]['cell']=array($row['id_consulta'],utf8_encode($row['texto']),$row[fecha],$row[quien],""); $i++;
}
echo json_encode($responce);
?>