Skip to Content
author's profile photo Former Member
Former Member

To create an alv, using more than one table

Hello,

I'm new to abap. I want to create an alv. I have to use two internal table, first ( say ibkpf ) as a header and display corresponding detail from another internal table ( say ibseg ).

I intend to use

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' in my program.

Can this be done and if yes how?

Thanks.

Ushma

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Apr 09, 2008 at 12:51 PM

    Hi,

    We can use more then one table.

    using for all entries or join. we can display the output through alv.

    U should move all data to one internal table after that u can display Reuse_ALV_Grid_Display

    Regards,

    Brown.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 09, 2008 at 01:07 PM

    Hi ,

    Check the following code...

    *REPORT zdemo_alvgrid .

    TABLES: EKKO.

    TYPE-POOLS: SLIS. "ALV Declarations

    *Data Declaration

    *----


    TYPES: BEGIN OF T_EKKO,

    EBELN TYPE EKPO-EBELN,

    EBELP TYPE EKPO-EBELP,

    STATU TYPE EKPO-STATU,

    AEDAT TYPE EKPO-AEDAT,

    MATNR TYPE EKPO-MATNR,

    MENGE TYPE EKPO-MENGE,

    MEINS TYPE EKPO-MEINS,

    NETPR TYPE EKPO-NETPR,

    PEINH TYPE EKPO-PEINH,

    LINE_COLOR(4) TYPE C, "Used to store row color attributes

    END OF T_EKKO.

    DATA: IT_EKKO TYPE STANDARD TABLE OF T_EKKO INITIAL SIZE 0,

    WA_EKKO TYPE T_EKKO.

    *ALV data declarations

    DATA: FIELDCATALOG TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,

    GD_TAB_GROUP TYPE SLIS_T_SP_GROUP_ALV,

    GD_LAYOUT TYPE SLIS_LAYOUT_ALV,

    GD_REPID LIKE SY-REPID.

    ************************************************************************

    *Start-of-selection.

    START-OF-SELECTION.

    PERFORM DATA_RETRIEVAL.

    PERFORM BUILD_FIELDCATALOG.

    PERFORM BUILD_LAYOUT.

    PERFORM DISPLAY_ALV_REPORT.

    &----


    *& Form BUILD_FIELDCATALOG

    &----


    • Build Fieldcatalog for ALV Report

    ----


    FORM BUILD_FIELDCATALOG.

    • There are a number of ways to create a fieldcat.

    • For the purpose of this example i will build the fieldcatalog manualy

    • by populating the internal table fields individually and then

    • appending the rows. This method can be the most time consuming but can

    • also allow you more control of the final product.

    • Beware though, you need to ensure that all fields required are

    • populated. When using some of functionality available via ALV, such as

    • total. You may need to provide more information than if you were

    • simply displaying the result

    • I.e. Field type may be required in-order for

    • the 'TOTAL' function to work.

    FIELDCATALOG-FIELDNAME = 'EBELN'.

    FIELDCATALOG-SELTEXT_M = 'Purchase Order'.

    FIELDCATALOG-COL_POS = 0.

    FIELDCATALOG-OUTPUTLEN = 10.

    FIELDCATALOG-EMPHASIZE = 'X'.

    FIELDCATALOG-KEY = 'X'.

    • fieldcatalog-do_sum = 'X'.

    • fieldcatalog-no_zero = 'X'.

    APPEND FIELDCATALOG TO FIELDCATALOG.

    CLEAR FIELDCATALOG.

    FIELDCATALOG-FIELDNAME = 'EBELP'.

    FIELDCATALOG-SELTEXT_M = 'PO Item'.

    FIELDCATALOG-COL_POS = 1.

    APPEND FIELDCATALOG TO FIELDCATALOG.

    CLEAR FIELDCATALOG.

    FIELDCATALOG-FIELDNAME = 'STATU'.

    FIELDCATALOG-SELTEXT_M = 'Status'.

    FIELDCATALOG-COL_POS = 2.

    APPEND FIELDCATALOG TO FIELDCATALOG.

    CLEAR FIELDCATALOG.

    FIELDCATALOG-FIELDNAME = 'AEDAT'.

    FIELDCATALOG-SELTEXT_M = 'Item change date'.

    FIELDCATALOG-COL_POS = 3.

    APPEND FIELDCATALOG TO FIELDCATALOG.

    CLEAR FIELDCATALOG.

    FIELDCATALOG-FIELDNAME = 'MATNR'.

    FIELDCATALOG-SELTEXT_M = 'Material Number'.

    FIELDCATALOG-COL_POS = 4.

    APPEND FIELDCATALOG TO FIELDCATALOG.

    CLEAR FIELDCATALOG.

    FIELDCATALOG-FIELDNAME = 'MENGE'.

    FIELDCATALOG-SELTEXT_M = 'PO quantity'.

    FIELDCATALOG-COL_POS = 5.

    APPEND FIELDCATALOG TO FIELDCATALOG.

    CLEAR FIELDCATALOG.

    FIELDCATALOG-FIELDNAME = 'MEINS'.

    FIELDCATALOG-SELTEXT_M = 'Order Unit'.

    FIELDCATALOG-COL_POS = 6.

    APPEND FIELDCATALOG TO FIELDCATALOG.

    CLEAR FIELDCATALOG.

    FIELDCATALOG-FIELDNAME = 'NETPR'.

    FIELDCATALOG-SELTEXT_M = 'Net Price'.

    FIELDCATALOG-COL_POS = 7.

    FIELDCATALOG-OUTPUTLEN = 15.

    FIELDCATALOG-DATATYPE = 'CURR'.

    APPEND FIELDCATALOG TO FIELDCATALOG.

    CLEAR FIELDCATALOG.

    FIELDCATALOG-FIELDNAME = 'PEINH'.

    FIELDCATALOG-SELTEXT_M = 'Price Unit'.

    FIELDCATALOG-COL_POS = 8.

    APPEND FIELDCATALOG TO FIELDCATALOG.

    CLEAR FIELDCATALOG.

    ENDFORM. " BUILD_FIELDCATALOG

    &----


    *& Form BUILD_LAYOUT

    &----


    • Build layout for ALV grid report

    ----


    FORM BUILD_LAYOUT.

    GD_LAYOUT-NO_INPUT = 'X'.

    GD_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.

    GD_LAYOUT-TOTALS_TEXT = 'Totals'(201).

    • Set layout field for row attributes(i.e. color)

    GD_LAYOUT-INFO_FIELDNAME = 'LINE_COLOR'.

    • gd_layout-totals_only = 'X'.

    • gd_layout-f2code = 'DISP'. "Sets fcode for when double

    • "click(press f2)

    • gd_layout-zebra = 'X'.

    • gd_layout-group_change_edit = 'X'.

    • gd_layout-header_text = 'helllllo'.

    ENDFORM. " BUILD_LAYOUT

    &----


    *& Form DISPLAY_ALV_REPORT

    &----


    • Display report using ALV grid

    ----


    FORM DISPLAY_ALV_REPORT.

    GD_REPID = SY-REPID.

    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

    EXPORTING

    I_CALLBACK_PROGRAM = GD_REPID

    • i_callback_top_of_page = 'TOP-OF-PAGE' "see FORM

    • i_callback_user_command = 'USER_COMMAND'

    • i_grid_title = outtext

    IS_LAYOUT = GD_LAYOUT

    IT_FIELDCAT = FIELDCATALOG[]

    • it_special_groups = gd_tabgroup

    • IT_EVENTS = GT_XEVENTS

    I_SAVE = 'X'

    • is_variant = z_template

    TABLES

    T_OUTTAB = IT_EKKO

    EXCEPTIONS

    PROGRAM_ERROR = 1

    OTHERS = 2.

    IF SY-SUBRC <> 0.

    • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

    • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

    ENDIF.

    ENDFORM. " DISPLAY_ALV_REPORT

    &----


    *& Form DATA_RETRIEVAL

    &----


    • Retrieve data form EKPO table and populate itab it_ekko

    ----


    FORM DATA_RETRIEVAL.

    DATA: LD_COLOR(1) TYPE C.

    SELECT EBELN EBELP STATU AEDAT MATNR MENGE MEINS NETPR PEINH

    UP TO 10 ROWS

    FROM EKPO

    INTO TABLE IT_EKKO.

    *Populate field with color attributes

    LOOP AT IT_EKKO INTO WA_EKKO.

    • Populate color variable with colour properties

    • Char 1 = C (This is a color property)

    • Char 2 = 3 (Color codes: 1 - 7)

    • Char 3 = Intensified on/off ( 1 or 0 )

    • Char 4 = Inverse display on/off ( 1 or 0 )

    • i.e. wa_ekko-line_color = 'C410'

    LD_COLOR = LD_COLOR + 1.

    • Only 7 colours so need to reset color value

    IF LD_COLOR = 8.

    LD_COLOR = 1.

    ENDIF.

    CONCATENATE 'C' LD_COLOR '10' INTO WA_EKKO-LINE_COLOR.

    • wa_ekko-line_color = 'C410'.

    MODIFY IT_EKKO FROM WA_EKKO.

    ENDLOOP.

    ENDFORM. " DATA_RETRIEVAL

    <REMOVED BY MODERATOR>

    Edited by: Alvaro Tejada Galindo on Apr 10, 2008 5:30 PM

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      hi,

      here is the example of hierarchical ALV

      TYPE-POOLS : slis.
      
      TABLES : mseg.
      
      DATA : BEGIN OF itab_head OCCURS 0,
              mat LIKE mseg-matnr,
      *        matnr LIKE mseg-matnr,
              werks LIKE mseg-werks,
             END OF itab_head.
      
      DATA : BEGIN OF itab_item OCCURS 0,
      *        mat LIKE mseg-matnr,
              matnr LIKE mseg-matnr,
              werks LIKE mseg-werks,
              mblnr LIKE mseg-mblnr,
              menge LIKE mseg-menge,
             END OF itab_item.
      
      DATA : t_fcat TYPE slis_t_fieldcat_alv,
             key_info TYPE slis_keyinfo_alv,
             t_eve TYPE slis_t_event,
             gt_subtot TYPE slis_t_sortinfo_alv,
             subtot LIKE LINE OF gt_subtot,
             t_listhead TYPE slis_t_listheader,
             st_line TYPE slis_listheader.
      
      DATA : lin_no TYPE i.
      
      DATA : t_mtdoc LIKE mseg-mblnr.
      
      SELECT-OPTIONS : mat FOR mseg-matnr.
      
      INITIALIZATION.
        PERFORM build_cat USING t_fcat.
        PERFORM build_eve.
      
      START-OF-SELECTION.
        PERFORM get_data.
        PERFORM dis_data.
      
      
      *&---------------------------------------------------------------------*
      *&      Form  build_cat
      *&---------------------------------------------------------------------*
      *       text
      *----------------------------------------------------------------------*
      *      -->TEMP_FCAT  text
      *----------------------------------------------------------------------*
      FORM build_cat USING temp_fcat TYPE slis_t_fieldcat_alv.
      
        DATA : wa_fcat TYPE slis_fieldcat_alv.
      
        wa_fcat-tabname = 'ITAB_HEAD'.
        wa_fcat-fieldname = 'MAT'.
        wa_fcat-seltext_m = 'Material'.
        APPEND wa_fcat TO temp_fcat.
        CLEAR wa_fcat.
      
        wa_fcat-tabname = 'ITAB_HEAD'.
        wa_fcat-fieldname = 'WERKS'.
        wa_fcat-seltext_m = 'Plant'.
      *  wa_fcat-edit = 'X'.
      *  wa_fcat-input = 'X'.
        APPEND wa_fcat TO temp_fcat.
        CLEAR wa_fcat.
      
        wa_fcat-tabname = 'ITAB_ITEM'.
        wa_fcat-fieldname = 'MBLNR'.
        wa_fcat-seltext_m = 'Material Doc.'.
        APPEND wa_fcat TO temp_fcat.
        CLEAR wa_fcat.
      
        wa_fcat-tabname = 'ITAB_ITEM'.
        wa_fcat-fieldname = 'MENGE'.
        wa_fcat-seltext_m = 'Quantity'.
      *  wa_fcat-edit = 'X'.
      *  wa_fcat-input = 'X'.
      *  wa_fcat-do_sum = 'Y'.
        APPEND wa_fcat TO temp_fcat.
        CLEAR wa_fcat.
      
        
      ENDFORM.                    "build_cat
      
      *&---------------------------------------------------------------------*
      *&      Form  build_eve
      *&---------------------------------------------------------------------*
      *       text
      *----------------------------------------------------------------------*
      FORM build_eve.
      
        DATA : wa_eve TYPE slis_alv_event.
      
        CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
         EXPORTING
           i_list_type           = 0
         IMPORTING
           et_events             = t_eve
      *   EXCEPTIONS
      *     LIST_TYPE_WRONG       = 1
      *     OTHERS                = 2
                  .
        IF sy-subrc <> 0.
      * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
      *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
        ENDIF.
      
        READ TABLE t_eve INTO wa_eve WITH KEY name = 'TOP_OF_PAGE'.
        IF sy-subrc = 0.
          wa_eve-form = 'TOP_OF_PAGE'.
          MODIFY t_eve FROM wa_eve INDEX sy-tabix.
        ENDIF.
      
      
      ENDFORM.                    "build_eve
      *&---------------------------------------------------------------------*
      *&      Form  get_data
      *&---------------------------------------------------------------------*
      *       text
      *----------------------------------------------------------------------*
      FORM get_data.
      
        SELECT matnr werks mblnr menge FROM mseg INTO CORRESPONDING FIELDS OF TABLE itab_item
        WHERE matnr IN mat.
      
      ENDFORM.                    "get_data
      
      *&---------------------------------------------------------------------*
      *&      Form  dis_data
      *&---------------------------------------------------------------------*
      *       text
      *----------------------------------------------------------------------*
      FORM dis_data.
      
      
        key_info-header01 = 'MAT'.
        key_info-item01 = 'MATNR'.
        key_info-header02 = 'WERKS'.
        key_info-item02 = 'WERKS'.
      
        REFRESH itab_head.
        LOOP AT itab_item.
          ON CHANGE OF itab_item-matnr OR itab_item-werks.
            MOVE-CORRESPONDING itab_item TO itab_head.
            itab_head-mat = itab_item-matnr.
            APPEND itab_head.
          ENDON.
        ENDLOOP.
      
        CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
          EXPORTING
            i_callback_program             = 'ZALV_PRDS'
            it_fieldcat                    = t_fcat
            it_events                      = t_eve[]
            i_tabname_header               = 'ITAB_HEAD'
            i_tabname_item                 = 'ITAB_ITEM'
            is_keyinfo                     = key_info
          TABLES
            t_outtab_header                = itab_head
            t_outtab_item                  = itab_item
      * EXCEPTIONS
      *   PROGRAM_ERROR                  = 1
      *   OTHERS                         = 2
                  .
        IF sy-subrc <> 0.
      * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
      *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
        ENDIF.
      
      
      ENDFORM.                    "dis_data
      
      
      *&---------------------------------------------------------------------*
      *&      Form  top_of_page
      *&---------------------------------------------------------------------*
      *       text
      *----------------------------------------------------------------------*
      FORM top_of_page.
      
        CLEAR st_line.
        st_line-typ = 'H'.
        st_line-info = 'Dhwani Shah'.
        APPEND st_line TO t_listhead.
      
        CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
          EXPORTING
            it_list_commentary       = t_listhead
      *   I_LOGO                   =
      *   I_END_OF_LIST_GRID       =
      *   I_ALV_FORM               =
                  .
      
      
      ENDFORM.                    "top_of_page
      

      <REMOVED BY MODERATOR>

      Edited by: Alvaro Tejada Galindo on Apr 10, 2008 5:31 PM

  • author's profile photo Former Member
    Former Member
    Posted on Apr 09, 2008 at 12:55 PM

    Hi

    1st you need to move the data what you fetch from that 2 tables into one table that is your final internal table

    write a for all entries for that 2 tables and move the data to final internal table then use that FM

    give that final internal table to display data

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 10, 2008 at 06:26 AM

    Hi,

    1. if u want to display 2 iab in single alv, then move noth the itabs to a single itab and display..

    else...

    2 . if u want to display in two alv in a single screen, then try this..

    
    REPORT ZSBN_BLOCK_ALV .
    
    TYPE-POOLS : SLIS.
    
    TABLES: PA0001, PA0002.
    
    DATA  : FCAT        TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
            FCAT1       TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,
            ILAYOUT     TYPE SLIS_LAYOUT_ALV,
            XS_EVENT    TYPE SLIS_ALV_EVENT,
            GT_XEVENTS  TYPE SLIS_T_EVENT,
            GT_YEVENTS  TYPE SLIS_T_EVENT,
            IT_SORT     TYPE SLIS_T_SORTINFO_ALV,
            WA_SORT     TYPE SLIS_SORTINFO_ALV.
    
    
    DATA : BEGIN OF INT_PA0001 OCCURS 0.
              INCLUDE TYPE PA0001.
    DATA : END OF INT_PA0001.
    
    DATA : BEGIN OF INT_PA0002 OCCURS 0.
              INCLUDE TYPE PA0002.
    DATA : END OF INT_PA0002.
    
    *&*& DATA FROM PA0001
    SELECT * FROM PA0001
           UP TO 5 ROWS
           INTO TABLE INT_PA0001.
    
    *&*& DATA FROM PA0002
    IF NOT INT_PA0001[] IS INITIAL.
      SELECT * FROM PA0002 INTO TABLE INT_PA0002
        FOR ALL ENTRIES IN INT_PA0001
        WHERE
               PERNR EQ INT_PA0001-PERNR.
    ENDIF.
    
    *&*& LAYOUT DEF
     ILAYOUT-COLWIDTH_OPTIMIZE = 'X'.
     ILAYOUT-ZEBRA  =  'X'.
    
    *&*& FCAT1
    CLEAR FCAT.
    FCAT-FIELDNAME = 'PERNR'.
    FCAT-SELTEXT_M   = 'Emp No'.
    FCAT-TABNAME  = 'INT_PA0001'.
    APPEND FCAT.
    
    CLEAR FCAT.
    FCAT-FIELDNAME = 'BEGDA'.
    FCAT-SELTEXT_M   = 'St Date'.
    FCAT-TABNAME  = 'INT_PA0001'.
    APPEND FCAT.
    
    CLEAR FCAT.
    FCAT-FIELDNAME = 'ENDDA'.
    FCAT-SELTEXT_M   = 'End Date'.
    FCAT-TABNAME  = 'INT_PA0001'.
    APPEND FCAT.
    
    CLEAR FCAT.
    FCAT-FIELDNAME = 'BUKRS'.
    FCAT-SELTEXT_M   = 'Comp Code'.
    FCAT-TABNAME  = 'INT_PA0001'.
    APPEND FCAT.
    
    CLEAR FCAT.
    FCAT-FIELDNAME = 'WERKS'.
    FCAT-SELTEXT_M   = 'Plant'.
    FCAT-TABNAME  = 'INT_PA0001'.
    APPEND FCAT.
    
    *&*& FCAT2
    CLEAR FCAT1.
    FCAT1-FIELDNAME = 'PERNR'.
    FCAT1-SELTEXT_M   = 'Emp No'.
    FCAT-TABNAME  = 'INT_PA0002'.
    APPEND FCAT1.
    
    CLEAR FCAT1.
    FCAT1-FIELDNAME = 'NACHN'.
    FCAT1-SELTEXT_M   = 'Emp Name'.
    FCAT-TABNAME  = 'INT_PA0002'.
    APPEND FCAT1.
    
    CLEAR FCAT1.
    FCAT1-FIELDNAME = 'INITS'.
    FCAT1-SELTEXT_M   = 'Initial'.
    FCAT-TABNAME  = 'INT_PA0002'.
    APPEND FCAT1.
    
    *&*& FOR EVENTS
    CLEAR XS_EVENT.
    XS_EVENT-NAME = SLIS_EV_TOP_OF_PAGE.
    XS_EVENT-FORM = 'XTOP_OF_PAGE'.
    APPEND XS_EVENT TO GT_XEVENTS.
    CLEAR XS_EVENT.
    
    CLEAR XS_EVENT.
    XS_EVENT-NAME = SLIS_EV_TOP_OF_PAGE.
    XS_EVENT-FORM = 'YTOP_OF_PAGE'.
    APPEND XS_EVENT TO GT_YEVENTS.
    CLEAR XS_EVENT.
    
    *&*& FOR SORT
    CLEAR WA_SORT.
    WA_SORT-FIELDNAME = 'PERNR'.
    APPEND WA_SORT TO IT_SORT.
    CLEAR WA_SORT.
    
    
    *&*& BLOCK ALV -- INTIALIZATION
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT'
      EXPORTING
        I_CALLBACK_PROGRAM             = SY-CPROG.
    
    *&*& APPENDING FIRST FCAT
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
      EXPORTING
        IS_LAYOUT                        = ILAYOUT
        IT_FIELDCAT                      = FCAT[]
        I_TABNAME                        = 'INT_PA0001'
        IT_EVENTS                        = GT_XEVENTS
        IT_SORT                          = IT_SORT
      TABLES
        T_OUTTAB                         = INT_PA0001.
    
    *&*& APPENDING SECOND FCAT
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
      EXPORTING
        IS_LAYOUT                        = ILAYOUT
        IT_FIELDCAT                      = FCAT1[]
        I_TABNAME                        = 'INT_PA0002'
        IT_EVENTS                        = GT_YEVENTS
        IT_SORT                          = IT_SORT
      TABLES
        T_OUTTAB                         = INT_PA0002.
    
    *&*& FUN MOD FOR DISPLAY
    CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY'.
    
    
    *&*& FOR TOP OF PAGE ALV-1
    FORM XTOP_OF_PAGE.
      WRITE : 'ALV 1', 30 SY-DATUM.
    ENDFORM.
    
    *&*& FOR TOP OF PAGE ALV-2
    FORM YTOP_OF_PAGE.
      WRITE : 'ALV 2', 30 SY-DATUM.
    ENDFORM.
    
    

    With Rgds,

    S.Barani

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 10, 2008 at 12:51 PM

    Thank You All.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.