13

我正在尝试创建一个 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 代码有帮助,我也可以发布它。

4

9 回答 9

20

我让它工作了!

dataType字段应该是datatype。它区分大小写。

于 2008-11-03T21:40:51.970 回答
5

当您在grid.locale-en.js之前包含脚本jquery.jqGrid.min.js时,也会出现此问题。如果控制器的方法调用有任何问题,请检查此项。

于 2010-06-07T09:41:15.570 回答
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"]}
]}
于 2010-09-15T05:24:14.127 回答
1

我也让它工作:数据类型是正确的拼写 - 它在示例中以这种方式显示,但它与库中的其他所有内容不一致,因此很容易出错

我已经厌倦了追逐这个稀疏的文档,我真的觉得在 JavaScript 中使用它是正确和适当的 JSON 确实被简短地介绍了支持 XML。通过 JSON 将 Python 和 JavaScript 结合在一起是一个非常强大的组合,但是对于这个特定的库来说,它是一个持续不断的斗争。

任何有替代方案的人:

1> 正确支持 jQuery UI 主题(包括圆角!)(http://datatables.net对主题有更好的支持)

2> 允许调整列的大小(http://datatables.net不支持此功能)

3> 允许子网格(http://datatables.net让你在这里做任何你想做的事,通过一个事件)

请告诉我。我在界面的这一部分上花费的时间比在整个其余部分上的总和还要多,而且我一直花在寻找工作示例和“尝试一些东西”上,这变得越来越烦人。

小号

于 2010-04-14T16:37:21.430 回答
1

这可能是一个较旧的帖子,但我会发布我的成功只是为了帮助他人。

您的 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;
}
于 2011-02-18T05:12:58.550 回答
1

伙计们只是想在这方面帮助你。我得到了以下工作:

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'}],

问候,

于 2011-11-30T12:17:30.727 回答
1

就我而言,问题是由以下 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();
于 2012-01-03T22:59:34.233 回答
0

我不认为你的 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"]}
]}
于 2009-04-02T20:54:09.663 回答
0

我正在使用 WAMP 2.4,我对这个问题很着迷,我尝试了很多东西,比如安装以前版本的 PHP 和 5.2,我在 Windows XP 中尝试过,还有很多 jqGrid 选项。最后感谢 Oleg 和 Mariusz,我找到了唯一的一行:

$responce = new stdClass(); 

在使用 $responce 可以解决所有问题之前,现在我的网格可以正常工作了!

谢谢我的朋友。

于 2013-12-08T22:43:38.187 回答