Author Topic: The grid is not being filled  (Read 2257 times)

celsol

  • Newbie
  • *
  • Posts: 3
    • View Profile
The grid is not being filled
« on: January 21, 2017, 07:32:53 pm »
Hi! I'm new to pqgrid. I modified a demo script but it does not work.
Remote access via PHP.
The problem is that getData, on line 39, always return UNDEFINED...
Could someone help me, please?

The javascript:
<!DOCTYPE html>
<html>
<head>
<!--jQuery dependencies-->
   <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/themes/base/jquery-ui.css" />   
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
   <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<!--PQ Grid files-->
    <link rel="stylesheet" href="../grid-2.1.0/pqgrid.min.css" />
    <script src="../grid-2.1.0/pqgrid.min.js"></script>
<!--PQ Grid Office theme-->
    <link rel="stylesheet" href="../grid-2.1.0/themes/office/pqgrid.css" />
<script>
$(function () {
    var obj = {
        width: 700,
        height: 400,
        numberCell:{resizable:true,title:"#",width:30,minWidth:30},
//       editor: {type: 'textbox'},
        title: "ParamQuery Grid with JSON Data",
        resizable:true,
        scrollModel:{autoFit:true, theme:true},
        draggable:true
    };
    obj.colModel = [{
               title: "Procedimento",
               width: 150,
               dataIndx: "procedimento"
                }];
    obj.dataModel = { location: "remote",
                  dataType: "JSON",
                 method: "GET",
                 url: "get_procedimentos_cnes_1.php?cnes=2792176",
                      error: function(jq){
                         alert("jq error");               
                         console.log(JSON.stringify(jq));
                      },
                      getData: function (dataJSON) {
                          console.log(dataJSON.data);
                         return { data: dataJSON.data };
                    }};
   $("#grid_json").pqGrid(obj);
});
</script>   
</head>
<body>
<div id="grid_json" style="margin:100px;"></div>
</body>

</html>


The PHP:
<?php

session_start();
include "db_connect.php";
$cnes = $_GET['cnes'];
$sql = "SELECT DISTINCT
        b.vl_sh procedimento
    FROM
        aih_rd a,
        tb_procedimento b
    WHERE
            a.proc_rea = b.co_procedimento
            AND a.cnes = '$cnes'
";
//$_SESSION['sqlGrid'] = $sql;
$res = mysqli_query($con, $sql);
if (!$res) {
    echo "erro na consulta: " . mysqli_errno() . $sql;
}
$dbdata = array();
while ($row = $res->fetch_assoc()) {
    $dbdata[] = $row;
}
$res->close();
header('Content-Type: application/json');
//Print array in JSON format
echo json_encode($dbdata);

?>

celsol

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: The grid is not being filled
« Reply #1 on: January 21, 2017, 08:09:53 pm »
I forgot to say that from the console I can see that php returns a valid json

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6310
    • View Profile
Re: The grid is not being filled
« Reply #2 on: January 23, 2017, 06:52:11 am »
In dataModel.getData callback, data is accessed as dataJSON.data

So response from server should be in this format.

{"data": [{}, {},...] }

Check the PHP tab in this example: https://paramquery.com/demos/sorting_remote

And sample response from server: https://paramquery.com/pro/orders/get
« Last Edit: January 23, 2017, 06:54:26 am by paramquery »

celsol

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: The grid is not being filled
« Reply #3 on: January 23, 2017, 11:48:33 pm »
it works... thanks a lot.