SAP/ABAP

Custom Search Help in ALV OOP

Denise 2013. 1. 23. 15:57
1. Field Catalog Setting
      WHEN 'LIFNR2'.
        gs_fieldcat-edit = 'X'.
        gs_fieldcat-f4availabl = 'X'.

      WHEN 'ZADDR2'.
        gs_fieldcat-edit = 'X'.
        gs_fieldcat-f4availabl = 'X'.
2. Event receiver setting.
      handle_f4 FOR EVENT onf4 OF cl_gui_alv_grid
        IMPORTING e_fieldname
                  es_row_no
                  er_event_data
                  et_bad_cells
                  e_display,
3. Method Implementation
  METHOD handle_f4.
    PERFORM on_f4 USING e_fieldname
                        es_row_no-row_id
                        er_event_data
                        et_bad_cells
                        e_display.
  ENDMETHOD.                                                "handle_f4
4. Business logic implementation
*&---------------------------------------------------------------------*
*&      Form  on_f4
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_FIELDNAME    text
*      -->ROW_ID         text
*      -->PR_EVENT_DATA  text
*      -->PT_BAD_CELLS   text
*      -->P_DISPLAY      text
*----------------------------------------------------------------------*
FORM on_f4  USING    p_fieldname
                     row_id
                     pr_event_data TYPE REF TO cl_alv_event_data
                     pt_bad_cells
                     p_display.

  DATA : lt_return        TYPE TABLE OF ddshretval WITH HEADER LINE,
         lv_field_in_list TYPE dfies-fieldname.

*--- 업체 Search Help.
  DATA : BEGIN OF lt_lifnr OCCURS 0,
           lifnr LIKE zstt9020-lifnr,
           name1 LIKE lfa1-name1,
         END OF lt_lifnr.

*--- 주소 순번 Search Help.
  DATA : BEGIN OF lt_zaddr OCCURS 0,
            lifnr   LIKE zstt9020-lifnr  ,
            zaddr   LIKE zstt9020-zaddr  ,
            land1   LIKE zstt9020-land1  ,
            zstate  LIKE zstt9020-zstate ,
            zcity   LIKE zstt9020-zcity  ,
            zstreet LIKE zstt9020-zstreet,
            zhnum1  LIKE zstt9020-zhnum1 ,
            zhnum2  LIKE zstt9020-zhnum2 ,
            zbldng  LIKE zstt9020-zbldng ,
            zdcomp  LIKE zstt9020-zdcomp ,
            zpocod  LIKE zstt9020-zpocod ,
            telnum  LIKE zstt9020-telnum ,
            faxnum  LIKE zstt9020-faxnum ,
            clerk   LIKE zstt9020-clerk  ,
         END OF lt_zaddr.

  FIELD-SYMBOLS : <fs_values> TYPE STANDARD TABLE.
  DATA : ls_mod_cell  TYPE lvc_s_modi,
         lv_value     TYPE lvc_value .
  DATA : ls_alv       LIKE LINE OF gt_alv.

  FIELD-SYMBOLS: <itab>   TYPE lvc_t_modi.

  CASE p_fieldname.   "read changed cell
    WHEN 'LIFNR2'.
      PERFORM get_lifnr_list CHANGING lt_lifnr[].

      lv_field_in_list = 'LIFNR'.
      ASSIGN lt_lifnr[] TO <fs_values>.

    WHEN 'ZADDR2'.
      READ TABLE gt_alv INDEX row_id.

      IF sy-subrc IS INITIAL
        AND gt_alv-lifnr2 IS NOT INITIAL.

        PERFORM get_zaddr_list USING    gt_alv-lifnr2
                               CHANGING lt_zaddr[].

        lv_field_in_list = 'ZADDR'.
        ASSIGN lt_zaddr[] TO <fs_values>.
      ELSE.
        pr_event_data->m_event_handled = 'X'.
        EXIT.
      ENDIF.

  ENDCASE.

  CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
    EXPORTING
      retfield        = lv_field_in_list
      value_org       = 'S'
      dynpprog        = sy-repid
      dynpnr          = sy-dynnr
    TABLES
      value_tab       = <fs_values>
      return_tab      = lt_return
    EXCEPTIONS
      parameter_error = 1
      no_values_found = 2
      OTHERS          = 3.

  pr_event_data->m_event_handled = 'X'.
  CHECK sy-subrc IS INITIAL AND lt_return-fieldval IS NOT INITIAL.

  ASSIGN pr_event_data->m_data->* TO <itab>.

  ls_mod_cell-row_id    = row_id.
  ls_mod_cell-fieldname = p_fieldname.
  ls_mod_cell-value     = lt_return-fieldval.

  APPEND ls_mod_cell TO <itab>.

ENDFORM.                                                    " ON_F4
5. Event Handler Setting
  SET HANDLER g_event_receiver1->handle_f4            FOR g_grid1.
6. Register Fields for search help
  DATA : lt_f4 TYPE lvc_t_f4,
         ls_f4 TYPE lvc_s_f4.

  DEFINE $_set_onf4.

    ls_f4-fieldname  = &1.
    ls_f4-register   = 'X'.
    ls_f4-chngeafter = 'X'.

    append ls_f4 to lt_f4.
    clear ls_f4.
  END-OF-DEFINITION.

  $_set_onf4 : 'LIFNR2', 'ZADDR2'.

  CALL METHOD g_grid1->register_f4_for_fields
    EXPORTING
      it_f4 = lt_f4.
# 테이블의 Search table와 ALV에서 선언한 경우 ALV의 search help가 우선시. 단. F4IF_INT_TABLE_VALUE_REQUEST 호출시 DDIC_STRUCTURE 옵션을 주게 되면 ALV의 세팅을 무시하고 field에 걸린 searh help를 호출한다. pr_event_data->m_event_handled = 'X'. 가 체크되지 않으면 DH804 에러 발생. return 필드를 여러개 받고 싶은 경우에는 http://kidoel.tistory.com/22 참조.

'SAP > ABAP' 카테고리의 다른 글

Chartset 변경.  (0) 2014.01.15
MM 단위 환산.  (0) 2013.03.14
Excel OLE constants  (0) 2012.12.03
BAPI_SALESORDER_CHANGE  (0) 2012.09.12
An Easy Reference for ALV Grid Control  (0) 2012.09.06