sapdev logo background
sapdev logo sapdev logo
Comments

ABAP Web Dynpro tree structure, display HR org structure as example




Displaying data in a tree structure using web dynpro for ABAP is actually very straight forward. You can actually have one up and running within a few minutes using an existing example, which is already available within your SAP system.

The example I am referring to is WDT_TREE_TABLE_BY_KEY which displays a simple 3 level tree structure similar to the image below.


Depending on your requirements this may not do exactly what you want but using this as a starting point will save you a lot of time. So simply copy this application, I will copy it to ZWDT_TREE_TABLE_BY_KEY for the purpose of this example.

Although this example shows a tree structure of only 3 levels, it is not limited to just 3. It can actually be any number of levels. For example to turn this into an application that shows the full org structure of your organisation all you need to do is replace the code within the 'GENERATE_DATA' method of the MAIN view with the code below.

Note: Unless you enter an org unit this displys the whole org structure so may take a few seconds to display

method generate_data .

  data:
    lt_rows type if_main=>elements_table_data_source,
    row     like line of lt_rows,
    lvl1_index type string,
    lvl2_index type string,
    lvl3_index type string.

  data: objec_tab type STANDARD TABLE OF objec,
         wa_objtab like line of objec_tab,
         struc_tab type STANDARD TABLE OF STRUC,
         wa_struc like line of struc_tab,
         wa_struc2 like line of struc_tab.
  data: zorg_unit   type p0001-orgeh.
  data: ld_orgeh type p0001-orgeh.

  data: ld_sobid type hrp1001-sobid,
        ld_stext type HRP1000-stext,
        ld_short type HRP1000-short,
        ld_pernr type pernr-pernr.


  SELECT SINGLE pernr
        FROM pa0105
        INTO ld_pernr
        WHERE usrid = sy-uname
          AND usrty = '0001'
          AND begda LE sy-datum
          AND endda GE sy-datum.

  SELECT SINGLE orgeh
          FROM pa0001
          INTO ld_orgeh
          WHERE pernr = ld_pernr
            AND begda LE sy-datum
            AND endda GE sy-datum.

  do.
*   ld_orgeh = Current Org Unit, SOBID would then contain the parent Org Unit.
*     Get next level up in org structure
    select single  sobid
         from hrp1001
         into ld_sobid
        WHERE otype = 'O'
          AND objid = ld_orgeh
          AND plvar = '01'
          AND RSIGN = 'A'
          AND RELAT = '002'
          AND istat = '1'
          AND begda LE sy-datum
          AND endda GE sy-datum.
    if sy-subrc = 0.
      ld_orgeh = ld_sobid.
    else.
      exit.
    endif.
  enddo.

  zorg_unit = ld_orgeh.

*****************************************************************************
*** SET TOP LEVEL ORG CODE
*** otherwise will use code captured for current loged in user is setup

* zorg_unit = '55555555'.
*****************************************************************************

  CALL FUNCTION 'RH_PM_GET_STRUCTURE'
    EXPORTING
      plvar           = '01'   "p0000-plvar
      otype           = 'O'  "0001-otype
      objid           = zorg_unit
      begda           = sy-datum
      endda           = sy-datum
      status          = '1'
      wegid           = 'SBESX'   "ORGEH
      77aw_int        = ' '
    TABLES
      objec_tab       = objec_tab
      STRUC_TAB       = struc_tab
    EXCEPTIONS
      not_found       = 1
      ppway_not_found = 2
      others          = 3.
  DELETE struc_tab where otype ne 'O'.
  loop at struc_tab into wa_struc.
    READ TABLE objec_tab into wa_objtab WITH key objid = wa_struc-objid.

    row-row_key             = wa_struc-objid.  " lvl1_index.
    row-tree_column_text    = wa_struc-objid.  "lvl1_index.
    row-table_column_1_text = wa_objtab-stext. "'some text'.
    row-table_column_2_text = wa_objtab-short. "'some other text'.

*   Get parent node
    read TABLE  struc_tab into wa_struc2 with key seqnr = wa_struc-pup.
    READ TABLE objec_tab into wa_objtab WITH key objid = wa_struc2-objid.

    row-parent_row_key      = wa_struc2-objid. "''.

*   check if last entry in current tree structure path  i.e. node doesn't exist as
*   a parent of another node
    read TABLE  struc_tab TRANSPORTING NO FIELDS with key pup =  wa_struc-seqnr.
    if sy-subrc eq 0.
      row-is_leaf             = abap_false.
    else.
      row-is_leaf             = abap_true.  "last branch
    endif.
    insert row into table lt_rows.
  endloop.

node->bind_table( new_items =  lt_rows ).




*Original code (commented out)
*  do 3 times.
**   save the index so that we can use it in deeper levels
*    lvl1_index = sy-index.
*    condense lvl1_index.
*
**   create a row
*    row-row_key             = lvl1_index.
*    row-parent_row_key      = ''.
*    row-tree_column_text    = lvl1_index.
*    row-table_column_1_text = 'some text'.
*    row-table_column_2_text = 'some other text'.
*    row-is_leaf             = abap_false.
*    insert row into table lt_rows.
*
*    do 3 times.
**     save the index so that we can use it in deeper levels
*      lvl2_index = sy-index.
*      condense lvl2_index.
*
**     create a row
*      concatenate lvl1_index `.` lvl2_index into row-row_key.
*      row-parent_row_key      = lvl1_index.
*      concatenate lvl1_index `-` lvl2_index into row-tree_column_text.
*      row-table_column_1_text = 'some text'.
*      row-table_column_2_text = 'some other text'.
*      row-is_leaf             = abap_false.
*      insert row into table lt_rows.
*
*      do 3 times.
**       save the index so that we can use it in deeper levels
*        lvl3_index = sy-index.
*        condense lvl3_index.
*
**       create a row
*        concatenate lvl1_index `.` lvl2_index `.` lvl3_index into row-row_key.
*        concatenate lvl1_index `.` lvl2_index into row-parent_row_key.
*        concatenate lvl1_index`-` lvl2_index `-` lvl3_index into row-tree_column_text.
*        row-table_column_1_text = 'some text'.
*        row-table_column_2_text = 'some other text'.
*        row-is_leaf             = abap_true.
*        insert row into table lt_rows.
*      enddo.
*    enddo.
*  enddo.

*  node->bind_table( new_items =  lt_rows ).

endmethod.


When you now run the application it will didsplay the details of the org structure



Set default selected row
If you want to set a default selected row it is the same as a standard wdp table

i.e. after node->bind_table( new_items = lt_rows ). add the following code

node->set_lead_selection_index( index = 3 ).

but note index 3 may not be the be the third row in the displayed tree table as it depends where is sits in the tree structure.





comments powered by Disqus