IT WORK/SAP FI and ABAP

[ABAP] ALV GRID에 SEARCH HELP 추가하기 How to add search help on ALV GRID

Bathildis 2016. 5. 17. 10:37
반응형
CLASS  LCL_EVENT_RECEIVER  DEFINITION.

  PUBLIC SECTION.
    METHODS :
      HANDLE_HELP_F4 FOR EVENT ONF4 OF CL_GUI_ALV_GRID
                IMPORTING
                  SENDER
                  E_FIELDNAME
                  E_FIELDVALUE
                  ES_ROW_NO
                  ER_EVENT_DATA
                  ET_BAD_CELLS
                  E_DISPLAY.

ENDCLASS.

CLASS  LCL_EVENT_RECEIVER  IMPLEMENTATION.
  METHOD HANDLE_HELP_F4.
     PERFORM HANDLE_HELP_F4 USING SENDER
                                  E_FIELDNAME
                                  E_FIELDVALUE
                                  ES_ROW_NO
                                  ER_EVENT_DATA
                                  ET_BAD_CELLS
                                  E_DISPLAY.
   ENDMETHOD. "HANDLE_HELP_F4

ENDCLASS.

1. CLASS 선언부에 Search Help Event를 등록한다.

1. Regist Search Help Event in Class Definition part

FORM set_event_handler USING p_grid TYPE REF TO cl_gui_alv_grid.
  CREATE OBJECT event_receiver.
  SET HANDLER : event_receiver->handle_help_f4       FOR p_grid.
ENDFORM.

2. Event Handler에 Search Help Event를 추가한다.

2. Add Search Help Event in Even Handler

FORM set_search_help.
  CLEAR : gs_f4, gt_f4, gt_f4[].
* 먼저 이곳에서 서치헬프를 하고자 하는 필드를 지정해준다.
* Set field you want to use Search Help
  gs_f4-fieldname = 'KOSTL'.
  gs_f4-register  = 'X'.
  INSERT gs_f4 INTO TABLE gt_f4.

  gs_f4-fieldname = 'MWSKZ'.
  gs_f4-register  = 'X'.
  INSERT gs_f4 INTO TABLE gt_f4.

* 화면의 필드를 G_GRID에 인스턴스 해준다.
* Instance the field on screen to G_GRID
  CALL METHOD gl_100grid->register_f4_for_fields
    EXPORTING
      it_f4 = gt_f4.
ENDFORM.

3. PBO를 Build하는 부분에서 Search Help 사용할 필드를 지정해준다.

3. Set the field you want to use Search Help in PBO 

FORM set_build_fieldcat .

  DATA : lt_fieldcat TYPE slis_t_fieldcat_alv.
  DATA : lt_alv_cat  TYPE TABLE OF lvc_s_fcat.

  DATA : l_table  LIKE dd02l-tabname,
         l_field  TYPE char50,
         l_colpos TYPE lvc_colpos,
         lt_fcat  TYPE lvc_t_fcat.

  FIELD-SYMBOLS  TYPE lvc_s_fcat.

  __clear : lt_fieldcat, gt_fieldcat.

  l_table = 'GS_LIST'.

  SET PARAMETER ID 'ALVBUFFER' FIELD sy-datum.

  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_program_name     = c_repid
      i_internal_tabname = l_table
      i_inclname         = c_repid
    CHANGING
      ct_fieldcat        = lt_fieldcat[].

  CHECK lt_fieldcat[] IS NOT INITIAL.

  CALL FUNCTION 'LVC_TRANSFER_FROM_SLIS'
    EXPORTING
      it_fieldcat_alv = lt_fieldcat
    IMPORTING
      et_fieldcat_lvc = gt_fieldcat
    TABLES
      it_data         = gt_list[].

  LOOP AT gt_fieldcat ASSIGNING .
    CLEAR : l_field.

    IF -fieldname = 'ICON'.
      l_field = '상태'.
      IF r_1 EQ c_x.
        -no_out  = c_x.
      ENDIF.

    ELSEIF -fieldname = 'BUKRS'.
      l_field        = '회사코드'.
      -key = c_off.

    ELSEIF -fieldname = 'MWSKZ'.
      l_field             = '세금'.
      f4availabl = c_x.

    ELSEIF -fieldname = 'KOSTL'.
      l_field         = '요청부서'.
      -f4availabl = c_x.

    ELSE.
      CONTINUE.
    ENDIF.

    IF l_field IS NOT INITIAL.
      -reptext   = l_field.
      -seltext   = l_field.
      -scrtext_s = l_field.
      -scrtext_m = l_field.
      -scrtext_l = l_field.
    ENDIF.

    APPEND  TO lt_fcat.
  ENDLOOP.

  REFRESH gt_fieldcat.
  gt_fieldcat[] = lt_fcat[].

ENDFORM.

4. Field Catalog를 생성하는 부분에서 Search Help 를 사용할 컬럼 값에 F4AVAILABL에 'X' 값을 준다.

4. Give 'X' to F4VAILABLE to the field in Field Catalog 

FORM handle_help_f4  USING    sender
                   e_fieldname   TYPE lvc_fname
                   e_fieldvalue  TYPE lvc_value
                   es_row_no     TYPE lvc_s_roid
                   er_event_data TYPE REF TO cl_alv_event_data
                   et_bad_cells  TYPE lvc_t_modi
                   e_display     TYPE char01.

  DATA :  lt_return LIKE ddshretval OCCURS 0 WITH HEADER LINE.
  DATA :  is_modi TYPE lvc_s_modi.
  FIELD-SYMBOLS  TYPE lvc_t_modi.
  ASSIGN er_event_data->m_data->* TO .

* 현재 라인의 라인번호와 필드 이름 가져오기
* Get line number and field name of current line
  DATA: l_es_row_id TYPE lvc_s_row,
        l_es_col_id TYPE lvc_s_col,
        l_es_row_no TYPE lvc_s_roid.

  DATA : BEGIN OF lt_kostl OCCURS 0,
           kostl TYPE cskt-kostl,
           ktext TYPE cskt-ktext,
         END OF lt_kostl.

  DATA : BEGIN OF lt_mwskz OCCURS 0,
           mwskz TYPE t007s-mwskz,
           text1 TYPE t007s-text1,
         END OF lt_mwskz.

  DATA : lt_f4 LIKE TABLE OF ddshretval WITH HEADER LINE.

  CLEAR: l_es_row_id, l_es_col_id, l_es_row_no,
         lt_kostl,    lt_kostl[],
         lt_mwskz,    lt_mwskz[].

  CASE E_FIELDNAME.
    WHEN 'KOSTL'.

      SELECT kostl ktext INTO TABLE lt_kostl
        FROM cskt
       WHERE spras EQ sy-langu
         AND kokrs EQ c_kokrs.

      SORT lt_kostl BY kostl.
      DELETE ADJACENT DUPLICATES FROM lt_kostl COMPARING kostl.

      CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
        EXPORTING
          retfield     = 'KOSTL'
          dynpprog     = sy-cprog
          dynpnr       = sy-dynnr
          dynprofield  = 'CSKT-KOSTL'
          window_title = '코스트센터'
          value_org    = 'S'
        TABLES
          value_tab    = lt_kostl
          return_tab   = lt_f4.

      IF e_display = ''.
        " E_DISPLAY = '' -> 활성화일때. E_DISPLAY = 'X'면 비활성화
        " E_DISPLAY = '' -> Activated. E_DISPLAY = 'X' -> Deactivated
        is_modi-row_id = es_row_no-row_id.
        is_modi-fieldname = 'KOSTL'.
        is_modi-value = lt_f4-fieldval.
        APPEND is_modi TO .
      ENDIF.
      er_event_data->m_event_handled = 'X'.

    WHEN 'MWSKZ'.

      SELECT mwskz text1 INTO TABLE lt_mwskz
        FROM t007s
       WHERE spras EQ sy-langu
         AND kalsm EQ 'TAXKR'
         AND mwskz BETWEEN 'V1' AND 'V9'
         AND mwskz NE 'V8'.

      SORT lt_mwskz BY mwskz.
      DELETE ADJACENT DUPLICATES FROM lt_mwskz COMPARING mwskz.

      CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
        EXPORTING
          retfield     = 'MWSKZ'
          dynpprog     = sy-cprog
          dynpnr       = sy-dynnr
          dynprofield  = 'T007S-MWSKZ'
          window_title = '세금코드'
          value_org    = 'S'
        TABLES
          value_tab    = lt_mwskz
          return_tab   = lt_f4.

      IF e_display = ''.
        " E_DISPLAY = '' -> 활성화일때. E_DISPLAY = 'X'면 비활성화
        is_modi-row_id = es_row_no-row_id.
        is_modi-fieldname = 'MWSKZ'.
        is_modi-value = lt_f4-fieldval.
        APPEND is_modi TO .
      ENDIF.
      er_event_data->m_event_handled = 'X'.

  ENDCASE.

ENDFORM.

5. 마지막으로 Handle_help_f4 부분에 소스 채워주면 끝. E_FIELDNAME으로 구분하면 여러 컬럼에 Search Help를 달 수 있다.

5. Add logic in Handle_help_f4 form. If you use E_FIELDNAME you can add Search Help to several fields.

 

반응형

 

반응형