본문 바로가기

SAP/ABAP

ALV+SearchHelp+SelectOption(1/3)

최초 작성일: 2024-05-14일

목표: 원하는 테이블의 값을 뽑아내는 프로그램 자유롭게 만들어 내기

 

여기서 포인트는 아래 alv 구조에서 'SELECTION-SCREEN' - 'SELECT-OPTION' - 'INITIALIZATION' 구문을 통해

1) 데이터 뷰를 보여주기전에 해당 데이터 뷰를 필터링 할 수 있는 SELECT-OPTION을 줄 수 있다는 것이고

2) 이때 셀렉트 한 값이 최종 조회 값으로 보여주기 위해서는 PERFORM 구문인 ' Data Retrieval' 혹은 'get data'를 통해서

select 옵션을 수정해줘야함

 

추가로 해야하는 

multiple selection option에서 상한값, 하한값 설정 하는 방법 확인 필요

 

[alv 구조]

 

report

type-pools

data 선언부

selection-screen

select- option

initialization

start-of selection

form

 

 

 

[소스코드]


*&---------------------------------------------------------------------*
*& Report ZTEST_SPFLI
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZTEST_SPFLI.
TABLES:     SPFLI.

TYPE-POOLS: slis.                                 "ALV Declarations

*Data Declaration
*----------------
TYPES: BEGIN OF t_SPFLI,
  MANDT TYPE SPFLI-MANDT,
  CARRID TYPE SPFLI-CARRID,
  CONNID TYPE SPFLI-CONNID,
  COUNTRYFR TYPE SPFLI-COUNTRYFR,
  CITYFROM TYPE SPFLI-CITYFROM,
  AIRPFROM TYPE SPFLI-AIRPFROM,
  COUNTRYTO TYPE SPFLI-COUNTRYTO,
  CITYTO TYPE SPFLI-CITYTO,
  AIRPTO TYPE SPFLI-AIRPTO,
  FLTIME TYPE SPFLI-FLTIME,
  DEPTIME TYPE SPFLI-DEPTIME,
  ARRTIME TYPE SPFLI-ARRTIME,
  DISTANCE TYPE SPFLI-DISTANCE,
  DISTID TYPE SPFLI-DISTID,
  FLTYPE TYPE SPFLI-FLTYPE,
  PERIOD TYPE SPFLI-PERIOD,
 END OF t_SPFLI.

DATA: it_SPFLI TYPE STANDARD TABLE OF t_SPFLI INITIAL SIZE 0,
      wa_spfli TYPE t_spfli.

*ALV data declarations
DATA: fieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      gd_tab_group TYPE slis_t_sp_group_alv,
      gd_layout    TYPE slis_layout_alv,
      gd_repid     LIKE sy-repid.


DATA : t TYPE slis_t_sp_group_alv .

TABLES : ZSPFLI04.

SELECTION-SCREEN BEGIN OF BLOCK part8 WITH FRAME TITLE text-008.
SELECT-OPTIONS b_ctryto FOR ZSPFLI04-countryto DEFAULT 'JP'.
SELECTION-SCREEN SKIP.
SELECT-OPTIONS b_CITYTO FOR ZSPFLI04-CITYTO.
SELECT-OPTIONS b_airpto for ZSPFLI04-AIRPTO.

SELECTION-SCREEN END OF BLOCK part8.

INITIALIZATION.

************************************************************************
*Start-of-selection.
START-OF-SELECTION.

  PERFORM data_retrieval.
  PERFORM build_fieldcatalog.
  PERFORM build_layout.
  PERFORM display_alv_report.


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*       Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*

FORM build_fieldcatalog.

  fieldcatalog-fieldname   = 'MANDT'.
  fieldcatalog-seltext_m   = 'Client'.
  fieldcatalog-col_pos     = 0.
  fieldcatalog-outputlen   = 10.
  fieldcatalog-INTTYPE = 'C'.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'CARRID'.
  fieldcatalog-seltext_m   = 'Airline Code'.
  fieldcatalog-col_pos     = 1.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'CONNID'.
  fieldcatalog-seltext_m   = 'Flight Connection Number'.
  fieldcatalog-col_pos     = 2.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'COUNTRYFR'.
  fieldcatalog-seltext_m   = 'Country Key'.
  fieldcatalog-col_pos     = 3.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'CITYFROM'.
  fieldcatalog-seltext_m   = 'Departure city'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'AIRFROM'.
  fieldcatalog-seltext_m   = 'Departure airport'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'COUNTRYTO'.
  fieldcatalog-seltext_m   = 'Country Key'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'CITYTO'.
  fieldcatalog-seltext_m   = 'Arrival city'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'AIRTO'.
  fieldcatalog-seltext_m   = 'Destination airport'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'FLTIME'.
  fieldcatalog-seltext_m   = 'Flight time'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'DEPTIME'.
  fieldcatalog-seltext_m   = 'Departure time'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'ARRTIME'.
  fieldcatalog-seltext_m   = 'Arrival time'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'DISTANCE'.
  fieldcatalog-seltext_m   = 'Distance'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'DISTID'.
  fieldcatalog-seltext_m   = 'Mass unit of distance (kms,miles)'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'FLTYPE'.
  fieldcatalog-seltext_m   = 'Flight type'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   = 'PERIOD'.
  fieldcatalog-seltext_m   = 'Arrival n day(s)later'.
  fieldcatalog-col_pos     = 4.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.
ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
FORM build_layout.

  gd_layout-no_input          = 'X'.
  gd_layout-colwidth_optimize = 'X'.
  gd_layout-zebra = 'X'.
*  gd_layout-info_fieldname =      'LINE_COLOR'.
*  gd_layout-def_status = 'A'.

ENDFORM.                    " BUILD_LAYOUT


*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*       Display report using ALV grid
*----------------------------------------------------------------------*
FORM display_alv_report.
  gd_repid = sy-repid.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = gd_repid
      is_layout          = gd_layout
      it_fieldcat        = fieldcatalog[]
      i_save             = 'X'
    TABLES
      t_outtab           = it_spfli
    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_REPORT


*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------*
FORM data_retrieval.
  DATA: ld_color(1) TYPE c.

  SELECT mandt carrid connid countryfr cityfrom airpfrom countryto cityto airpto fltime deptime arrtime distance distid fltype period
   UP TO 10 ROWS
    FROM spfli
    INTO TABLE it_spfli
    WHERE countryto IN b_ctryto AND
          cityto IN b_cityto    AND
          airpto IN b_airpto.
ENDFORM.                    " DATA_RETRIEVAL

 

[출력화면]