Sei sulla pagina 1di 3

How to create a Hierarchical Grid Portlet

Adapted from an original document written by Kathryn Ellis

A hierarchical grid (also called an HGrid) is a grid portlet for use when you have an item that
uses a one to many relationship with another item. For instance a single project can have
many tasks or an object can have many sub-objects. When you view an HGrid portlet you
will see a list of the parent items. If a child item exists for that parent a plus sign will show
and allow you to expand and view the child items. The portlet content is all based on how
you create the NSQL query the portlet is based on. This document will show you how to
create an NSQL query, explain what each part is, and why it is needed.
Creating the Query
The best way to create this is to create the FROM statement first and then grab the items to
want from the FROM clause for the SELECT statement.
The FROM clause is created using a Union of all the items that you would like to display in
this portlet. For instance if you want to display Projects and Tasks you would create a Union
of the Projects table (or view) and the Tasks table. In each part of the Union you will need to
have at least the following two items
1. ID: unique id usually the system created ID
2. hg_has_children : value indicating that this item has child items. Created by a case
statement
Here is an example of a case statement:
case when ( select count(*)
from prteam pt
where pt.prprojectid = p.id
) > 0 then p.id else null
end hg_has_children

This statement is saying the following: If you have team members for this project id then
return the project id (p.id). If not then return null and place that in the hg_has_children
variable.
In the WHERE clause you will need to add the following NSQL parameter:
AND @WHERE:PARAM:USER_DEF:INTEGER:hg_row_id@ IS NULL
Or
AND pt.prprojectid = WHERE:PARAM:USER_DEF:INTEGER:hg_row_id@

The first line would be used for the part of the Union that is the highest Parent item. The
second line you would assign the hg_row_id to the ID of the parent item. Use only one of
these lines in each part of the UNION statement. These lines are used to associate the child
item with the parent item and to place it within the hierarchy.

As in all UNION statements each column must be of the same data type through each select
clause.
Complete the FROM clause by creating an alias for the entire UNION.
You can now create the SELECT statement by pulling only the columns that you want from
the select statements inside the UNION. You need to select the hg_has_children column or
you will not be able to expand the hierarchy. Unfortunately you will only be able to select
column names that are in the highest parent section of the UNION.
Once the query has been completed you can then create the portlet based on that query.
Here is an example of what it might look like:

I have provided an example of a query that shows a list of Projects and the Staff associated
with those Projects. Here is the query used:
SELECT
@SELECT:DIM:USER_DEF:IMPLIED:PROJECT:PROTEAM.GID:GridID@,
@SELECT:DIM_PROP:USER_DEF:IMPLIED:PROJECT:PROTEAM.GName:GridName@,
@SELECT:DIM_PROP:USER_DEF:IMPLIED:PROJECT:PROTEAM.hg_has_children:HG_HAS_CHILDREN@
FROM
(
SELECT
p.ID AS GID,
p.name AS GName,
CASE WHEN (SELECT COUNT(*)FROM prteam pt WHERE pt.prprojectid = p.id) > 0 THEN p.id
ELSE NULL
END hg_has_children
FROM srm_projects p
WHERE p.is_active = 1 AND p.type is not null
AND
@WHERE:PARAM:USER_DEF:INTEGER:hg_row_id@ IS NULL
GROUP BY p.ID,P.name
UNION
SELECT
pt.prid AS GID,
r.last_name || ', ' || r.first_name AS GName,
null hg_has_children
FROM
prteam pt,srm_resources r
WHERE pt.prresourceid = r.id
AND
pt.prprojectid = @WHERE:PARAM:USER_DEF:INTEGER:hg_row_id@

) proteam
WHERE
@FILTER@

Potrebbero piacerti anche