Wednesday, September 9, 2015

Master-Detail in Tree preview


Last month when I created a master-detail form, suddenly stared at the page and I was thinking about how can I expose total information of master-detail in this page so I found this idea "Tree preview" without change the structure of tables.

The first sample is showing to create the "Tree preview of Master-Detail" and the second about "Tree preview of Master-Detail-Detail".



 First sample:



Demo : Tree Preview

In this sample I use the "ROWNUM" that called "Virtual ID" VID and ID.3

SELECT    VALUE,
          VID,
          ID,
          NAME,
          (CASE
              WHEN FLAG_CTL = 1 THEN MAX (VID) OVER (PARTITION BY ID ORDER BY ID)
              ELSE NULL
           END) AS PARENT_VID
     FROM (SELECT VALUE,
                  ROWNUM AS VID,
                  ID,
                  NAME,
                  FLAG_CTL
             FROM (SELECT EMPNO || '-' || DEPTNO AS VALUE,
                          DEPTNO AS ID,
                          ENAME AS NAME,
                          1 AS FLAG_CTL
                   FROM EMP
                   UNION ALL
                   SELECT 0 || '-' || DEPTNO AS VALUE,
                          DEPTNO AS ID,
                          DNAME AS NAME,
                          NULL AS FLAG_CTL
                   FROM DEPT))

Below code is in APEX:

select case when connect_by_isleaf = 1 then 0
            when level = 1             then 1
            else                           -1
       end as status,
       level,
       "NAME" as title,
       null as icon,
       value,
       "NAME" as tooltip,  
       null as link
from "#OWNER#"."VIEW_TREE_MASTER_DETAIL"
start with "PARENT_VID" is null
connect by prior "VID" = "PARENT_VID"
order siblings by VID

Second sample:


I use two "ID" fields. For each "Detail" you must add a new "ID". For example "ID2"
It needs a create new table "COURSE".(this table uses only for sample)

CREATE TABLE COURSE
(
  COURSENO  NUMBER(6),
  NAME      VARCHAR2(100 BYTE),
  DURATION  NUMBER(3),
  EMPNO     NUMBER(4)
)

Insert into table:
COURSENO
NAME
DURATION
EMPNO
100
Oracle APEX
80
7654
120
PL/SQL
40
7654
110
SQL
40
7654
130
Oracle
100
7900

This is the query of master-detail-detail

SELECT    VALUE,
          VID,
          ID,
          ID2,
          NAME,
          (CASE
              WHEN FLAG_CTL = 1 THEN MAX (VID) OVER (PARTITION BY ID ORDER BY ID)
              WHEN FLAG_CTL = 2 THEN MAX (VID) OVER (PARTITION BY ID2 ORDER BY ID)
              ELSE NULL
           END)AS PARENT_VID
     FROM (SELECT VALUE,
                  ROWNUM AS VID,
                  ID,
                  ID2,
                  NAME,
                  FLAG_CTL
             FROM (SELECT COURSENO || '-' || EMPNO AS VALUE,
                          EMPNO AS ID,
                          EMPNO AS ID2,
                          NAME AS NAME,
                          2 AS FLAG_CTL
                   FROM COURSE
                   UNION ALL
                   SELECT EMPNO || '-' || DEPTNO AS VALUE,
                          DEPTNO AS ID,
                          EMPNO AS ID2,
                          ENAME AS NAME,
                          1 AS FLAG_CTL
                   FROM EMP
                   UNION ALL
                   SELECT 0 || '-' || DEPTNO AS VALUE,
                          DEPTNO AS ID,
                          0 AS ID2,
                          DNAME AS NAME,
                          NULL AS FLAG_CTL
                   FROM DEPT))

Below code is in APEX:

select case when connect_by_isleaf = 1 then 0
            when level = 1             then 1
            else                           -1
       end as status,
       level,
       "NAME" as title,
       null as icon,
       value,
       "NAME" as tooltip,  
       null as link
from "#OWNER#"."VIEW_TREE_MASTER_DETAIL_DETAIL"
start with "PARENT_VID" is null
connect by prior "VID" = "PARENT_VID"
order siblings by VID


Hopefully it might be useful for you one day.
All suggestions are welcome!

No comments :

Post a Comment