php - 免费的 jqGrid - 使用 PHP 的 JSON 编码示例

标签 php json jqgrid

免费的 jqGrid 使用以下 JSON 名称:值对格式的数据:

var data = {
        "page": "1",
        "records": "3",
        "rows": [
            { "DataID": "1", "DataDesc": "Test 1", "DataTitle": "Test 1" }
        ]
    };

我在 PHP 脚本中有以下内容:

$i=0; 
while ($row = mysql_fetch_assoc($result)) {
      $data->rows[$i]['cell']=array($row); 
      $i++;
}
print json_encode($data);

哪个返回:

{"rows":[{"cell":[{"user_id":"00082563","first_name":"Peter","case_title":"Male with STI (urethritis)","case_started":"2017-06-02 10:52:10"}]}]}

看起来没问题。但是,对于下面代码的 JSON 部分,网格根本不显示。

    function loadFirstGrid() {
        $("#FirstGrid").jqGrid({
            url: "scripts/json_test.php?user=" + user,
            dataType: "json",
            mtype: "GET",
            postData: {
                json: JSON.stringify(data)
            },
            colModel: [{
        name: "user_id",
        label: "User ID",
        width: 120
    },
    {
        name: "first_name",
        label: "Name",
        width: 400
    },
    {
        name: "case_title",
        label: "Case Title",
        width: 500
    },
    {
        name: "case_started",
        label: "Case Started",
        width: 200
    },
],
            emtyrecords: "Nothing to display",
            viewrecords: true,
            sortable: true,
            shrinkToFit: false,
            autowidth: true,
            caption: 'First Grid'
        });
    }

但如果我删除 postData 部分以具有以下内容,则网格显示,但当然没有数据。

  function loadFirstGrid() {
    $("#FirstGrid").jqGrid({
        url: "scripts/json_test.php?user=" + user,
        dataType: "json",
        mtype: "GET",
        colModel: [{...

有什么想法吗?

最佳答案

好吧,终于解决了这个问题并让它与这个一起工作

function loadFirstGrid() {
    $("#FirstGrid").jqGrid({
         url: "scripts/json_test.php?user=" + user,
        dataType: "json",
        mtype: "GET",
      colModel: [
            {name: "user_id", label:"User ID", width: 120},
            {name: "first_name", label:"Name", width: 400},
            {name: "case_title", label:"Case Title", width: 500},
            {name: "case_started", label:"Case Started", width: 200},
        ],
        emtyrecords: "Nothing to display",
        viewrecords: true,
        sortable: true,
        shrinkToFit: false,
        autowidth: true,
        caption: 'First Grid',
    });
}

为 jqGrid 获取正确格式的 JSON:

{"page":"1","total":"1","records":"1","rows":[{"user_id":"00082563","first_name":"Peter","case_title":"Male with STI (urethritis)","case_started":"2017-06-02 10:52:10"}]}

我使用了以下 PHP 脚本:

$page = '1';
$total_pages = '1';
$count = '1';

$data = (object) array('page' => $page, 'total' => $total_pages, 'records' =>$count, 'rows' => "");

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

$i=0; 
while ($row = mysql_fetch_assoc($result)) {
      $data->rows=array($row); 
      $i++;
}
print json_encode($data);
?>

(注意:我不关心页数、total_pages 和计数,因为我的网格将永远只有一个主记录和多个只有一个记录的子网格)。所以希望这对某人有帮助;文档或示例中没有太多内容描述如何使用 Free jqGrid 执行此操作;-(

关于php - 免费的 jqGrid - 使用 PHP 的 JSON 编码示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44450837/

相关文章:

javascript - 使用 Wistia API 上传 jQuery 文件

jquery - 如何获取jqGrid选定行单元格的值

javascript - jqgrid 服务器控件的每次回发都会调用 ashx 文件

javascript - Jquery - 使用一个动态值多次重用函数

php - composer.phar更新,然后解析错误: syntax error, unexpected $end

php - jQuery AJAX 在成功函数中引用 $(this)

json - 如何将自定义编码器添加到 akka http?

javascript - 如何在php中访问多维JSON数组

jquery - 使用变量访问json对象?

javascript - 当代码在后端生成并作为 jQgrid 中的 JSON 发送到 View 时,如何添加自定义格式化程序?