Skip to Content
author's profile photo
Former Member

problem with alv

i want to disply 4 different srtures in alv....

like if one field value equal to 1 we need to disply first structure if it equal to 2 2nd structure and so on...(with colonm headings)

example

1 a b c d

2 e f g h i j

3 k l m

4 m n o

1 a b c d

2 e f g h i j

3 k l m

4 m n o

if the field value eual to 1 struture cantaining field a, b,c,d should be displayed.

and soon....

Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

4 Answers

  • Best Answer
    author's profile photo
    Former Member
    Posted on Sep 28, 2007 at 05:23 PM

    Small but nice example -:)

    *&---------------------------------------------------------------------*
    *& Report  ZDUMMY_ATG_2
    *&
    *&---------------------------------------------------------------------*
    REPORT zdummy_atg_2.
    
    TYPES: BEGIN OF ty_header,
           carrid TYPE spfli-carrid,
           END OF ty_header.
    
    TYPES: BEGIN OF ty_scarr,
           carrid TYPE scarr-carrid,
           carrname TYPE scarr-carrname,
           END OF ty_scarr.
    
    TYPES: BEGIN OF ty_tree,
           carrid TYPE spfli-carrid,
           connid TYPE spfli-connid,
           countryfr TYPE spfli-countryfr,
           cityfrom TYPE spfli-cityfrom,
           airpfrom TYPE spfli-airpfrom,
           countryto TYPE spfli-countryto,
           cityto TYPE spfli-cityto,
           END OF ty_tree.
    
    DATA: t_spfli TYPE STANDARD TABLE OF spfli,
          w_spfli LIKE LINE OF t_spfli,
          lt_f4 TYPE lvc_t_f4 WITH HEADER LINE,
          return_tab TYPE STANDARD TABLE OF ddshretval WITH HEADER LINE,
          t_stable TYPE STANDARD TABLE OF lvc_s_stbl WITH HEADER LINE,
          t_header TYPE STANDARD TABLE OF ty_header,
          t_scarr TYPE STANDARD TABLE OF ty_scarr,
          t_tree TYPE STANDARD TABLE OF ty_tree WITH HEADER LINE.
    
    DATA: g_alv_tree TYPE REF TO cl_gui_alv_tree,
          g_custom_container TYPE REF TO cl_gui_custom_container,
          gs_fieldcat TYPE lvc_s_fcat,
          gt_fieldcat_tree TYPE lvc_t_fcat,
          l_hierarchy_header TYPE treev_hhdr,
          it_node_key TYPE lvc_t_nkey,
          w_index TYPE sy-tabix.
    
    DATA: ok_code LIKE sy-ucomm,
          gt_fieldcat TYPE lvc_t_fcat.
    
    FIELD-SYMBOLS: <fs_spfli> LIKE LINE OF t_spfli,
                   <fs_header> LIKE LINE OF t_header,
                   <fs_scarr> LIKE LINE OF t_scarr.
    
    *&---------------------------------------------------------------------*
    *&      START-OF-SELECTION                                             *
    *&---------------------------------------------------------------------*
    START-OF-SELECTION.
      PERFORM cargar_datos.
      PERFORM init_tree.
    
      CALL SCREEN 0100.
    
    *&---------------------------------------------------------------------*
    *&      Form  CARGAR_DATOS                                             *
    *&---------------------------------------------------------------------*
    FORM cargar_datos.
    
      SELECT mandt carrid connid countryfr cityfrom
             airpfrom countryto cityto airpto
             fltime deptime arrtime distance
             distid fltype period
      INTO TABLE t_spfli
      FROM spfli.
    
      SELECT carrid carrname
      INTO TABLE t_scarr
      FROM scarr
      FOR ALL ENTRIES IN t_spfli
      WHERE carrid EQ t_spfli-carrid.
    
      DELETE ADJACENT DUPLICATES FROM t_scarr
      COMPARING carrid.
    
      LOOP AT t_scarr ASSIGNING <fs_scarr>.
        APPEND INITIAL LINE TO t_header
        ASSIGNING <fs_header>.
        MOVE <fs_scarr> TO <fs_header>.
      ENDLOOP.
    
    ENDFORM.                    " CARGAR_DATOS
    
    *&---------------------------------------------------------------------*
    *&      Module  STATUS_0100  OUTPUT                                    *
    *&---------------------------------------------------------------------*
    MODULE status_0100 OUTPUT.
      SET PF-STATUS 'MAIN_STATUS'.
    *  SET TITLEBAR 'xxx'.
    
    ENDMODULE.                 " STATUS_0100  OUTPUT
    
    *&---------------------------------------------------------------------*
    *&      Module  USER_COMMAND_0100  INPUT                               *
    *&---------------------------------------------------------------------*
    MODULE user_command_0100 INPUT.
    
      ok_code = sy-ucomm.
    
      CASE ok_code.
        WHEN 'BACK' OR 'STOP' OR 'CANCEL'.
          SET SCREEN 0.
          LEAVE SCREEN.
      ENDCASE.
    
    ENDMODULE.                 " USER_COMMAND_0100  INPUT
    
    *---------------------------------------------------------------------*
    *       FORM init_tree                                                *
    *---------------------------------------------------------------------*
    FORM init_tree.
    
      DATA: l_tree_container_name(30) TYPE c.
    
      l_tree_container_name = 'CUSTOM_ALV'.
    
      CREATE OBJECT g_custom_container
        EXPORTING
          container_name              = l_tree_container_name
        EXCEPTIONS
          cntl_error                  = 1
          cntl_system_error           = 2
          create_error                = 3
          lifetime_error              = 4
          lifetime_dynpro_dynpro_link = 5.
    
      CREATE OBJECT g_alv_tree
        EXPORTING
          parent                      = g_custom_container
          node_selection_mode         = cl_gui_column_tree=>node_sel_mode_single
          item_selection              = ''
          no_html_header              = 'X'
          no_toolbar                  = ''
        EXCEPTIONS
          cntl_error                  = 1
          cntl_system_error           = 2
          create_error                = 3
          lifetime_error              = 4
          illegal_node_selection_mode = 5
          failed                      = 6
          illegal_column_name         = 7.
    
      PERFORM fill_catalog_tree.
      PERFORM build_hierarchy_header CHANGING l_hierarchy_header.
    
      CALL METHOD g_alv_tree->set_table_for_first_display
        EXPORTING
          is_hierarchy_header = l_hierarchy_header
        CHANGING
          it_outtab           = t_tree[]
          it_fieldcatalog     = gt_fieldcat_tree.
    
      PERFORM create_hierarchy.
    
    ENDFORM.                    "INIT_TREE
    
    *&---------------------------------------------------------------------*
    *&      Form  FILL_CATALOG_TREE                                        *
    *&---------------------------------------------------------------------*
    FORM fill_catalog_tree.
    
      DATA: gs_fieldcat TYPE lvc_s_fcat.
    
      CLEAR gs_fieldcat.
      gs_fieldcat-col_pos   = 1.
      gs_fieldcat-fieldname = 'CARRID'.
      gs_fieldcat-scrtext_s = 'Compañía'.
      gs_fieldcat-tabname   = 'T_SPFLI'.
      gs_fieldcat-edit      = 'X'.
      gs_fieldcat-f4availabl = 'X'.
      gs_fieldcat-outputlen = '8'.
      APPEND gs_fieldcat TO gt_fieldcat_tree.
    
      CLEAR gs_fieldcat.
      gs_fieldcat-col_pos   = 2.
      gs_fieldcat-fieldname = 'CONNID'.
      gs_fieldcat-scrtext_s = 'Conexión'.
      gs_fieldcat-tabname   = 'T_SPFLI'.
      gs_fieldcat-edit      = 'X'.
      gs_fieldcat-outputlen = '8'.
      APPEND gs_fieldcat TO gt_fieldcat_tree.
    
      CLEAR gs_fieldcat.
      gs_fieldcat-col_pos   = 3.
      gs_fieldcat-fieldname = 'COUNTRYFR'.
      gs_fieldcat-scrtext_s = 'País'.
      gs_fieldcat-tabname   = 'T_SPFLI'.
      gs_fieldcat-edit      = 'X'.
      gs_fieldcat-outputlen = '4'.
      APPEND gs_fieldcat TO gt_fieldcat_tree.
    
      CLEAR gs_fieldcat.
      gs_fieldcat-col_pos   = 4.
      gs_fieldcat-fieldname = 'CITYFROM'.
      gs_fieldcat-scrtext_s = 'Ciudad Salida'.
      gs_fieldcat-tabname   = 'T_SPFLI'.
      gs_fieldcat-edit      = space.
      gs_fieldcat-outputlen = '20'.
      APPEND gs_fieldcat TO gt_fieldcat_tree.
    
      CLEAR gs_fieldcat.
      gs_fieldcat-col_pos   = 5.
      gs_fieldcat-fieldname = 'AIRPFROM'.
      gs_fieldcat-scrtext_s = 'Arp. Salida'.
      gs_fieldcat-tabname   = 'T_SPFLI'.
      gs_fieldcat-edit      = 'X'.
      gs_fieldcat-outputlen = '10'.
      APPEND gs_fieldcat TO gt_fieldcat_tree.
    
      CLEAR gs_fieldcat.
      gs_fieldcat-col_pos   = 6.
      gs_fieldcat-fieldname = 'COUNTRYTO'.
      gs_fieldcat-scrtext_s = 'País'.
      gs_fieldcat-tabname   = 'T_SPFLI'.
      gs_fieldcat-edit      = 'X'.
      gs_fieldcat-outputlen = '4'.
      APPEND gs_fieldcat TO gt_fieldcat_tree.
    
      CLEAR gs_fieldcat.
      gs_fieldcat-col_pos   = 7.
      gs_fieldcat-fieldname = 'CITYTO'.
      gs_fieldcat-scrtext_s = 'Ciudad Llegada'.
      gs_fieldcat-tabname   = 'T_SPFLI'.
      gs_fieldcat-edit      = space.
      gs_fieldcat-outputlen = '20'.
      APPEND gs_fieldcat TO gt_fieldcat_tree.
    
    ENDFORM.                    " FILL_CATALOG_TREE
    
    *---------------------------------------------------------------------*
    *       FORM build_hierarchy_header                                   *
    *---------------------------------------------------------------------*
    FORM build_hierarchy_header CHANGING
                                   p_hierarchy_header TYPE treev_hhdr.
    
      CLEAR p_hierarchy_header.
      p_hierarchy_header-heading = 'Código'(300).
      p_hierarchy_header-width = 60.
      p_hierarchy_header-width_pix = ' '.
    
    ENDFORM.                    "BUILD_HIERARCHY_HEADER
    
    *---------------------------------------------------------------------*
    *       FORM create_hierarchy                                         *
    *---------------------------------------------------------------------*
    FORM create_hierarchy.
    
      DATA: l_root_key TYPE lvc_nkey,
            l_next_key TYPE lvc_nkey,
            l_last_key TYPE lvc_nkey,
            header TYPE string,
            w_menge_text(13) TYPE c.
    
      CLEAR l_root_key.
    
      LOOP AT t_header ASSIGNING <fs_header>.
        READ TABLE t_scarr WITH KEY carrid = <fs_header>-carrid
        ASSIGNING <fs_scarr>.
        IF sy-subrc EQ 0.
          header = <fs_scarr>-carrname.
        ENDIF.
        CLEAR l_root_key.
        CLEAR l_next_key.
        PERFORM add_node USING header l_root_key
                         CHANGING l_next_key.
        LOOP AT t_spfli ASSIGNING <fs_spfli>
        WHERE carrid EQ <fs_header>-carrid.
    
          MOVE-CORRESPONDING <fs_spfli> TO t_tree.
    
          PERFORM add_leaf USING t_tree l_next_key
                           CHANGING l_last_key.
        ENDLOOP.
      ENDLOOP.
    
      CALL METHOD g_alv_tree->frontend_update.
    
    ENDFORM.                    "CREATE_HIERARCHY
    
    *---------------------------------------------------------------------*
    *       FORM ADD_NODE                                                 *
    *---------------------------------------------------------------------*
    FORM add_node USING l_name l_root_key
                  CHANGING l_next_key.
    
      DATA: l_node_text TYPE lvc_value,
            ls_tree TYPE ty_tree.
    
      l_node_text =  l_name.
      CALL METHOD g_alv_tree->add_node
        EXPORTING
          i_relat_node_key = l_root_key
          i_relationship   = cl_gui_column_tree=>relat_last_child
          i_node_text      = l_node_text
          is_outtab_line   = ls_tree
        IMPORTING
          e_new_node_key   = l_next_key.
    
    ENDFORM.                    "ADD_NODE
    
    *---------------------------------------------------------------------*
    *       FORM ADD_LEAF                                                 *
    *---------------------------------------------------------------------*
    FORM add_leaf USING l_tree TYPE ty_tree
                        l_next_key
                  CHANGING l_last_key.
    
      CALL METHOD g_alv_tree->add_node
        EXPORTING
          i_relat_node_key = l_next_key
          i_relationship   = cl_gui_column_tree=>relat_last_child
          is_outtab_line   = t_tree
        IMPORTING
          e_new_node_key   = l_last_key.
    
    ENDFORM.                    "ADD_LEAF
    

    Greetings,

    Blag.

    Add comment
    10|10000 characters needed characters exceeded

  • author's profile photo
    Former Member
    Posted on Sep 28, 2007 at 02:19 PM

    consolidate the entire data that you want to display in the ALV into one ITAB...

    based on the conditions that you want to select the structure..

    eg. if one field value equal to 1 then append that record in ITAB

    same way for all the structures give the conditions..

    Pass this consolidated ITAB to ALV....

    Add comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 28, 2007 at 02:24 PM

    Build a structure with the fields of each structure, fill the internal table to display by moving only the required fields into the table when adding a record leaving the other ones initial.

    1 a b c d _ _ _ _ _ _ _ _

    2 _ _ _ _ e f g h i j _ _ _

    3 _ _ _ _ _ _ _ _ _ k l m

    CLEAR record
    CASE field1.
      WHEN '1'.
        MOVE-CORRESPONDING structure1 to record.
      WHEN '2'.
        MOVE-CORRESPONDING structure2 to record.
    ENDCASE.
    APPEND record TO itab.

    Regards

    Add comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 28, 2007 at 02:57 PM

    Have a look at the example programs BCALV_TREE_*. They show how to set up a heirarchical tree using an ALV grid.

    Add comment
    10|10000 characters needed characters exceeded