Simple ALV Tree / Hierarchy

Just copy and execute.

REPORT yalvhierarchy.

TYPE-POOLS: slis.

DATA : t_fieldcat         TYPE slis_t_fieldcat_alv,
       d_alv_keyinfo      TYPE slis_keyinfo_alv,
       d_layout           TYPE slis_layout_alv,
       d_f2code           LIKE sy-ucomm,
       d_print            TYPE slis_print_alv.

TABLES: scarr, spfli.

DATA: BEGIN OF t_scarr OCCURS 0.
        INCLUDE STRUCTURE scarr.
DATA:   expand, 
      END OF t_scarr.

DATA: BEGIN OF t_spfli OCCURS 0.
        INCLUDE STRUCTURE spfli.
DATA:   expand, 
      END OF t_spfli.

START-OF-SELECTION.
  PERFORM f_get_data.

END-OF-SELECTION.
  PERFORM f_write_data.

*&---------------------------------------------------------------------*
*&      Form  F_GET_DATA
*&---------------------------------------------------------------------*
FORM f_get_data .
  SELECT * FROM scarr INTO TABLE t_scarr.
  SELECT * FROM spfli INTO TABLE t_spfli.
ENDFORM.                    " F_GET_DATA
*&---------------------------------------------------------------------*
*&      Form  f_write_data
*&---------------------------------------------------------------------*
FORM f_write_data.
  PERFORM f_fieldcat.
  PERFORM f_layout.
  PERFORM f_alv_tree.
ENDFORM.                    "f_write_data
*&---------------------------------------------------------------------*
*&      Form  f_fieldcat
*&---------------------------------------------------------------------*
FORM f_fieldcat.
  PERFORM f_fieldcats USING 'T_SCARR' :
* FieldName       RefTable       RefFieldName    Text
*   Len   sum   CurrFieldname   spot   Hide    UOM      Input  Key.
'CARRID'   'SCARR' 'CARRID'   ''
  '' '' '' '' '' '' '' '',
'CARRNAME' 'SCARR' 'CARRNAME' ''
  '' '' '' '' '' '' '' '',
'CURRCODE' 'SCARR' 'CURRCODE' ''
  '' '' '' '' '' '' '' ''.

  PERFORM f_fieldcats USING 'T_SPFLI' :
* FieldName       RefTable       RefFieldName    Text
*   Len   sum   CurrFieldname   spot   Hide    UOM      Input  Key.
'CONNID'   'SPFLI' 'CONNID'   ''
  '' '' '' '' '' '' '' '',
'CITYFROM' 'SPFLI' 'CITYFROM' ''
  '' '' '' '' '' '' '' '',
'CITYTO'   'SPFLI' 'CITYTO'   ''
  '' '' '' '' '' '' '' '',
'DEPTIME'  'SPFLI' 'DEPTIME'  ''
  '' '' '' '' '' '' '' '',
'ARRTIME'  'SPFLI' 'ARRTIME'  ''
  '' '' '' '' '' '' '' ''.

ENDFORM.                    "f_fieldcat
*&---------------------------------------------------------------------*
*&      Form  f_fieldcats
*&---------------------------------------------------------------------*
FORM f_fieldcats USING fu_tabname     "Itab Name
                       fu_fname       "Field Itab
                       fu_reftb       "Reff.Table for fu_fname
                       fu_reffname    "Reff.Field for fu_reftb
                       fu_text        "Text will Display
                       fu_len         "Length Text
                       fu_sum         "Sum for currency
                       fu_curr        "Currency field at Itab
                       fu_spot        "X = Spot
                       fu_out         "X = Hide
                       fu_uom         "UoM Field at Itab
                       fu_input       "Inputing Field
                       fu_key.        "Field Key

  DATA : lt_fieldcat TYPE slis_fieldcat_alv.
  lt_fieldcat-tabname        = fu_tabname.
  lt_fieldcat-fieldname      = fu_fname.
  lt_fieldcat-ref_tabname    = fu_reftb.
  lt_fieldcat-ref_fieldname  = fu_reffname.
  lt_fieldcat-seltext_l      = lt_fieldcat-seltext_m =
  lt_fieldcat-seltext_s      = fu_text.
  lt_fieldcat-reptext_ddic   = fu_text.
  lt_fieldcat-outputlen      = fu_len.
  lt_fieldcat-do_sum         = fu_sum.
  lt_fieldcat-cfieldname     = fu_curr.
  lt_fieldcat-hotspot        = fu_spot.
  lt_fieldcat-no_out         = fu_out.
  lt_fieldcat-qfieldname     = fu_uom.
  lt_fieldcat-input          = fu_input.
  lt_fieldcat-key            = fu_key.

  APPEND lt_fieldcat TO t_fieldcat.
  CLEAR: lt_fieldcat.
ENDFORM.                    " F_FIELDCATS

*&---------------------------------------------------------------------*
*&      Form  f_sort
*&---------------------------------------------------------------------*
FORM f_layout.
  d_layout-f2code            = d_f2code.
  d_layout-expand_fieldname  = 'EXPAND'.
  d_layout-expand_all        = 'X'.
ENDFORM.                    "f_fieldcats
*&---------------------------------------------------------------------*
*&      Form  f_alv_tree
*&---------------------------------------------------------------------*
FORM f_alv_tree.
  d_alv_keyinfo-header01 = 'CARRID'.
  d_alv_keyinfo-item01   = 'CARRID'.
  d_alv_keyinfo-header02 = space.
  d_alv_keyinfo-item02   = 'CONNID'.

  d_print-no_print_listinfos = 'X'.

  CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      is_layout          = d_layout
      it_fieldcat        = t_fieldcat[]
      i_default          = 'X'
      i_save             = 'A'
      i_tabname_header   = 'T_SCARR'
      i_tabname_item     = 'T_SPFLI'
      is_keyinfo         = d_alv_keyinfo
      is_print           = d_print
    TABLES
      t_outtab_header    = t_scarr
      t_outtab_item      = t_spfli.

ENDFORM.                    "f_alv_tree
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s