I thing with the conversations I've had with LeftyX there doesn't seem to be a native way to do this in jqGrid, so I have created a method for doing a "JOIN" between objects in an array. The function is as follows:
function joinJSONFK (entities, fkProperties, fkLookupArrays) {
function findValInAry(ary, idfield, value) {
for (var i = 0; i < ary.length; i++) {
if (value == ary[i][idfield]) {
return ary[i];
}
}
return null;
};
function applyFKProperties(entity, fkProperties, fkLookupArrays) {
for (var i = 0; i < fkProperties.length; i++) {
entity[fkProperties[i] + "Source"] = findValInAry(fkLookupArrays[i], fkProperties[i], entity[fkProperties[i]]);
}
return entity;
}
var entityary = [];
if (!entities instanceof Array) {
entities = applyFKProperties(entities);
return entities[0];
}
else {
for (var i = 0; i < entities.length; i++) {
entities[i] = applyFKProperties(entities[i], fkProperties, fkLookupArrays);
}
return entities;
}
}
You would use it as follows:
userRoleData = joinJSONFK(result, ["SysRoleId", "BranchId"], [GlobalRoles, GlobalBranches]);
Where "result" is an array of JSON objects with the following format:
[{"entityHashCode":null,"BranchId":25,"SysRoleId":1,"SysUserId":1},
{"entityHashCode":null,"BranchId":25,"SysRoleId":2,"SysUserId":1},
{"entityHashCode":null,"BranchId":26,"SysRoleId":1,"SysUserId":1]
And ["SysRoleId", "BranchId"] is an array of the foreign keys that need to be "JOINED" and [GlobalRoles, GlobalBranches] is an array containing the "lookup" data for the foreign keys.
GlobalRoles would look something like this:
[{"Name":"Admin","SysRoleId":1,"Description":"Some description"},
{"Name":"Role 2","SysRoleId":2,"Description":"Some description"},
{"Name":"A new role","SysRoleId":3,"Description":"Some description"},
{"Name":"Another Role","SysRoleId":4,"Description":"Some description"}]
And GlobalBranches would look something like this:
[{"BranchName":"Branch 25","BranchId":25,"Description":"describe the branch"},
{"BranchName":"Branch 26","BranchId":26,"Description":"describe the branch"},
{"BranchName":"Branch 27","BranchId":27,"Description":"describe the branch"}]
After calling the function, the "userRoleData" will lok something like this:
[{"entityHashCode":null,"BranchId":25,"SysRoleId":1,"SysUserId":1, "SysRoleIdSource":{"Name":"Admin","SysRoleId":1,"Description":"Some description"}, "BranchIdSource":{"BranchName":"Branch 25","BranchId":25,"Description":"describe the branch"}},
{"entityHashCode":null,"BranchId":25,"SysRoleId":2,"SysUserId":1}, "SysRoleIdSource":{"Name":"Role 2","SysRoleId":2,"Description":"Some description"}, "BranchIdSource":{"BranchName":"Branch 25","BranchId":25,"Description":"describe the branch"}},
{"entityHashCode":null,"BranchId":26,"SysRoleId":1,"SysUserId":1, "SysRoleIdSource":{"Name":"Admin","SysRoleId":1,"Description":"Some description"}, "BranchIdSource":{"BranchName":"Branch 26","BranchId":26,"Description":"describe the branch"}}]
This way have a nicely structured collection of objects.