1

I'm having trouble using the jQgrid, when I initialize the jqgrid with json datatype it results to an error:

obj is undefined

ret = obj[expr];

When I initialize the jqgrid with local datatype, the error will not occur but the json data will not be loaded

index.html

<table id="products"></table>
<div id="pager"></div>

<script type="text/javascript">
$('document').ready(function(){
    jQuery("#products").jqGrid({
        url: 'product.php',
        editurl: 'product_update.php',
        datatype: "json",
        mtype: 'POST',
        colNames:['Product Name'],
        colModel:[
            {name:'product_name',index:'product_name', width:90}
        ],
        rowNum:-1,
        viewrecords: true, 
        rowList:[10,20,30],
        pager: '#pager',
        toolbar : [true,"top"],
        sortorder: "DESC",
        caption:"Products",
        width: 940,
        height: "100%"
    });
});
</script>

product.php (this is a mock data only)

    $arrayName = array();
    $arrayName['page'] = 1;
    $arrayName['total'] = 1;
    $arrayName['records'] = 3;
    $arrayName['rows'][0] = array(
        'product_name' => 'Product X'
        );
    $arrayName['rows'][1] = array(
        'product_name' => 'Product Y'
        );
    $arrayName['rows'][2] = array(
        'product_name' => 'Product Z'
        );

    echo json_encode($arrayName);

json output:

{"page":1,"total":1,"records":3,"rows":[{"product_name":"Product X"},{"product_name":"Product Y"},{"product_name":"Product Z"}]}

I'm hoping someone can help me here.

Thank you in advance :D

Community
  • 1
  • 1
jjz
  • 2,007
  • 3
  • 21
  • 30

1 Answers1

2

Default input format of JSON data - array of items with ids like

{"page":1,"total":1,"records":3,"rows":[
    {"id":"Product X", "cell":["Product X"]},
    {"id":"Product Y", "cell":["Product Y"]},
    {"id":"Product Z", "cell":["Product Z"]}
]}

(see here). To read the data which you currently posted you should add jsonReader: {repeatitems: false, id: "product_name"} option to jqGrid definition.

The most compact input you would have if you would use jsonReader: {cell: "", id: "0"}. In the case you should change the format of the JSON data to

{"page":1,"total":1,"records":3,"rows":[
    ["Product X","Product Y","Product Z"]}
]}

Additionally I would not recommend you to use rowNum:-1. The usage of some positive value which is large enough is better. For example rowNum: 10000. If you would add loadonce: true to the grid having rowNum:-1 you will that jqGrid will render grid in wrong way.

You should add gridview: true to improve it performance. The last remark: is the usage of index:'invdate' was typing error?

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you Oleg, I'm not aware that jsonReader should be used. I already used jqgrid before, but didn't encounter this problem and I based my code from my old project. What I did is to add `jsonReader: {repeatitems: false, id: "product_name"}` as you mentioned above. Thank you for this :D – jjz Apr 13 '12 at 15:47