问题
I have a jqGrid that I bind to a JSON datasource (WCF web service). The WCF method returns a list of ids. Below is an example of the JSON that is returned. As you can see it is a relationship of a user to a branch to a role i.e. A user can have different roles in different branches.
[{"entityHashCode":null,"BranchId":25,"SysRoleId":1,"SysUserId":1},
{"entityHashCode":null,"BranchId":25,"SysRoleId":2,"SysUserId":1},
{"entityHashCode":null,"BranchId":26,"SysRoleId":1,"SysUserId":1]
Displaying this data in jqGrid is fine, but obviously I want to show the user the Branch and Role names rather than their ids. Unfortunately, changing the WCF so that it returns the data as JOINS is not an option because the WCF method may not change.
I also have access to 2 web service methods GetBranches and GetRoles both of which return arrays with the full details - I have to javascript arrays that I store this info into.
Is there a way that I can tell jqGrid to bind to my original array but somehow tell it to get the Branch and Role name from different datasources (the GetBranches and GetRoles arrays)?
回答1:
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.
回答2:
Why don't you just call GetBranches
and GetRoles
in your WCF method and build all the matches (relations), generating a new array (List of) object which contains BranchId, BranchName, SysRoleId, SysRoleName, SysUserId
?
You can return your new List of to jqGrid with just one call. It would be faster and easier to implement.
来源:https://stackoverflow.com/questions/6177198/show-foreign-key-text-in-jqgrid