Processing Events Passed from a Tree to an Application

To modify the FieldChange PeopleCode to load the direct children of the node into the HTML trees, use the following FieldChange PeopleCode to process the events passed from an HTML tree to an application. The code that processes the load children event loads the direct children of a node the first time the node is expanded by the user. Changes that you must make to the FieldChange PeopleCode are as follows.

  1. Globally change TREECTLEVENT to the name of the invisible field used to process the events.

  2. Set the tree-specific variables.

    The &SET_ID, &USERKEYVALUE, &TREE_NAME, &TREE_DT, and &BRANCH_NAME variables contain specific information about the tree. Set these values to the tree you want to open. In the example PeopleCode that follows, they are set like this:

    &SET_ID = PSTREEDEFN_VW.SETID; 
    &USERKEYVALUE = ""; 
    &TREE_NAME = PSTREEDEFN_VW.TREE_NAME; 
    &TREE_DT = PSTREEDEFN_VW.EFFDT; 
    &BRANCH_NAME = ""; 
     
  3. (Optional) Set the DISPLAY_OPTION field.

    The default for the DISPLAY_OPTION field is to display both the node name and the description. You can display just the node name or just the description. The values for this field are:

    Field Value Description

    N

    Display the name only.

    D

    Display the description only.

    B

    Display both the name and description.

  4. (Optional) Set the STYLECLASSNAME field for the root node.

    The STYLECLASSNAME field controls the style of the link associated with a node or leaf. The default for the STYLECLASSNAME is PSHYPERLINK. If PSHYPERLINK is not the style you want to use, change this field value to the style you want.

  5. Change the assignment of the output of every GenerateTree call to the field attached to the HTML area that will display the tree.

    In this example, the HTML area control is the DERIVED_HTML.HTMLAREA. You must specify the record and field name associated with the HTML area control on your page.

  6. Change the code that processes the select event to perform the action you want when the user selects a node or leaf.

    This section is marked as Process Select Event in the following code sample.

FieldChange PeopleCode Example

The following is the PostBuild PeopleCode example:

Component Rowset &TREECTL; 
/* process load children event */ 
If Left(TREECTLEVENT, 1) = "X" Then 
   &ROW = Value(Right(TREECTLEVENT, Len(TREECTLEVENT) - 1)) + 1; 
   &NODE_ROWSET = &TREECTL.GetRow(2).GetRowset(1); 
   &PARENT_REC = &NODE_ROWSET.GetRow(&ROW).GetRecord(1); 
   &PARENT_LEVEL = &PARENT_REC.GetField(Field.TREE_LEVEL_NUM).Value;
   &ROW = &ROW + 1; 
   &SET_ID = PSTREEDEFN_VW.SETID; 
   &USERKEYVALUE = ""; 
   &TREE_NAME = PSTREEDEFN_VW.TREE_NAME; 
   &TREE_DT = PSTREEDEFN_VW.EFFDT; 
   &BRANCH_NAME = ""; 
   &MYSESSION = %Session; 
   &SRC_TREE = &MYSESSION.GetTree(); 
   &RES = &SRC_TREE.OPEN(&SET_ID, &USERKEYVALUE, &TREE_NAME, 
&TREE_DT, &BRANCH_NAME, False); 
   /* Find the parent node and expand the tree one level below 
the parent.  Insert just the direct children of the parent node 
into the &TREECTL Rowset.  If any of the child nodes have 
children, set their PARENT_FLAG to 'X', so that their children 
are loaded on demand. */ 
   &PARENT_NODE = &SRC_TREE.FindNode(&PARENT_REC.
GetField(Field.TREE_NODE).Value, ""); 
   If &PARENT_NODE.HasChildren Then 
      &PARENT_NODE.Expand(2); 
      If &PARENT_NODE.HasChildLeaves Then 
         /* Load the child leaves into the &TREECTL Rowset. */ 
         &FIRST = True; 
         &CHILD_LEAF = &PARENT_NODE.FirstChildLeaf; 
         While &FIRST Or 
               &CHILD_LEAF.HasNextSib 
            If &FIRST Then 
               &FIRST = False; 
            Else 
               &CHILD_LEAF = &CHILD_LEAF.NextSib; 
            End-If; 
            If &CHILD_LEAF.Dynamic = True Then 
               &RANGE_FROM = ""; 
               &RANGE_TO = ""; 
               &DYNAMIC_RANGE = "Y"; 
            Else 
               &RANGE_FROM = &CHILD_LEAF.RangeFrom; 
               &RANGE_TO = &CHILD_LEAF.RangeTo; 
               &DYNAMIC_RANGE = "N"; 
            End-If; 
            &NODE_ROWSET.InsertRow(&ROW - 1); 
            &REC = &NODE_ROWSET.GetRow(&ROW).GetRecord(1); 
            /* Set the NODE values: 
1) LEAF_FLAG - If this is a leaf set to "Y".  Default value: N 
2) TREE_NODE - Node name. 
3) DESCR - Node description.  (optional) 
4) RANGE_FROM - Leaf's range from value. 
5) RANGE_TO - Leaf's range to value. 
6) DYNAMIC_FLAG - If this leaf has a dynamic range, set to "Y".  
Default value: N 
7) ACTIVE_FLAG - Set to "N" for the node or leaf not to be a link.
  Default value: Y 
8) DISPLAY_OPTION - Set to "N" to display the name only.  
Set to "D" to display the description only.  
Set to "B" to display both the name and the description.  
Only used for nodes.  Default value: B 
9) STYLECLASSNAME - Used to control the style of the link 
associated with the node or leaf.  Default value: PSHYPERLINK 
10) PARENT_FLAG - If this node is a parent and its direct 
children will be loaded now, set to "Y".  If this node is a 
parent and its direct children are to be loaded on demand, 
set to "X".  Default value: N 
11) TREE_LEVEL_NUM - Set to the node's level.  Default value: 1 
12) LEVEL_OFFSET - If a child node is to be displayed more than 
one level to the right of its parent, specify the number of 
additional levels.  Default value: 0 
*/ 
            &REC.GetField(Field.LEAF_FLAG).Value = "Y"; 
            &REC.GetField(Field.TREE_NODE).Value = ""; 
            &REC.GetField(Field.DESCR).Value = ""; 
            &REC.GetField(Field.RANGE_FROM).Value = &RANGE_FROM; 
            &REC.GetField(Field.RANGE_TO).Value = &RANGE_TO; 
            &REC.GetField(Field.DYNAMIC_FLAG).Value = 
&DYNAMIC_RANGE; 
            &REC.GetField(Field.ACTIVE_FLAG).Value = "Y"; 
            &REC.GetField(Field.DISPLAY_OPTION).Value = "B"; 
            &REC.GetField(Field.STYLECLASSNAME).Value = 
"PSHYPERLINK"; 
            /* Leaves never have children. */ 
            &REC.GetField(Field.PARENT_FLAG).Value = "N"; 
            &REC.GetField(Field.TREE_LEVEL_NUM).Value = 
&PARENT_LEVEL + 1; 
            &REC.GetField(Field.LEVEL_OFFSET).Value = 0; 
            &ROW = &ROW + 1; 
         End-While; 
      End-If; 
      If &PARENT_NODE.HasChildNodes Then 
         /* Load the child nodes into the &TREECTL Rowset. */ 
         &FIRST = True; 
         &CHILD_NODE = &PARENT_NODE.FirstChildNode; 
         While &FIRST Or 
               &CHILD_NODE.HasNextSib 
            If &FIRST Then 
               &FIRST = False; 
            Else 
               &CHILD_NODE = &CHILD_NODE.NextSib; 
            End-If; 
            If &CHILD_NODE.HasChildren Then 
               &PARENT_FLAG = "X"; 
            Else 
               &PARENT_FLAG = "N"; 
            End-If; 
            /* If the tree uses strict levels, set the 
&LEVEL_OFFSET to the number of levels that the child node is to 
the right of its parent minus 1. */ 
            If &SRC_TREE.LevelUse = "S" Then 
               &LEVEL_OFFSET = &CHILD_NODE.LevelNumber - 
&PARENT_NODE.LevelNumber - 1; 
            Else 
               &LEVEL_OFFSET = 0; 
            End-If; 
            &NODE_ROWSET.InsertRow(&ROW - 1); 
            &REC = &NODE_ROWSET.GetRow(&ROW).GetRecord(1); 
            &REC.GetField(Field.LEAF_FLAG).Value = "N"; 
            &REC.GetField(Field.TREE_NODE).Value = &CHILD_NODE.Name; 
            &REC.GetField(Field.DESCR).Value = 
&CHILD_NODE.Description; 
            &REC.GetField(Field.RANGE_FROM).Value = ""; 
            &REC.GetField(Field.RANGE_TO).Value = ""; 
            &REC.GetField(Field.DYNAMIC_FLAG).Value = "N"; 
            &REC.GetField(Field.ACTIVE_FLAG).Value = "Y"; 
            &REC.GetField(Field.DISPLAY_OPTION).Value = "B"; 
            &REC.GetField(Field.STYLECLASSNAME).Value = 
"PSHYPERLINK"; 
            &REC.GetField(Field.PARENT_FLAG).Value = &PARENT_FLAG; 
            &REC.GetField(Field.TREE_LEVEL_NUM).Value = 
&PARENT_LEVEL + 1; 
            &REC.GetField(Field.LEVEL_OFFSET).Value = &LEVEL_OFFSET; 
            &ROW = &ROW + 1; 
         End-While; 
      End-If; 
      /* change the parent's PARENT_FLAG from 'X' to 'Y' */ 
      &PARENT_REC.GetField(Field.PARENT_FLAG).Value = "Y"; 
      HTMLAREA = GenerateTree(&TREECTL, TREECTLEVENT); 
   End-If; 
   &SRC_TREE.Close(); 
Else 
   /* Process select event. */ 
 
   /* As an example, just display the selected node name or 
leaf range as a MessageBox. */ 
   If Left(TREECTLEVENT, 1) = "S" Then 
      &ROW = Value(Right(TREECTLEVENT, Len(TREECTLEVENT) - 1)) + 1;
      &NODE_ROWSET = &TREECTL.GetRow(2).GetRowset(1); 
      &REC = &NODE_ROWSET.GetRow(&ROW).GetRecord(1); 
      If &REC.GetField(Field.LEAF_FLAG).Value = "N" Then 
         MessageBox(0, "", 0, 0, "The selected node is %1.", 
&REC.GetField(Field.TREE_NODE).Value); 
      Else 
         If &REC.GetField(Field.DYNAMIC_FLAG).Value = "N" Then 
            If &REC.GetField(Field.RANGE_FROM).Value = 
&REC.GetField(Field.RANGE_TO).Value Then 
               &TEMP = "[" | &REC.GetField(Field.RANGE_FROM).
Value | "]"; 
            Else 
               &TEMP = "[" | &REC.GetField(Field.RANGE_FROM).
Value | " - " | &REC.GetField(Field.RANGE_TO).Value | "]"; 
            End-If; 
         Else 
            &TEMP = "[ ]"; 
         End-If; 
         MessageBox(0, "", 0, 0, "The selected leaf is %1.", &TEMP); 
      End-If; 
   Else 
      /* process all other events */ 
      HTMLAREA = GenerateTree(&TREECTL, TREECTLEVENT); 
   End-If; 
End-If; 
/* done processing the event, so clear it */ 
TREECTLEVENT = "";