When using jqGrid and JSON server responses, I seem to be having a problem getting my JSON mapping correct.
For example, my server response looks like this:
[
{ID: 'cmp1', Name: 'Name1', Address: 'Address1', Phone: 'Phone1', Agent: 'Agent1', last_trx: 'last_trx1'},
{ID: 'cmp2', Name: 'Name2', Address: 'Address2', Phone: 'Phone2', Agent: 'Agent2', last_trx: 'last_trx2'}
]
and my jqGrid settings look like this (local datatype and local dataset used for testing):
var grid = $('#grid_id');
grid.jqGrid({
datatype: 'local',
colNames: ['ID', 'Company Name', 'Location', 'Phone', 'Agent', 'Last Load'],
colModel: [
{name: 'ID', index: 'ID', jsonmap: 'ID', width: 75},
{name: 'Company Name', index: 'Name', jsonmap: 'Name', width: 150},
{name: 'Location', index: 'Address', jsonmap: 'Address', width: 150},
{name: 'Phone', index: 'Phone', jsonmap: 'Phone', width: 125, align: 'center'},
{name: 'Agent', index: 'Agent', jsonmap: 'Agent', width: 150},
{name: 'Last Load', index: 'last_trx', jsonmap: 'last_trx', width: 150}
],
loadonce: true,
shrinkToFit: false,
width: 600,
rowNum: 20,
rowList: [10, 20, 30, 40, 50, 100],
repeatitems: false,
jsonReader: { repeatitems: false, id: '0' },
pager: '#companies_pager',
caption: 'Company List',
data: [
{ID: 'cmp1', Name: 'Name1', Address: 'Address1', Phone: 'Phone1', Agent: 'Agent1', last_trx: 'last_trx1'},
{ID: 'cmp2', Name: 'Name2', Address: 'Address2', Phone: 'Phone2', Agent: 'Agent2', last_trx: 'last_trx2'}
]
});
ID, Phone, and Agent all show up (as their datasource names are exactly the same). However, Company Name, Location, and Last Load are all not being displayed. I thought that using jsonmap
along with jsonReader: { repeatitems: false}
allowed you to have different names for your JSON object than your colNames
object.
All help would be greatly appreciated.
UPDATE Sorry for the late update. This is how the code will look out of testing. _data.rows is an array of JSON objects.
var noRecords = $('<div>No results for the entered company name.</div>');
grid.jqGrid({
datatype: 'local',
colNames: ['ID', 'Company Name', 'Location', 'Phone', 'Agent', 'Last Load'],
colModel: [
{name: 'ID', jsonmap: 'ID', width: 75},
{name: 'Company Name', jsonmap: 'Name', width: 150},
{name: 'Location', jsonmap: 'Address', width: 150},
{name: 'Phone', jsonmap: 'Phone', width: 125, align: 'center'},
{name: 'Agent', jsonmap: 'Agent', width: 150},
{name: 'Last Load', jsonmap: 'last_trx', width: 150}
],
loadonce: true,
shrinkToFit: false,
width: 600,
rowNum: 20,
rowList: [10, 20, 30, 40, 50, 100],
repeatitems: false,
jsonReader: { repeatitems: false, id: '0' },
pager: '#companies_pager',
caption: 'Company List',
loadComplete: function() {
if(grid[0].p.reccount === 0) {
noRecords.show();
}
else {
noRecords.hide();
}
}
});
/* Get the list of companies based on the search criteria */
function getCompanies() {
var company = document.getElementById('company').value;
if((company != '') && (company != oldCompany)) {
oldCompany = company;
myAjax('get', {method: 'getCompanies', a: 'companies', data: company}, callbackGetCompanies);
}
}
/* Parse the server response */
function callbackGetCompanies(_data) {
if(_data && _data.message) {
if(_data.message == 'true') {
grid.jqGrid('clearGridData').jqGrid('setGridParam', {data: _data.rows, page: 1}).trigger('reloadGrid');
}
else {
dialog(_data.message);
}
}
else {
serverError();
}
}