Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <pre><code>function joinJSONFK (entities, fkProperties, fkLookupArrays) { function findValInAry(ary, idfield, value) { for (var i = 0; i &lt; ary.length; i++) { if (value == ary[i][idfield]) { return ary[i]; } } return null; }; function applyFKProperties(entity, fkProperties, fkLookupArrays) { for (var i = 0; i &lt; 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 &lt; entities.length; i++) { entities[i] = applyFKProperties(entities[i], fkProperties, fkLookupArrays); } return entities; } } </code></pre> <p>You would use it as follows:</p> <pre><code>userRoleData = joinJSONFK(result, ["SysRoleId", "BranchId"], [GlobalRoles, GlobalBranches]); </code></pre> <p>Where "result" is an array of JSON objects with the following format:</p> <pre><code>[{"entityHashCode":null,"BranchId":25,"SysRoleId":1,"SysUserId":1}, {"entityHashCode":null,"BranchId":25,"SysRoleId":2,"SysUserId":1}, {"entityHashCode":null,"BranchId":26,"SysRoleId":1,"SysUserId":1] </code></pre> <p>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. </p> <p>GlobalRoles would look something like this:</p> <pre><code>[{"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"}] </code></pre> <p>And GlobalBranches would look something like this:</p> <pre><code>[{"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"}] </code></pre> <p>After calling the function, the "userRoleData" will lok something like this:</p> <pre><code>[{"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"}}] </code></pre> <p>This way have a nicely structured collection of objects.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload