11 Replies Latest reply: Sep 19, 2011 9:24 PM by Clemens Li RSS

How to group data in ALV grid display

Shubham Agarwal
Currently Being Moderated

Hello Experts,

 

I am using REUSE_ALV_GRID_DISPLAY to display my data.

here, I am using SLIS_T_FIELDCAT_ALV to create field catalog.

 

My requirement is data of 1 field/column in ALV had to be displayed by grouping.

Is there any field any fieldcatalog which I can set so that data will be displayed as after grouping.

 

I had searched a lot but found solution for OO ALV but not for function modules.

 

Edited by: shubh_ag on Sep 2, 2011 4:38 PM

  • Re: How to group data in ALV grid display
    Shivakumar Hosaganiger
    Currently Being Moderated

    Hi,

     

    use sort option to group the column in ALV and pass the required fields

     

    http://wiki.sdn.sap.com/wiki/display/Snippets/ABAP-ALVReportGroupFunctionalitywithCheckbox

     

    Shiva

    • Re: How to group data in ALV grid display
      Shubham Agarwal
      Currently Being Moderated

      Hi,

      I had used following code:

      wa_sort-spos = '1'.
        wa_sort-fieldname = 'DMBTR'.
        wa_sort-tabname = 'IT_GOODS'.
        wa_sort-up = 'X'.
      
       APPEND wa_sort to it_sort.
      
      clear wa_sort.
      wa_sort-spos = '2'.
        wa_sort-fieldname = 'BELNR'.
        wa_sort-tabname = 'IT_GOODS'.
        wa_sort-up = 'X'.
      
       APPEND wa_sort to it_sort.

       

       

      but then also values are not grouped for DMBTR field(but its coming as sorted) however the values are coming as a group and sorted for BELNR field.

      What is the problem?

       

      Edited by: shubh_ag on Sep 3, 2011 6:00 PM

      • Re: How to group data in ALV grid display
        Clemens Li
        Currently Being Moderated

        Hi shubh_ag,

         

        DMBTR is a CURRency value field. Only Character fields can be sorted/grouped.      

         

        Could you post 3-5 lines of expected grid (formatted as code to preserve columns)?

         

        Regards

         

        Clemens

        • Re: How to group data in ALV grid display
          Shubham Agarwal
          Currently Being Moderated

          Hi,

           

          @clemens: At present output is like:

           

          550000289     10      10000

          550000289     10      10000

          550000289     10      10000

          550000289     10      10000

           

          The problem is if anytime I am doing SUM for this column then result will be 4 times of the actual value.

          So, I need data to be displayed like this:

           

          550000289     10      10000

          550000289     10     

          550000289     10     

          550000289     10     

           

           

           

          Regards,

          Shubh

          • Re: How to group data in ALV grid display
            Madhumahesh T
            Currently Being Moderated

            just try,

            delete adjacent duplicates from <itab> comparing <fieldname of amount>."del only that amount col

            then use sort and sutot.

            hope it will be done as per  your requirement.

             

             

            regards,

            kool.

          • Re: How to group data in ALV grid display
            Jeevan Sagar
            Currently Being Moderated

            Since you cannot sort by numeric fields add a field which corresponds to DMBTR in character type. Copy all the column values of DMBTR to new character field, sort it but do not display it in the alv grid. Now make the duplicate DMBTR to zero using double loop.

             

            
            loop at itab.
            loop at itab where dmbtr = itab-dmbtr.
            check sy-tabix ne 1.
            clear itab-dmbtr.
            modify itab.
            endloop.
            endloop.
            

             

          • Re: How to group data in ALV grid display
            Muralii immadisetty
            Currently Being Moderated

            Hi

             

            You can excute the below code.Accouding to the below logic u can write ur code.May it will be useful for your requirment

             

            REPORT  ZMRL_SAMPLE3.

            TYPES:BEGIN OF TY_DATA,

              ENO TYPE I,

              ACC TYPE I,

              JJ TYPE string,

              END OF TY_DATA.

            DATA:ITAB TYPE STANDARD TABLE OF TY_DATA,

                  WA TYPE TY_DATA,

                  ITAB1 TYPE STANDARD TABLE OF TY_DATA,

                  WA1 TYPE TY_DATA.

             

            START-OF-SELECTION.

              WA-ENO = '550002'.

              WA-ACC = '10'.

              WA-JJ = '1000'.

              APPEND WA TO ITAB.

              WA-ENO = '550002'.

              WA-ACC = '10'.

              WA-JJ = '1000'.

              APPEND WA TO ITAB.

              WA-ENO = '550002'.

              WA-ACC = '10'.

              WA-JJ = '1000'.

              APPEND WA TO ITAB.

              loop at itab into wa.

                at new jj.

                  wa1-jj = wa-jj.

                endat.

              wa1-eno = wa-eno.

              wa1-acc = wa-acc.

              append wa1 to itab1.

              clear:wa1.

              endloop.

              loop at itab1 into wa1.

                write:/ wa1-eno,wa1-acc,wa1-jj.

              endloop.

             

            output: 550,002          10  1000

            550,002          10

            550,002          10

             

            regards

            muralli

  • Re: How to group data in ALV grid display
    Madhumahesh T
    Currently Being Moderated

    HI, Try this code ,

    *&---------------------------------------------------------------------*
    *& Report  ZALV_SALES_ALVREPORT2
    
    REPORT  ZALV_SALES_ALVREPORT2.
    TYPE-POOLS:SLIS.
    INCLUDE:ZINCLVBAK.
    DATA:IT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
         WA_FIELDCAT TYPE SLIS_FIELDCAT_ALV,
    
         IT_SORTINFO TYPE SLIS_T_SORTINFO_ALV,
         WA_SORTINFO TYPE SLIS_SORTINFO_ALV.
    DATA:G_VBELN TYPE VBAK-VBELN.
    SELECT-OPTIONS:S_VBELN FOR G_VBELN.
    START-OF-SELECTION.
    PERFORM GET_VBAK.
    PERFORM OPT.
    *&---------------------------------------------------------------------*
    *&      Form  GET_VBAK
    *&---------------------------------------------------------------------*
    FORM GET_VBAK .
    SELECT VBELN
           ERDAT
           ERNAM
           NETWR
           WAERK
           VKORG FROM VBAK
            INTO TABLE IT_VBAK
            WHERE VBELN IN S_VBELN.
    
    ENDFORM.                    " GET_VBAK
    *&---------------------------------------------------------------------*
    *&      Form  OPT
    *&---------------------------------------------------------------------*
    FORM OPT .
    WA_FIELDCAT-COL_POS = '1'.
    WA_FIELDCAT-ROW_POS = '1'.
    WA_FIELDCAT-FIELDNAME = 'VBELN'.
    WA_FIELDCAT-SELTEXT_M = 'SALESDOCNO'.
    APPEND WA_FIELDCAT TO IT_FIELDCAT.
    CLEAR WA_FIELDCAT.
    
    WA_FIELDCAT-COL_POS = '2'.
    WA_FIELDCAT-ROW_POS = '1'.
    WA_FIELDCAT-FIELDNAME = 'ERDAT'.
    WA_FIELDCAT-SELTEXT_M = 'DATE'.
    APPEND WA_FIELDCAT TO IT_FIELDCAT.
    CLEAR WA_FIELDCAT.
    
    WA_FIELDCAT-COL_POS = '3'.
    WA_FIELDCAT-ROW_POS = '1'.
    WA_FIELDCAT-FIELDNAME = 'ERNAM'.
    WA_FIELDCAT-SELTEXT_M = 'SALESDOCNO'.
    WA_FIELDCAT-EMPHASIZE = 'C661'.
    APPEND WA_FIELDCAT TO IT_FIELDCAT.
    CLEAR WA_FIELDCAT.
    
    WA_FIELDCAT-COL_POS = '4'.
    WA_FIELDCAT-ROW_POS = '1'.
    WA_FIELDCAT-FIELDNAME = 'NETWR'.
    WA_FIELDCAT-SELTEXT_M = 'AMOUNT'.
    WA_FIELDCAT-DO_SUM = 'X'.
    APPEND WA_FIELDCAT TO IT_FIELDCAT.
    CLEAR WA_FIELDCAT.
    
    WA_FIELDCAT-COL_POS = '5'.
    WA_FIELDCAT-ROW_POS = '1'.
    WA_FIELDCAT-FIELDNAME = 'WAERK'.
    WA_FIELDCAT-SELTEXT_M = 'CURRENCY'.
    APPEND WA_FIELDCAT TO IT_FIELDCAT.
    CLEAR WA_FIELDCAT.
    
    WA_FIELDCAT-COL_POS = '6'.
    WA_FIELDCAT-ROW_POS = '1'.
    WA_FIELDCAT-FIELDNAME = 'VKORG'.
    WA_FIELDCAT-SELTEXT_M = 'SALESORG'.
    APPEND WA_FIELDCAT TO IT_FIELDCAT.
    CLEAR WA_FIELDCAT.
    
    
    ***************DATE WISE SORTING***********************
    WA_SORTINFO-FIELDNAME = 'ERDAT'.
    WA_SORTINFO-SUBTOT = 'X'.
    APPEND WA_SORTINFO TO IT_SORTINFO.
    CLEAR WA_SORTINFO.
    
    
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
     EXPORTING
       I_CALLBACK_PROGRAM                = SY-REPID
       I_GRID_TITLE                      = 'SALESDOCUMENT HEDER DETAILS'
       IT_FIELDCAT                       = IT_FIELDCAT
       IT_SORT                           = IT_SORTINFO
      TABLES
        T_OUTTAB                          = IT_VBAK
              .
    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.                    " OPT

     

    regards,

    kool.

  • Re: How to group data in ALV grid display
    Abhisek Pradhan
    Currently Being Moderated

    Hi shubh,

     

    try this,

     

    * Declarations related to ALV Display

     

    TYPE-POOLS : slis.

     

    DATA : gt_fcat TYPE slis_t_fieldcat_alv ,

           gs_fcat TYPE slis_fieldcat_alv,

           list_gt_fcat TYPE slis_t_fieldcat_alv ,

           list_gs_fcat TYPE slis_fieldcat_alv,

           gt_listheader TYPE slis_t_listheader,

           gs_listheader TYPE slis_listheader,

           gs_layout TYPE slis_layout_alv,

           list_gs_layout TYPE slis_layout_alv,

           gs_wvar TYPE i VALUE 1.

     

    DATA: gt_events           TYPE slis_t_event,

          gs_events           LIKE LINE OF gt_events,

          gt_sort             TYPE  slis_t_sortinfo_alv,

          gs_sort             TYPE slis_sortinfo_alv.

     

     

    DATA:gs_ovar TYPE disvariant.

     

     

    FORM alv_display .

      PERFORM field_catalog.

      PERFORM display_data.

    ENDFORM.                    " ALV_DISPLAY

    &----


    *&      Form  FIELD_CATALOG

    &----


    •       text

    ----


    • -->  p1        text

    • <--  p2        text

    ----


     

     

    FORM field_catalog .

     

      gs_fcat-col_pos   = gs_wvar.

      gs_fcat-tabname   = 'GT_FINAL'.

      gs_fcat-fieldname = 'MATERIAL'.

      gs_fcat-do_sum    = 'X'.

      gs_fcat-seltext_m = text-001.

      gs_fcat-emphasize = 'C510'.

      gs_fcat-fix_column = 'X'.

      APPEND gs_fcat TO gt_fcat.

      CLEAR gs_fcat.

      ADD 1 TO gs_wvar.

     

     

      gs_fcat-col_pos = gs_wvar.

      gs_fcat-tabname = 'GT_FINAL'.

      gs_fcat-fieldname = 'DESCRIPTION'.

      gs_fcat-seltext_m = text-011.

      gs_fcat-emphasize = 'C510'.

      gs_fcat-fix_column = 'X'.

      APPEND gs_fcat TO gt_fcat.

      CLEAR gs_fcat.

      ADD 1 TO gs_wvar.

     

      gs_fcat-col_pos = gs_wvar.

      gs_fcat-tabname = 'GT_FINAL'.

      gs_fcat-fieldname = 'TYPE'.

      gs_fcat-seltext_m = text-002.

      gs_fcat-emphasize = 'C510'.

      gs_fcat-fix_column = 'X'.

      APPEND gs_fcat TO gt_fcat.

      CLEAR gs_fcat.

      ADD 1 TO gs_wvar.

     

      gs_fcat-col_pos = gs_wvar.

      gs_fcat-tabname = 'GT_FINAL'.

      gs_fcat-fieldname = 'UOM'.

      gs_fcat-seltext_m = text-003.

      gs_fcat-emphasize = 'C510'.

      gs_fcat-fix_column = 'X'.

      APPEND gs_fcat TO gt_fcat.

      CLEAR gs_fcat.

      ADD 1 TO gs_wvar.

     

      gs_fcat-col_pos = gs_wvar.

      gs_fcat-tabname = 'GT_FINAL'.

      gs_fcat-fieldname = 'PLANT'.

      gs_fcat-do_sum    = 'X'.

      gs_fcat-seltext_m = text-004.

      gs_fcat-emphasize = 'C510'.

      gs_fcat-fix_column = 'X'.

      APPEND gs_fcat TO gt_fcat.

      CLEAR gs_fcat.

      ADD 1 TO gs_wvar.

     

      gs_fcat-col_pos = gs_wvar.

      gs_fcat-tabname = 'GT_FINAL'.

      gs_fcat-fieldname = 'PURCHASING_GROUP'.

      gs_fcat-seltext_m = text-005.

      gs_fcat-emphasize = 'C510'.

      gs_fcat-fix_column = 'X'.

      APPEND gs_fcat TO gt_fcat.

      CLEAR gs_fcat.

      ADD 1 TO gs_wvar.

     

      gs_fcat-col_pos = gs_wvar.

      gs_fcat-tabname = 'GT_FINAL'.

      gs_fcat-fieldname = 'MRP_TYPE'.

      gs_fcat-seltext_m = text-006.

      gs_fcat-emphasize = 'C510'.

      gs_fcat-fix_column = 'X'.

      APPEND gs_fcat TO gt_fcat.

      CLEAR gs_fcat.

      ADD 1 TO gs_wvar.

     

      gs_fcat-col_pos = gs_wvar.

      gs_fcat-tabname = 'GT_FINAL'.

      gs_fcat-fieldname = 'MRP_CONTROLLER'.

      gs_fcat-seltext_m = text-007.

      gs_fcat-emphasize = 'C510'.

      gs_fcat-fix_column = 'X'.

      APPEND gs_fcat TO gt_fcat.

      CLEAR gs_fcat.

      ADD 1 TO gs_wvar.

     

      gs_fcat-col_pos = gs_wvar.

      gs_fcat-tabname = 'GT_FINAL'.

      gs_fcat-fieldname = 'SPECIAL_PRO '.

      gs_fcat-seltext_m = text-008.

      gs_fcat-emphasize = 'C510'.

      gs_fcat-fix_column = 'X'.

      APPEND gs_fcat TO gt_fcat.

      CLEAR gs_fcat.

      ADD 1 TO gs_wvar.

     

      gs_fcat-col_pos = gs_wvar.

      gs_fcat-tabname = 'GT_FINAL'.

      gs_fcat-fieldname = 'STORAGE_LOCATION'.

      gs_fcat-seltext_m = text-009.

      gs_fcat-emphasize = 'C510'.

      gs_fcat-fix_column = 'X'.

      APPEND gs_fcat TO gt_fcat.

      CLEAR gs_fcat.

      ADD 1 TO gs_wvar.

     

      gs_fcat-col_pos   = gs_wvar.

      gs_fcat-tabname   = 'GT_FINAL'.

      gs_fcat-fieldname = 'UNRESTRCITED_STOCK'.

      gs_fcat-do_sum    = 'X'.

      gs_fcat-seltext_m = text-010.

      gs_fcat-emphasize = 'C510'.

      gs_fcat-fix_column = 'X'.

      APPEND gs_fcat TO gt_fcat.

      CLEAR gs_fcat.

      ADD 1 TO gs_wvar.

     

     

      gs_sort-fieldname = 'MATERIAL'.

      gs_sort-tabname = 'GT_FINAL'.

      gs_sort-subtot = 'X'.

      APPEND gs_sort TO gt_sort.

     

      gs_sort-fieldname = 'PLANT'.

      gs_sort-tabname = 'GT_FINAL'.

      gs_sort-subtot = 'X'.

      APPEND gs_sort TO gt_sort.

     

     

     

    ENDFORM.                    " FIELD_CATALOG

    &----


    *&      Form  DISPLAY_DATA

    &----


    •       text

    ----


    • -->  p1        text

    • <--  p2        text

    ----


    FORM display_data .

     

     

      DATA:c_top  TYPE slis_formname.

     

      c_top  = 'TOP'.

     

     

    • " For avoiding EPC warnings

    • CLEAR sy-index.

    • IF sy-index EQ 1.

      PERFORM top.

    *

    • ENDIF.

     

      gs_layout-zebra = 'X'.

      gs_layout-colwidth_optimize = 'X'.

     

     

      CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

        EXPORTING

          i_callback_program      = sy-cprog

          i_callback_user_command = 'USER_COMMAND'

          i_callback_top_of_page  = c_top

          is_layout               = gs_layout

          it_fieldcat             = gt_fcat[]

          it_sort                 = gt_sort

          i_default               = 'X'

          i_save                  = 'U'

          is_variant              = gs_ovar

        TABLES

          t_outtab                = gt_final

        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_DATA

     

     

     

    Regards,

    Abhisek

Actions