메뉴 건너뛰기

SAP 한국 커뮤니티



Dynamic Select 소스

떡밥 2011.06.04 00:12 조회 수 : 17828

  <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가지가 조금 걸리네요...

번호 제목 글쓴이 날짜 조회 수
107 KR_POSTCODE : 한국주소입력 Version 2.0 대략나 2021.03.31 2201
106 HTTP 통신하는 로직 [3] 대략나 2016.12.20 5915
105 XML 을 아밥 스트럭쳐 또는 인터널테이블로 변경시키는 로직 [1] 대략나 2016.12.20 3918
104 ALV EDIT 샘플 프로그램 sapjoy 2015.06.26 9323
103 파일 메일(e-mail) 첨부로 보내기 [2] sapjoy 2014.10.10 7370
102 데이터 이메일(e-mail) xls 파일로 보내기 [2] sapjoy 2014.10.10 6730
101 Tree ALV 샘플 [5] 양키(이경환) 2014.02.05 10671
100 Split 의 사용 [1] 양키(이경환) 2014.02.05 11236
99 소수점 뒷자리 0 제거 로직 file 양키(이경환) 2014.01.20 9619
98 10이하의 홀수 마방진 은미짱 2013.10.19 6830
97 [Module pool] Container에 webpage 넣기. [2] 냥냥 2013.03.20 11238
96 COMAPY CODE에 할당된 Fiscal variant에 대한 calendar date 변환 paran 2013.03.01 10505
95 멀 어떻게 해야 하는건질 몰라 질문 드립니다 [5] 쏠라맨 2012.10.13 11704
94 nugg 프로그램을 sap 에 생성하기.. [3] file MadMax 2012.10.11 10891
93 인터널 테이블을 사용한 구구단 출력 두가지 입니다 - WRITE, ALV 이용 [1] kofnhuge 2012.10.10 14127
92 Search For Program/Function/Table Desc&Name [8] file 초짜 2011.09.30 9863
91 스크린에서 버튼을 tree 처럼 만들기. [32] file Jenny 2011.08.12 17707
90 통화단위, krw, usd 일반 필드에 변환시 로직(소수점, 자리수) [4] sapjoy 2011.07.20 15638
» Dynamic Select 소스 [5] 떡밥 2011.06.04 17828
88 [onepaper] 참고자료 Archiving 세팅에 필요한 파일소스입니다. [7] file 원니컴 2011.03.22 13832