Dynamically Building the Tree Structure

The initial tree structure that you define can be overridden at run time by data stored in database. You can activate operations in services to retrieve the relevant data and build the ValRep list dynamically.

To dynamically build the tree structure:

  1. Draw the tree widget on a form and define its properties.
  2. Define a service with operations that retrieve the necessary data from the database entities and define the ValRep list for the tree widget.
  3. Call the service and operations from the exec operation of the tree widget form.

    For example:

    operation exec 
      variables
        string BASICSETTINGS
      endvariables
    
      activate "TRESERV".BLDBASIC(BASICSETTINGS)
      $valrep(TREFLD.DUMMY) = BASICSETTINGS
      $ORIGPROP$ = $properties(TREFLD.DUMMY)
      clrmess
      edit
    end; exec
    
  4. Define a detail trigger for the tree widget field, and use getitem and $result to determine actions and values of events triggered by the widget.

    For examples, see Using $result in the Tree Widget) and Example: Detail Trigger for Tree Widget.

    Caution: Be careful about using $valrep or $fieldvalrep within the detail trigger to dynamically fill a tree widget field with data, especially if the tree widget has a List View. The normal behavior of these functions is to open each item in the List View, which activates the detail trigger for each open, causing a loop, unless you have provided ProcScript to intercept or ‘trap’ this situation. It is best to build the complete tree once, for instance in the exec operation, then use only the extended $fieldvalrep functionality to update it. For details, see utree (Tree).

  5. Define a local entry that ensures that the ValRep list is built correctly.

    For example, the following local entry is for a tree field:

    entry LP_GETSTRUCTURE
    params
       string WHICH: IN
       string STRUCTURE: OUT
       string NODETYPE: OUT
       string COLUMNDATA: OUT
    endparams
    
    getitem/id STRUCTURE, $valrep(TREFLD.DUMMY), WHICH
    if(STRUCTURE = "")
       getitem/id STRUCTURE, $fieldvalrep(TREFLD.DUMMY), WHICH
    endif
    ; get the type to which the selected item belongs
    getitem NODETYPE, STRUCTURE, 2
    ; get the data of the first column of the selected item
    getitem COLUMNDATA, STRUCTURE, 3
    done
    end ; entry LP_GETSTRUCTURE
    
  6. Define the valueChanged trigger, writing ProcScript to get the items from the ValRep list depending on the selected tree items.

    For example:

    trigger valueChanged ; of tree widget field
    variables
      string PARENT
      string ITEMVALREP
      string TEXT
    endvariables
    
    call LP_GETSTRUCTURE(LAST.DUMMY,$PARENTSTRUCTURE$,PARENT,TEXT)
    
    ITEMVALREP = ""
    selectcase PARENT
       case "ORGANISATION"
       ; if tree item type ORGANISATION is selected
          activate "TRESERV".BLDORG(TREFLD.DUMMY, ITEMVALREP)
          ; build parent structure for organisation
          $fieldvalrep(TREFLD.DUMMY) = "0=;%%ITEMVALREP"
          ; fieldvalrep contains parent structure
       case "DEPARTMENT"
          activate "TRESERV".BLDDEP(TREFLD.DUMMY,ITEMVALREP)
          $fieldvalrep(TREFLD.DUMMY) = "0=;%%ITEMVALREP"
    endselectcase
    end; trigger valueChanged
    
  7. Compile and test.

Related Topics