<style type="text/css"> SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF; } .L1S31 { font-style: italic; color: #808080; } .L1S32 { color: #3399FF; } .L1S33 { color: #4DA619; } .L1S52 { color: #0000FF; } </style> *&---------------------------------------------------------------------*
*& Report  ZTEST_HJKIM3                                                *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  ztest_hjkim3                                                .
TABLES : sflight, sbook.

DATA : gv_tabname TYPE dd02l-tabname.

DATA : gt_fcat TYPE lvc_t_fcat.

DATA : dyn_disp TYPE REF TO data,
       dyn_tab  TYPE REF TO data,
       dyn_disp_wa TYPE REF TO data,
       dyn_tab_wa TYPE REF TO data.

FIELD-SYMBOLS : <dyn_disp> TYPE STANDARD TABLE,
                <dyn_tab>  TYPE STANDARD TABLE,
                <dyn_disp_wa>,
                <dyn_tab_wa>,
                <fs>.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS : s_carids FOR sflight-carrid MODIF ID m1,
                 s_conids FOR sflight-connid MODIF ID m1,
                 s_fldate FOR sflight-fldate MODIF ID m1,
                 s_ptype FOR sflight-planetype MODIF ID m1.
SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
SELECT-OPTIONS : s_caridb FOR sbook-carrid MODIF ID m2,
                 s_conidb FOR sbook-carrid MODIF ID m2,
                 s_bookid FOR sbook-bookid MODIF ID m2.
SELECTION-SCREEN END OF BLOCK b2.

SELECTION-SCREEN BEGIN OF BLOCK b99 WITH FRAME TITLE text-099
                                                          NO INTERVALS.
PARAMETERS : p_1 RADIOBUTTON GROUP rad1 USER-COMMAND rad1,
             p_2 RADIOBUTTON GROUP rad1.
SELECTION-SCREEN END OF BLOCK b99.

INITIALIZATION.
  p_1 = 'X'.

AT SELECTION-SCREEN OUTPUT.
  IF p_1 = 'X'.
    gv_tabname = 'SFLIGHT'.
    LOOP AT SCREEN.
      IF screen-group1 = 'M1'.
        screen-active = 1.
      ELSEIF screen-group1 = 'M2'.
        screen-active = 0.
      ENDIF.
      MODIFY SCREEN.
    ENDLOOP.
  ELSE.
    gv_tabname = 'SBOOK'.
    LOOP AT SCREEN.
      IF screen-group1 = 'M1'.
        screen-active = 0.
      ELSEIF screen-group1 = 'M2'.
        screen-active = 1.
      ENDIF.
      MODIFY SCREEN.
    ENDLOOP.
  ENDIF.

START-OF-SELECTION.
  PERFORM create_dyntab.
  PERFORM select_data.
  PERFORM display.

*&---------------------------------------------------------------------*
*&      Form  create_dyntab
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM create_dyntab.
*- Create Display dyn tab
  REFRESH gt_fcat[].
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = gv_tabname
    CHANGING
      ct_fieldcat            = gt_fcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.

  SORT gt_fcat BY fieldname.

*-Create Data dyn tab
  REFRESH gt_fcat[].
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = gv_tabname
    CHANGING
      ct_fieldcat            = gt_fcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = gt_fcat[]
    IMPORTING
      ep_table        = dyn_tab.

  ASSIGN dyn_tab->* TO <dyn_tab>.
  CREATE DATA dyn_tab_wa LIKE LINE OF <dyn_tab>.
  ASSIGN dyn_tab_wa->* TO <dyn_tab_wa>.

  PERFORM set_fcat  TABLES gt_fcat
                    USING :
        'S' 'FIELDNAME' 'E_CODE',
        'E' 'NO_OUT'    'X',

        'S' 'FIELDNAME' 'LIGHT',
        'E' 'NO_OUT'    'X'.

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = gt_fcat[]
    IMPORTING
      ep_table        = dyn_disp.

  ASSIGN dyn_disp->* TO <dyn_disp>.
  CREATE DATA dyn_disp_wa LIKE LINE OF <dyn_disp>.
  ASSIGN dyn_disp_wa->* TO <dyn_disp_wa>.
ENDFORM.                    "create_dyntab
*&---------------------------------------------------------------------*
*&      Form  set_fcat
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->T_FCAT     text
*      -->P_GUB      text
*      -->P_FNAME    text
*      -->P_CON      text
*----------------------------------------------------------------------*
FORM set_fcat       TABLES t_fcat TYPE lvc_t_fcat
                    USING    p_gub
                             p_fname
                             p_con.

  STATICS : lv_tabix TYPE sy-tabix,
            lv_subrc TYPE sy-subrc,
            ls_fcat LIKE LINE OF t_fcat.

  DATA l_col(40).

  FIELD-SYMBOLS <fs>.

  IF p_gub = 'S'.
    CLEAR ls_fcat.
  ENDIF.

  CONCATENATE 'LS_FCAT-' p_fname INTO l_col.

  ASSIGN  (l_col)  TO  <fs>.
  MOVE     p_con   TO  <fs>.

  IF p_gub = 'S'.
    CLEAR : lv_tabix, lv_subrc.
    READ TABLE t_fcat INTO ls_fcat WITH KEY
                                    fieldname = ls_fcat-fieldname.
    lv_tabix = sy-tabix.
    lv_subrc = sy-subrc.
  ENDIF.

  IF p_gub = 'E'.
    IF lv_subrc EQ 0.
      MODIFY t_fcat FROM ls_fcat INDEX lv_tabix.
    ELSE.
      APPEND ls_fcat TO t_fcat.
    ENDIF.
  ENDIF.

ENDFORM.                    " MAKE_FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  select_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM select_data.
  TYPE-POOLS : rsds.

  DATA : lt_trange TYPE rsds_trange,
         ls_trange LIKE LINE OF lt_trange,
         ls_field TYPE rsds_frange,
         ls_condi TYPE rsdsselopt.

  DATA : lt_where TYPE rsds_twhere,
         ls_where LIKE LINE OF lt_where.

  IF NOT p_1 IS INITIAL.
    PERFORM set_where TABLES lt_trange USING 'CARRID' 'S_CARIDS'.
    PERFORM set_where TABLES lt_trange USING 'CONNID' 'S_CONIDS'.
    PERFORM set_where TABLES lt_trange USING 'FLDATE' 'S_FLDATE'.
    PERFORM set_where TABLES lt_trange USING 'PLANETYPE' 'S_PTYPE'.
  ELSE.
    PERFORM set_where TABLES lt_trange USING 'CARRID' 'S_CARIDB'.
    PERFORM set_where TABLES lt_trange USING 'CONNID' 'S_CONIDB'.
    PERFORM set_where TABLES lt_trange USING 'BOOKID' 'S_BOOKID'.
  ENDIF.

  CALL FUNCTION 'FREE_SELECTIONS_RANGE_2_WHERE'
    EXPORTING
      field_ranges  = lt_trange
    IMPORTING
      where_clauses = lt_where.

  READ TABLE lt_where INTO ls_where WITH KEY tablename = gv_tabname.

  SELECT *
    INTO CORRESPONDING FIELDS OF TABLE <dyn_tab>
    FROM (gv_tabname)
  WHERE (ls_where-where_tab).

  LOOP AT <dyn_tab> INTO <dyn_tab_wa>.
    CLEAR <dyn_disp_wa>.
    MOVE-CORRESPONDING <dyn_tab_wa> TO <dyn_disp_wa>.
    APPEND <dyn_disp_wa> TO <dyn_disp>.
  ENDLOOP.

ENDFORM.                    "select_data
*&---------------------------------------------------------------------*
*&      Form  set_where
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_FIELD    text
*----------------------------------------------------------------------*
FORM set_where  TABLES t_tab TYPE rsds_trange
                USING p_dbfield p_field.

  DATA : lt_trange TYPE rsds_trange,
         ls_trange LIKE LINE OF lt_trange,
         ls_field TYPE rsds_frange,
         ls_condi TYPE rsdsselopt.

  DATA : lt_where TYPE rsds_twhere,
         ls_where LIKE LINE OF lt_where.

  DATA : lv_constring TYPE string.

  DATA : wa TYPE REF TO data,
         ldyn_wa TYPE REF TO data,
         ldyn_tab TYPE REF TO data .

  DATA : lt_fcat TYPE lvc_t_fcat.

  FIELD-SYMBOLS : <wa>,
                  <sel> TYPE ANY TABLE,
                  <ldyn_wa>,
                  <ldyn_tab> TYPE STANDARD TABLE.

  CONCATENATE p_field '[]' INTO lv_constring.
  ASSIGN (lv_constring) TO <sel>.
  CREATE DATA wa LIKE LINE OF <sel>.
  ASSIGN wa->* TO <wa>.

  CLEAR ls_trange.
  ls_trange-tablename = gv_tabname.
  ls_field-fieldname = p_dbfield.

  IF NOT <sel>[] IS INITIAL.
    LOOP AT <sel> INTO <wa>.
      MOVE-CORRESPONDING <wa> TO ls_condi.
      APPEND ls_condi TO ls_field-selopt_t.
    ENDLOOP.
  ENDIF.

  APPEND ls_field TO ls_trange-frange_t.
  APPEND ls_trange TO t_tab.

ENDFORM.                    "set_where
*&---------------------------------------------------------------------*
*&      Form  display
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM display.
  DATA : ls_layout   TYPE lvc_s_layo.

  ls_layout-cwidth_opt = 'X'.
  ls_layout-zebra      = 'X'.
  ls_layout-excp_fname = 'LIGHT'.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
*      i_structure_name = 'SFLIGHT'
      is_layout_lvc    = ls_layout
      it_fieldcat_lvc  = gt_fcat[]
    TABLES
      t_outtab         = <dyn_disp>
    EXCEPTIONS
      program_error    = 1
      OTHERS           = 2.
ENDFORM.                    "display

 

보완사항.

1. select-options dynamic.

2. where 절 field 명 dynamic.

 

2가지가 조금 걸리네요...