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.
Related Articles
BIND_STRUCTURE method of interface IF_WD_CONTEXT_NODE to assign structure to context
BIND_TABLE method of interface IF_WD_CONTEXT_NODE
Get the name of the button the user has clicked on within the SAP web dynpro action ABAP code
Create field Refering to datatype of webDynpro context element
WebDynpro context for creation of screen field including standard search help
Disable SAP User Personalisation in Web Dynpro app using WDDISABLEUSERPERSONALIZATION and WDENABLEUIELEMENTSHIDE
GET_ATTRIBUTE method of interface IF_WD_CONTEXT_NODE to retrieve value of context element
GET_SELECTED_ELEMENTS to get selected row of ABAP web dynpro table when not using lead selection
GET_STATIC_ATTRIBUTE_TABLE method to return all rows of a context table node
IF_WD_CONTEXT_NODE interface methods withn ABAP Web Dynpro to enable users to view and change data
ABAP code to manually trigger ABAP web dynpro plug
Popuate SAP WebDynpro field
Freely Programmed search help for your web dynpro application (custom value help/OVS)
Change the stylesheet theme used by your web dynpro and portal apps (i.e. SAP_TRADESHOW, SAP_CORBU)
Change the stylesheet theme used by your web dynpro and portal apps (i.e. SAP_TRADESHOW, SAP_CORBU)
SET_ATTRIBUTE method of interface IF_WD_CONTEXT_NODE to assign value to individuale context attribute
Clear context attribute using SET_ATTRIBUTE_NULL method of interface IF_WD_CONTEXT_NODE
Add Dropdown by index UI Element to table field within abap web dynpro
Add freely Programmed search help to a web dynpro application field
Retrieve SAP webDynpro field value entered by user
ABAP Web dynpro ALV report table
Hide fields of a web dynpro ALV table
Display web dynpro ALV report select options to resict output results
Assign value to ABAP web dynpro context via table, structure or individual attribute
Get selected ABAP Web dynpro dropdown byindex value (dropdownbyindex UI element)
ABAP web dynpro Dropdown by index UI Element allows a field to display a drop down list of values
ABAP web dynpro Dropdown default value, allow you to set current value of dropdown UI element
Change SAP web dynpro logon screen
Read ABAP Web dynpro table context on action / button click
ABAP Web Dynpro tables to allow users to view and change data
Get selected row of ABAP web dynpro table
Get all selected rows of table within your web dynpro for ABAP applications using GET_SELECTED_ELEMENTS
Get selected row of ABAP web dynpro table
SAP ABAP Web Dynpro UI elements
View Container UI Element to display a web dynpro view within a wdp view
ALV ABAP Web Dynpro application
ABAP Web dynpro button click / Action
Call URL from SAP Webdynpro for ABAP ACTION
Call URL from SAP Webdynpro for ABAP ACTION
Display popup screen within ABAP Web dynpro
ABAP Web dynpro populate table on action / button click
ABAP code to Print abap web dynpro table
Adding URL parameters to you ABAP webdynpro to change thinks like the CSS