How to create a table using REUSE_ALV in the simplest way in abap.

REPORT zcem_reuse_alv."Your Program Name

TYPE-POOLSslis.

*--------------------------------------------------------------------*
TABLESscarr."You can write tables names here.

*----------------------------------------------------------------------*
* ALV VARIABLES
*----------------------------------------------------------------------*
DATA:
  gs_fieldcat TYPE lvc_s_fcat,
  gt_fieldcat TYPE lvc_t_fcat,
  gt_sort     TYPE lvc_t_sort,
  gs_sort     TYPE lvc_s_sort,
  gs_layout   TYPE lvc_s_layo,
  variant     TYPE disvariant.
CONSTANTS :
  gc_form TYPE slis_alv_event-form VALUE 'TOP_OF_PAGE'.

"You should create a structure, and add the fields that you want to display on your ALV screen.
"If you'd like, you can define a table yourself and use it. I will provide two examples below.

DATA :
  gt_out TYPE TABLE OF zstr_name," Your Structure
  gs_out TYPE zstr_name"Your Structure


*--------------------------------------------------------------------*
"Optional
**TYPES:
**  BEGIN OF gty_structure,
**    currcode TYPE s_currcode,
**    carrname TYPE s_carnamee,
*     ...
*     url      TYPE s_url,
*     ...
**  END OF gty_structure,
**


*DATA :
*  gt_out TYPE TABLE OF gty_structure," Your Structure
*  gs_out TYPE gty_structure. "Your Structure
*--------------------------------------------------------------------*


*----------------------------------------------------------------------*
* SELECTION SCREEN
*----------------------------------------------------------------------*
"You can customize this selection screen according to your preferences.
SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE TEXT-s01.
  SELECT-OPTIONS s_carrid   FOR scarr-carrid.
  SELECT-OPTIONS s_carrname FOR scarr-carrname.
  SELECT-OPTIONS s_currcode FOR scarr-currcode.
  SELECT-OPTIONS s_url      FOR scarr-url.
SELECTION-SCREEN END OF BLOCK bl1.

*--------------------------------------------------------------------*
INITIALIZATION.
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
  "...
*  IF sy-ucomm = '???'.
*  ENDIF.

*--------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM get_data.
  IF gt_out[] IS NOT INITIAL.
    PERFORM write_report.
  ELSE.
    MESSAGE 'Data not found!' TYPE 'S' DISPLAY LIKE 'E'.
  ENDIF.

*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
FORM get_data.
  "You write your SQL query in this area. The following example is made for the SCARR table.
  SELECT
      scarr~carrid,
      scarr~carrname,
      scarr~currcode,
      scarr~url
    FROM scarr
    WHERE scarr~carrid EQ 'AZ'
    AND  scarr~carrid    IN  @s_carrid
    AND  scarr~carrnames IN  @s_carrnames
    AND  scarr~currcode  IN  @s_currcode
    AND  scarr~url       IN  @s_url
    INTO CORRESPONDING FIELDS OF TABLE @gt_out[].
 
 "In this area, you can use field symbols within a loop to arrange the data in the desired format. It is optional in this area.   
  LOOP AT gt_out[] ASSIGNING FIELD-SYMBOL(<fs_gt_out>).
    "Loop Area.
  ENDLOOP.
  
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  WRITE_REPORT
*&---------------------------------------------------------------------*
FORM write_report.
  PERFORM prepare_fcat.
  PERFORM prepare_layout.
  PERFORM display_alv.
ENDFORM.                    " WRITE_REPORT
*&---------------------------------------------------------------------*
*&      Form  prepare_fcat
*&---------------------------------------------------------------------*
FORM prepare_fcat .

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_buffer_active        ''
      i_structure_name       'ZSTR_NAME'"<---This area is important. You should write the name of the structure you created here. 
*     I_CLIENT_NEVER_DISPLAY = 'X'
      i_bypassing_buffer     'X'
*     I_INTERNAL_TABNAME     =
    CHANGING
      ct_fieldcat            gt_fieldcat
    EXCEPTIONS
      inconsistent_interface 1
      program_error          2
      OTHERS                 3.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  prepare_layout
*&---------------------------------------------------------------------*
FORM prepare_layout .
  gs_layout-zebra      'X' .
  gs_layout-cwidth_opt 'X' .
  gs_layout-no_merging ' ' .
*  gs_layout-box_fname  = 'LMARK'.
ENDFORM.                    " prepare_layout
*&---------------------------------------------------------------------*
*&      Form  display_alv
*&---------------------------------------------------------------------*
FORM display_alv .

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       sy-repid
      is_layout_lvc            gs_layout
      it_fieldcat_lvc          gt_fieldcat
      it_sort_lvc              gt_sort
      i_callback_user_command  'USER_COMMAND'
      i_callback_top_of_page   gc_form
      i_callback_pf_status_set 'PF_STATUS_SET'
    TABLES
      t_outtab                 gt_out
    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
*---------------------------------------------------------------------*
*       FORM USER_COMMAND                                             *
*---------------------------------------------------------------------*
FORM user_command USING u_ucomm     LIKE sy-ucomm
                        us_selfield TYPE slis_selfield.     "#EC CALLED

  CASE u_ucomm.
*    WHEN '&ICRT'.
*      PERFORM create_invoice.
  ENDCASE.

ENDFORM.                               " USER_COMMAND
*---------------------------------------------------------------------*
*       FORM PF_STATUS_SET                                            *
*---------------------------------------------------------------------*
FORM pf_status_set USING ut_extab TYPE slis_t_extab.        "#EC CALLED
  SET PF-STATUS 'STANDARD_FULLSCREEN' OF PROGRAM 'SAPLKKBL'.
ENDFORM.                               " PF_STATUS_SET
*&---------------------------------------------------------------------*
*&      Form  TOP_OF_PAGE
*&---------------------------------------------------------------------*
FORM top_of_page .

  DATA tarih1(10)tarih2(10)saat(8)info(30.

  DATA lt_header TYPE slis_t_listheader WITH HEADER LINE.
  DESCRIBE TABLE gt_out LINES sy-tfill.

  CLEAR lt_header[] .

  lt_header-typ  'H'.
  lt_header-key  ''.
  lt_header-info sy-title.
  APPEND lt_headerCLEAR lt_header.

  CONCATENATE sy-datum+6(2'.'
              sy-datum+4(2'.'
              sy-datum+0(4INTO tarih1.

  CONCATENATE sy-uzeit+0(2':'
              sy-uzeit+2(2':'
              sy-uzeit+4(2INTO saat.

  CONCATENATE tarih1 '/' saat INTO info SEPARATED BY space.

  lt_header-typ  'S'.
*  lt_header-key  = text-010.
  lt_header-info info .
  APPEND lt_headerCLEAR lt_header.


  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary lt_header[].


ENDFORM.                    " top_head

Yorumlar

Bu blogdaki popüler yayınlar

Transaction Code assignment for Query created in SQVI - SQVI Raporu Tcode Bağlanması