在jqGrid中显示外键文本 [英] Show foreign key text in jqGrid

查看:59
本文介绍了在jqGrid中显示外键文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到JSON数据源(WCF Web服务)的jqGrid. WCF方法返回ID列表.以下是返回的JSON的示例.如您所见,这是用户与角色的分支之间的关系,即用户在不同的分支中可以具有不同的角色.

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]

在jqGrid中显示此数据很好,但显然我想向用户显示分支和角色名称,而不是其ID. 不幸的是,由于WCF方法可能不会更改,因此不能更改WCF以使其以JOINS的形式返回数据.

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.

我还可以访问2个Web服务方法GetBranches和GetRoles,这两个方法均返回具有完整详细信息的数组-我必须将这些信息存储到的javascript数组中.

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.

有没有一种方法可以告诉jqGrid绑定到我的原始数组,但又以某种方式告诉它从不同的数据源(GetBranches和GetRoles数组)中获取Branch和Role名称?

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)?

推荐答案

我与LeftyX进行的对话似乎并不是在jqGrid中执行此操作的本机方法,因此我创建了一个方法用于在数组中的对象之间执行"JOIN".功能如下:

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]); 

其中结果"是具有以下格式的JSON对象数组:

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]

并且["SysRoleId","BranchId"]是需要"JOINED"的外键的数组,而[GlobalRoles,GlobalBranches]是包含外键的查找"数据的数组.

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看起来像这样:

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"}]

GlobalBranches看起来像这样:

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"}]

调用函数后,"userRoleData"将显示如下内容:

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.

这篇关于在jqGrid中显示外键文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆