Friday, 31 March 2017

ALV Grid With Field Symbols

Displaying ALV with using Field Symbols

*&---------------------------------------------------------------------*
*& Report  ZDEMO11
*&
*&---------------------------------------------------------------------*
*&
*&  ALV Display Using Field Symbols
*&---------------------------------------------------------------------*

REPORT zdemo11.

INITIALIZATION.

  TYPES: BEGIN OF gty_emp,
          id TYPE char50,
          name TYPE char50,
          ph TYPE char50,
         END OF gty_emp.

  DATA: gt_emp TYPE TABLE OF gty_emp,
        gv_ref_data TYPE REF TO data.

START-OF-SELECTION.
  PERFORM populate_record.
  PERFORM display_alv.


*&---------------------------------------------------------------------*
*&      Form  POPULATE_RECORD
*&---------------------------------------------------------------------*
*       POPULATING THE RECORDS
*----------------------------------------------------------------------*
FORM populate_record .

  FIELD-SYMBOLS: <fs_emp>    TYPE STANDARD TABLE,
                 <fs_wa_emp> LIKE LINE OF gt_emp.

  ASSIGN gt_emp[] TO <fs_emp>.

  CREATE DATA gv_ref_data  LIKE LINE OF gt_emp.
  ASSIGN gv_ref_data->* TO <fs_wa_emp>.

  <fs_wa_emp>-id    = '1'.
  <fs_wa_emp>-name  = 'ROY MILLER'.
  <fs_wa_emp>-ph    = '18363864'.

  APPEND <fs_wa_emp> TO <fs_emp>.

ENDFORM.                    " POPULATE_RECORD

*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV
*&---------------------------------------------------------------------*
*       DISPLAYING ALV
*----------------------------------------------------------------------*
FORM display_alv .

  DATA: lt_fcat   TYPE slis_t_fieldcat_alv,
        ls_fcat   TYPE slis_fieldcat_alv,
        ls_layout TYPE slis_layout_alv,
        lv_count TYPE i VALUE 1
        .
  CLEAR: ls_fcat,
         ls_layout.

  FIELD-SYMBOLS: <fs_wa_fcat> TYPE slis_fieldcat_alv,
                 <fs_fcat> TYPE STANDARD TABLE.

  CREATE DATA gv_ref_data LIKE LINE OF lt_fcat.
  ASSIGN gv_ref_data->* TO <fs_wa_fcat>.
  ASSIGN lt_fcat[] TO <fs_fcat>.

  <fs_wa_fcat>-fieldname = 'ID'.
  <fs_wa_fcat>-seltext_m = 'Emp ID'.
  <fs_wa_fcat>-col_pos = lv_count.
  APPEND <fs_wa_fcat> TO <fs_fcat>.
  lv_count = lv_count + 1.
  CLEAR: <fs_wa_fcat>.

  <fs_wa_fcat>-fieldname = 'NAME'.
  <fs_wa_fcat>-seltext_m = 'Emp Name'.
  <fs_wa_fcat>-col_pos = lv_count.
  APPEND <fs_wa_fcat> TO <fs_fcat>.
  lv_count = lv_count + 1.
  CLEAR: <fs_wa_fcat>.

  <fs_wa_fcat>-fieldname = 'PH'.
  <fs_wa_fcat>-seltext_m = 'Emp Ph'.
  <fs_wa_fcat>-col_pos = lv_count.
  APPEND <fs_wa_fcat> TO <fs_fcat>.
  lv_count = lv_count + 1.
  CLEAR: <fs_wa_fcat>.

  ls_layout-colwidth_optimize = 'X'.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = sy-repid
      is_layout          = ls_layout
      it_fieldcat        = lt_fcat
    TABLES
      t_outtab           = gt_emp
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
  IF sy-subrc <> 0.
*   Implement suitable error handling here
  ENDIF.

ENDFORM.                    " DISPLAY_ALV

No comments:

Post a Comment

Report to find CDS view of Standard Table

A small change has been made to the original program ( SAP YARD Article ) so that it can also display the common CDS used by multiple table...