带有 JSON 数据的 jqGrid 将表呈现为空
Posted
技术标签:
【中文标题】带有 JSON 数据的 jqGrid 将表呈现为空【英文标题】:jqGrid with JSON data renders table as empty 【发布时间】:2010-09-20 12:58:46 【问题描述】:我正在尝试创建一个 jqgrid,但表是空的。表格呈现,但数据不显示。
我从 php 调用中得到的数据是:
"page":"1",
"total":1,
"records":"10",
"rows":[
"id":"2:1","cell":["1","image","Chief Scout","Highest Award test","0"],
"id":"2:2","cell":["2","image","Link Badge","When you are invested as a Scout, you may be eligible to receive a Link Badge. (See page 45)","0"],
"id":"2:3","cell":["3","image","Pioneer Scout","Upon completion of requirements, the youth is invested as a Pioneer Scout","0"],
"id":"2:4","cell":["4","image","Voyageur Scout Award","Voyageur Scout Award is the right after Pioneer Scout.","0"],
"id":"2:5","cell":["5","image","Voyageur Citizenship","Learning about and caring for your community.","0"],
"id":"2:6","cell":["6","image","Fish and Wildlife","Demonstrate your knowledge and involvement in fish and wildlife management.","0"],
"id":"2:7","cell":["7","image","Photography","To recognize photography knowledge and skills","0"],
"id":"2:8","cell":["8","image","Recycling","Demonstrate your knowledge and involvement in Recycling","0"],
"id":"2:10","cell":["10","image","Voyageur Leadership ","Show leadership ability","0"],
"id":"2:11","cell":["11","image","World Conservation","World Conservation Badge","0"]
]
javascript 配置如下所示:
$("#"+tableId).jqGrid (
url:'getAwards.php?id='+classId,
dataType : 'json',
mtype:'POST',
colNames:['Id','Badge','Name','Description',''],
colModel : [
name:'awardId', width:30, sortable:true, align:'center',
name:'badge', width:40, sortable:false, align:'center',
name:'name', width:180, sortable:true, align:'left',
name:'description', width:380, sortable:true, align:'left',
name:'selected', width:0, sortable:false, align:'center'
],
sortname: "awardId",
sortorder: "asc",
pager: $('#'+tableId+'_pager'),
rowNum:15,
rowList:[15,30,50],
caption: 'Awards',
viewrecords:true,
imgpath: 'scripts/jqGrid/themes/green/images',
jsonReader :
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: true,
cell: "cell",
id: "id",
userdata: "userdata",
subgrid: root:"rows", repeatitems: true, cell:"cell"
,
width: 700,
height: 200
);
html 看起来像:
<table class="awardsList" id="awardsList2" class="scroll" name="awardsList" />
<div id="awardsList2_pager" class="scroll"></div>
我不确定是否需要定义 jsonReader,因为我已尝试保持默认设置。如果php代码有帮助,我也可以发布。
【问题讨论】:
我很好奇你是怎么让 classId 去这样的路径的?您的页面中的价值来自哪里? tableId和classId是函数参数。整个配置块是函数的主体:函数 makeAwardsTable (classId,tableId)。调用页面如下所示:我让它工作了!
dataType 字段应为datatype。区分大小写。
【讨论】:
这快把我逼疯了,不知道这需要多长时间,感谢这篇文章!【参考方案2】:当您在 grid.locale-en.js 之前包含脚本 jquery.jqGrid.min.js 时也会出现此问题。如果控制器的方法调用有任何问题,请检查此项。
【讨论】:
【参考方案3】:从 jqGrid 3.6 迁移到 jqGrid 3.7.2 时,我遇到了同样的问题。问题是我的 JSON 没有正确双引号(按照 JSON 规范的要求)。 jqGrid 3.6 容忍了我的无效 JSON,但 jqGrid 3.7 更严格。
参考这里:http://simonwillison.net/2006/Oct/11/json/
无效:
page:"1",
total:1,
records:"10",
rows:[
"id":"2:1","cell":["1","image","Chief Scout","Highest Award test","0"],
"id":"2:2","cell":["2","image","Link Badge","When you are invested as a Scout, you may be eligible to receive a Link Badge. (See page 45)","0"],
"id":"2:3","cell":["3","image","Pioneer Scout","Upon completion of requirements, the youth is invested as a Pioneer Scout","0"]
]
有效:
"page":"1",
"total":1,
"records":"10",
"rows":[
"id":"2:1","cell":["1","image","Chief Scout","Highest Award test","0"],
"id":"2:2","cell":["2","image","Link Badge","When you are invested as a Scout, you may be eligible to receive a Link Badge. (See page 45)","0"],
"id":"2:3","cell":["3","image","Pioneer Scout","Upon completion of requirements, the youth is invested as a Pioneer Scout","0"]
]
【讨论】:
另外,在 json 序列化(带有转义)中还有另一个错误,jqGrid 也默默地吞下了它,没有明显的错误。【参考方案4】:我也得到了它的工作:数据类型是正确的拼写 - 它在示例中以这种方式显示,但它与库中的 everything 不一致,因此很容易出错 p>
我已经厌倦了追逐这个稀疏的文档,我真的觉得在 JavaScript 中使用正确且合适的 JSON 确实得到了支持 XML 的简短报道。通过 JSON 将 Python 和 JavaScript 结合在一起是一个非常强大的组合,但对于这个特定的库来说,这是一个持续不断的斗争。
任何有替代方案的人:
1> 正确支持 jQuery UI 主题(包括圆角!)(http://datatables.net 对主题有更好的支持)
2> 允许调整列的大小(http://datatables.net 不支持此功能)
3> 允许子网格(http://datatables.net 让您在这里通过事件做任何您想做的事情)
请告诉我。我在界面的这一部分上花费的时间比在整个其余部分上的总和还要多,而且我一直花在寻找工作示例和“尝试一些东西”上,这越来越烦人了。
S
【讨论】:
jqGrid 是开源的,如果你觉得追逐文档很烦人,你可以在这里购买商业版本,trirand.net 或者你也可以为 jqGrid 文档做贡献。【参考方案5】:这可能是一个较旧的帖子,但我会发布我的成功只是为了帮助他人。
您的 JSON 需要采用以下格式:
"rows": [
"id": 1,
"cell": [
1,
"lname",
"fname",
"mi",
phone,
"cell1",
"cell2",
"address",
"email"
]
,
"id": 2,
"cell": [
2,
"lname",
"fname",
"mi",
phone,
"cell1",
"cell2",
"address",
"email"
]
]
我在 Zend 中编写了这个模型,所以如果您愿意,可以使用它。随心所欲地操作它。
public function fetchall ($sid, $sord)
$select = $this->getDbTable()->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
$select->setIntegrityCheck(false)
->join('Subdiv', 'Subdiv.SID = Contacts.SID', array("RepLastName" => "LastName",
"Subdivision" => "Subdivision",
"RepFirstName" => "FirstName"))
->order($sid . " ". $sord);
$resultset = $this->getDbTable()->fetchAll($select);
$i=0;
foreach ($resultset as $row)
$entry = new Application_Model_Contacts();
$entry->setId($row->id);
$entry->setLastName($row->LastName);
$entry->setFirstName1($row->FirstName1);
$entry->setFirstName2($row->FirstName2);
$entry->setHomePhone($row->HomePhone);
$entry->setCell1($row->Cell1);
$entry->setCell2($row->Cell2);
$entry->setAddress($row->Address);
$entry->setSubdivision($row->Subdivision);
$entry->setRepName($row->RepFirstName . " " . $row->RepLastName);
$entry->setEmail1($row->Email1);
$entry->setEmail2($row->Email2);
$response['rows'][$i]['id'] = $entry->getId(); //id
$response['rows'][$i]['cell'] = array (
$entry->getId(),
$entry->getLastName(),
$entry->getFirstName1(),
$entry->getFirstName2(),
$entry->getHomePhone(),
$entry->getCell1(),
$entry->getCell2(),
$entry->getAddress(),
$entry->getSubdivision(),
$entry->getRepName(),
$entry->getEmail1(),
$entry->getEmail2()
);
$i++;
return $response;
【讨论】:
感谢您提供可爱的 zend 模型!正是我需要的:D【参考方案6】:伙计们只是想在这方面为您提供帮助。我得到了以下工作:
JSON
var mydata1 = "page": "1", "total": 1, "records": "4","rows": [ "id": 1, "cell": ["1", "cell11", "values1" ] ,
"id": 2, "cell": ["2", "cell21", "values1"] ,
"id": 3, "cell": ["3", "cell21", "values1"] ,
"id": 4, "cell": ["4", "cell21", "values1"]
];
//在重要行下方标记。数据类型“jsonstring”对我有用,而不是“json”。
datatype: "jsonstring",
contentType: "application/json; charset=utf-8",
datastr: mydata1,
colNames: ['Id1', 'Name1', 'Values1'],
colModel: [
name: 'id1', index: 'id1', width: 55 ,
name: 'name1', index: 'name1', width: 80, align: 'right', sorttype: 'string' ,
name: 'values1', index: 'values1', width: 80, align: 'right', sorttype: 'string'],
问候,
【讨论】:
【参考方案7】:在我的情况下,问题是由以下 PHP 代码行引起的(取自 jqGrid 演示):
$responce->page = $page;
这里的问题是:我正在访问对象$responce
的属性页而没有先创建它。这导致 Apache 显示以下错误消息:
Strict Standards: Creating default object from empty value in /home/mariusz/public_html/rezerwacja/apps/frontend/modules/service/actions/actions.class.php on line 35
最后,错误消息用于在脚本中发送到 json 阅读器。
我通过创建空对象解决了这个问题:
$responce = new stdClass();
【讨论】:
谢谢你^_^你帮了我很多【参考方案8】:我不认为你的 ID 是正确的类型,我认为它应该是一个 int。
对于给定的 json,你真的不需要 jsonreader 设置。无论如何,您列出的都是默认值,而且您的 json 中没有子网格。
试试这个:
"page":"1",
"total":1,
"records":"10",
"rows":[
"id":1 ,"cell":["1","image","Chief Scout","Highest Award test","0"],
"id":2,"cell":["2","image","Link Badge","When you are invested as a Scout, you maybe eligible to receive a Link Badge. (See page 45)","0"],
"id":3,"cell":["3","image","Pioneer Scout","Upon completion of requirements, the youth is invested as a Pioneer Scout","0"],
"id":4,"cell":["4","image","Voyageur Scout Award","Voyageur Scout Award is the right after Pioneer Scout.","0"],
"id":5,"cell":["5","image","Voyageur Citizenship","Learning about and caring for your community.","0"],
"id":6,"cell":["6","image","Fish and Wildlife","Demonstrate your knowledge and involvement in fish and wildlife management.","0"],
"id":7,"cell":["7","image","Photography","To recognize photography knowledge and skills","0"],
"id":8,"cell":["8","image","Recycling","Demonstrate your knowledge and involvement in Recycling","0"],
"id":9,"cell":["10","image","Voyageur Leadership ","Show leadership ability","0"],
"id":10,"cell":["11","image","World Conservation","World Conservation Badge","0"]
]
【讨论】:
【参考方案9】:我正在使用 WAMP 2.4,我对这个问题很着迷,我尝试了很多东西,比如安装以前版本的 PHP 和 5.2,我在 Windows XP 中尝试过,还有很多 jqGrid 选项。 好吧,最后感谢 Oleg 和 Mariusz,我找到了唯一的一行:
$responce = new stdClass();
以前使用 $responce 可以解决所有问题,现在我的网格很好用!!!
谢谢我的朋友们。
【讨论】:
以上是关于带有 JSON 数据的 jqGrid 将表呈现为空的主要内容,如果未能解决你的问题,请参考以下文章