[SAP ABAP] Column in ABAP Display Output Report


Source Code
*&---------------------------------------------------------------------*
*& Report  YDYNAMIC_COL_01                                             *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  ydynamic_col_01    LINE-SIZE 1023 NO STANDARD PAGE HEADING.

TABLES : mara.

TYPES : BEGIN OF ty_matkl,
          matkl LIKE mara-matkl,
        END OF ty_matkl.

DATA : gi_mara LIKE STANDARD TABLE OF mara WITH HEADER LINE,
       gi_tmp_matkl LIKE STANDARD TABLE OF mara WITH HEADER LINE,
       gi_matkl TYPE STANDARD TABLE OF ty_matkl WITH HEADER LINE.


SELECT-OPTIONS : so_matkl FOR mara-matkl DEFAULT '004' TO '007'.

START-OF-SELECTION.
  PERFORM fm_collect_data.
  PERFORM fm_process_data.
  PERFORM fm_display_data.


*&--------------------------------------------------------------------*
*&      Form  fm_collect_data
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
FORM fm_collect_data.
  SELECT * INTO TABLE gi_mara
  FROM mara
  WHERE matkl IN so_matkl.

ENDFORM.                    "fm_collect_data

*&--------------------------------------------------------------------*
*&      Form  fm_process_data
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
FORM fm_process_data.
  gi_tmp_matkl[] = gi_mara[].
  SORT gi_tmp_matkl BY matkl.
  DELETE ADJACENT DUPLICATES FROM gi_tmp_matkl
  COMPARING matkl.

  LOOP AT gi_tmp_matkl.
    gi_matkl-matkl = gi_tmp_matkl-matkl.
    APPEND gi_matkl.
  ENDLOOP.
ENDFORM.                    "fm_process_data

*&--------------------------------------------------------------------*
*&      Form  fm_display_data
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
FORM fm_display_data.
  DATA : lv_posx TYPE n LENGTH 4,
         lv_posy TYPE i,
         lv_totalrow TYPE i,
         lv_totaltmp TYPE i,
         lv_tabix TYPE i,
         lv_maxcol TYPE i.

  lv_maxcol = ( LINES( gi_matkl ) * 20 ) + ( LINES( gi_matkl ) * 2 ).

  IF lv_maxcol > 1023.
    MESSAGE 'Max Colom 1023 Char !!' TYPE 'I'.
    EXIT.
  ENDIF.

  "Write Dynamic Header base on Mat. Group
  WRITE / '|'.
  ULINE AT 2(lv_maxcol).
  WRITE / '|'.
  LOOP AT gi_matkl.

    WRITE AT lv_posx(1) '|'.
    lv_posx = lv_posx + 2.

    WRITE AT lv_posx(20) gi_matkl-matkl CENTERED.

    lv_posx = lv_posx + 20.

  ENDLOOP.


  IF LINES( gi_matkl ) > 0.

    "Write last Pipe at Header
    lv_posx = lv_posx + 1.
    WRITE AT lv_posx(1) '|'.


    "Cek Total Row
    LOOP AT gi_matkl.
      CLEAR lv_totaltmp.
      LOOP AT gi_mara WHERE matkl = gi_matkl-matkl.
        lv_totaltmp = lv_totaltmp + 1.
      ENDLOOP.

      IF lv_totaltmp > lv_totalrow.
        lv_totalrow = lv_totaltmp.
      ENDIF.
    ENDLOOP.

    "Write Item Data
    CLEAR lv_posx.
    SORT gi_mara BY matkl matnr.

    WRITE / '|'.
    DO lv_totalrow TIMES.
      lv_posy = lv_posy + 1.


      ULINE AT 2(lv_maxcol).
      WRITE / '|'.

      CLEAR : lv_posx, lv_tabix.
      LOOP AT gi_matkl.

        WRITE AT lv_posx(1) '|'.
        lv_posx = lv_posx + 2.

        READ TABLE gi_mara WITH KEY matkl = gi_matkl-matkl.
        IF sy-subrc = 0.
          lv_tabix = sy-tabix + lv_posy - 1.
          READ TABLE gi_mara INDEX lv_tabix.
          IF sy-subrc = 0.
            IF gi_mara-matkl = gi_matkl-matkl.
              WRITE AT lv_posx(20) gi_mara-matnr.
            ENDIF.
          ENDIF.
        ENDIF.

        lv_posx = lv_posx + 20.

      ENDLOOP.

      "Write last Pipe
      lv_posx = lv_posx + 1.
      WRITE AT lv_posx(1) '|'.

      WRITE / '|'.


    ENDDO.
    ULINE AT 2(lv_maxcol).

  ENDIF.

ENDFORM.                    "fm_display_data

Comments