gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:10:27 24 4
gpt4 key购买 nike

免费的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com