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 😀
Default input format of JSON data – array of items with ids like
(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 toAdditionally I would not recommend you to use
rowNum:-1. The usage of some positive value which is large enough is better. For examplerowNum: 10000. If you would addloadonce: trueto the grid havingrowNum:-1you 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?