doujing5150 2015-03-11 13:13
浏览 35
已采纳

jquery jqgrid根据json对象中的参数显示没有结果

I have set up a jqgrid and it loads the data via php. No results are displayed

The grid is set up with

$(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 + ");\" />";
}

And the corrsponding json output from the php files is

{"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"]}]}

if I remove

$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;

I get results displayed but can not use pagination

  • 写回答

1条回答 默认 最新

  • dsapkqaduj6718493 2015-03-13 14:07
    关注

    i found a solution that works for me so to set up a jqgrid via php mysql with filtering via toolbar and paging i used the following

    $(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 + ");\" />";
    }
    

    the php file used is the following

    <?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);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?