我有一个jqGrid,我绑定到JSON数据源(WCF Web服务)。 WCF方法返回id列表。下面是返回的JSON示例。正如您所看到的,它是用户与分支到角色的关系,即用户可以在不同的分支中拥有不同的角色。
[{"entityHashCode":null,"BranchId":25,"SysRoleId":1,"SysUserId":1},
{"entityHashCode":null,"BranchId":25,"SysRoleId":2,"SysUserId":1},
{"entityHashCode":null,"BranchId":26,"SysRoleId":1,"SysUserId":1]
在jqGrid中显示此数据很好,但显然我想向用户显示分支和角色名称而不是它们的ID。 不幸的是,更改WCF以使其作为JOINS返回数据不是一个选项,因为WCF方法可能不会更改。
我还可以访问2个Web服务方法GetBranches和GetRoles,这两个方法都返回了包含完整详细信息的数组 - 我需要将这些信息存储到的javascript数组中。
有没有办法可以告诉jqGrid绑定到我的原始数组,但不知何故告诉它从不同的数据源(GetBranches和GetRoles数组)获取Branch和Role名称?
答案 0 :(得分:0)
为什么不在 WCF 方法中调用GetBranches
和GetRoles
并构建所有匹配项(关系),生成新的数组(List of)对象其中包含BranchId, BranchName, SysRoleId, SysRoleName, SysUserId
?
您只需拨打一个电话即可将新列表返回 jqGrid 。实施起来会更快更容易。
答案 1 :(得分:0)
我认为我与LeftyX的对话似乎没有在jqGrid中执行此操作的原生方式,因此我创建了一种在数组中的对象之间执行“JOIN”的方法。功能如下:
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;
}
}
您可以按如下方式使用它:
userRoleData = joinJSONFK(result, ["SysRoleId", "BranchId"], [GlobalRoles, GlobalBranches]);
其中“result”是具有以下格式的JSON对象数组:
[{"entityHashCode":null,"BranchId":25,"SysRoleId":1,"SysUserId":1},
{"entityHashCode":null,"BranchId":25,"SysRoleId":2,"SysUserId":1},
{"entityHashCode":null,"BranchId":26,"SysRoleId":1,"SysUserId":1]
[“SysRoleId”,“BranchId”]是需要“JOINED”的外键数组,[GlobalRoles,GlobalBranches]是包含外键“查找”数据的数组。
GlobalRoles看起来像这样:
[{"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"}]
GlobalBranches看起来像这样:
[{"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"}]
调用该函数后,“userRoleData”将会出现如下情况:
[{"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"}}]
这种方式有一个结构合理的对象集合。