How to implement F4 search help in OO ALV Grid

I have searched SDN myself for the answer for sometime. All the friends that answered were helpful but there were only example links. So here is a simple code to implement F4 search help for OO ALV grid cells:

First we have to define the method and sub programs. In the definition part of our events class, include a code as the following: 

CLASS lcl_event_handler DEFINITION .
  PUBLIC SECTION .
    METHODS:

….

    on_f4 FOR EVENT onf4 OF cl_gui_alv_grid
      IMPORTING e_fieldname
                es_row_no
                er_event_data
                et_bad_cells

                e_display.

….

ENDCLASS.  

For the implementation part, code can be as follows:
  CLASS lcl_event_handler IMPLEMENTATION .
…..
….
  METHOD on_f4.
    PERFORM on_f4 USING e_fieldname
                es_row_no-row_id
                er_event_data
                et_bad_cells
                e_display
                er_data_changed.
    er_event_data->m_event_handled = ‘X’.
  ENDMETHOD.                                                "on_f4
…..
…..
ENDCLASS . 

Within our main program after displaying the table (by method set_table_for_first_display), we have to create the event handler object and set the handlers for these events.
    CREATE OBJECT gr_event_handler .
*–Registering handler methods to handle ALV Grid events
…..
…..
    SET HANDLER gr_event_handler->on_f4 FOR gr_alvgrid .
    PERFORM register_f4_fields.    "set cells with search help
*§3.Optionally register ENTER to raise event DATA_CHANGED.
    call method gr_alvgrid->register_edit_event
      EXPORTING
        i_event_id = cl_gui_alv_grid=>mc_evt_modified.
…… ……. ……
FORM register_f4_fields.    "which fields will have F4 search help  DATA: lt_f4 TYPE lvc_t_f4 WITH HEADER LINE .
  DATA: lt_f4_data TYPE lvc_s_f4.
  lt_f4_data-fieldname = ‘CHARG’.
  lt_f4_data-register = ‘X’ .
*  lt_f4_data-getbefore = ‘X’ .
  lt_f4_data-CHNGEAFTER  =’X’.
  INSERT lt_f4_data INTO TABLE lt_f4.
  lt_f4_data-fieldname = ‘LGORT’.
  lt_f4_data-register = ‘X’ .
*  lt_f4_data-getbefore = ‘X’ .
  lt_f4_data-CHNGEAFTER  =’X’.
  INSERT lt_f4_data INTO TABLE lt_f4.
  CALL METHOD gr_alvgrid->register_f4_for_fields
    EXPORTING
      it_f4 = lt_f4[].
ENDFORM.                    "register_f4_fields

The main sub program where we handle the changed cell value is as follows:
FORM on_f4  USING    P_E_FIELDNAME
                     ROW_ID
                     P_ER_EVENT_DATA
                              TYPE REF TO CL_ALV_EVENT_DATA
                     P_ET_BAD_CELLS
                     P_E_DISPLAY
                     IR_DATA_CHANGED
                              TYPE REF TO cl_alv_changed_data_protocol.
  DATA: BEGIN OF value_charg OCCURS 0, "the value table that is passed to F4 fm
          charg like zpp_kpduzelt-charg,
          lgort like zpp_kpduzelt-lgort,
          clabs like mchb-clabs,
  END OF value_charg.
  DATA : ls_mod_cell TYPE lvc_s_modi ,
         ls_del_cell TYPE lvc_s_moce  ,
         ls_good_cell TYPE lvc_s_modi,
         lv_value TYPE lvc_value .
  DATA : ls_mod_row like line of gt_list.

…..,

…. *§5 define fields and field-symbols for data-update
  field-symbols: <itab> type lvc_t_modi.
  data: ls_modi type lvc_s_modi.
  create object ir_data_changed.
  SORT ir_data_changed->mt_mod_cells BY row_id .
  LOOP AT ir_data_changed->mt_mod_cells
  INTO ls_mod_cell.
  ENDLOOP.
  case p_e_fieldname.   "read changed cell
    when ‘CHARG’.      
……
* here must be the code to fill in the possible values table
* and the call to fm F4IF_INT_TABLE_VALUE_REQUEST
……
      CALL FUNCTION ‘F4IF_INT_TABLE_VALUE_REQUEST’
        EXPORTING
          retfield        = ‘CHARG’
          value_org       = ‘S’
*      DYNPPROG        = SY-REPID
*      DYNPNR          = SY-DYNNR
*      DYNPROFIELD     = ‘PRUEFLOS’
        TABLES
          value_tab       = value_charg
*          field_tab       = field_tab
          return_tab      = return_tab
        EXCEPTIONS
          parameter_error = 1
          no_values_found = 2
          OTHERS          = 3.
      IF sy-subrc = 0 and return_tab-fieldval <> ”.
        move return_tab-fieldval to charg.
        ls_mod_cell-row_id    = row_id.
        ls_mod_cell-fieldname = ‘CHARG’.
        move charg to lv_value.
        ls_mod_cell-value = lv_value.
        append ls_mod_cell to ir_data_changed->mt_mod_cells.
*******************
        call method gr_alvgrid->get_frontend_fieldcatalog
          IMPORTING
            et_fieldcatalog = lt_fcat.
        read table gt_list index row_id into wa_tab.
        create data lp_wa like line of gt_list.
        assign lp_wa->* to <ls_wa>.
        <ls_wa> = wa_tab.
        read table lt_fcat
           with key fieldname = ls_mod_cell-fieldname into ls_fieldcat.
        move ls_fieldcat-ref_table to l_tabname.
        move ls_fieldcat-fieldname to l_fieldname.
        assign component ls_fieldcat-fieldname
                       of structure wa_tab
                       to <l_field_value>.
**§6 assign the cell table fieldsymbol to the dereferenced data table
*and
*   fill the table.
        assign p_er_event_data->m_data->* to <itab>.
        append ls_mod_cell to <itab>.
….
….
     ENDIF.
….
…..
endcase.

–>

SAP ABAP建立多个SELECTION SCREEN

当用户输入选择条件时,须一步一步确定的时候,可以建立多个选择屏幕,根据用户的选择弹出选择屏幕。

REPORT demo_call_selection_screen.

SELECTION-SCREEN BEGIN OF BLOCK sel1 WITH FRAME TITLE tit1.
PARAMETERS: cityfr TYPE spfli-cityfrom,
             cityto TYPE spfli-cityto.
SELECTION-SCREEN END OF BLOCK sel1.

SELECTION-SCREEN BEGIN OF SCREEN 500 AS WINDOW.
SELECTION-SCREEN INCLUDE BLOCKS sel1.
SELECTION-SCREEN BEGIN OF BLOCK sel2
                           WITH FRAME TITLE tit2.
PARAMETERS: airpfr TYPE spfli-airpfrom,
             airpto TYPE spfli-airpto.
SELECTION-SCREEN END OF BLOCK sel2.
SELECTION-SCREEN END OF SCREEN 500.

INITIALIZATION.
   tit1 = ‘Cities’.

AT SELECTION-SCREEN.
   CASE sy-dynnr.
     WHEN ‘0500’.
       MESSAGE w888(SABAPDOCU) WITH ‘Screen 500’.
     WHEN ‘1000’.
       MESSAGE w888(SABAPDOCU) WITH ‘Screen 1000’.
   ENDCASE.

START-OF-SELECTION.
   tit1 = ‘Cities for Airports’.
   tit2 = ‘Airports’.
   CALL SELECTION-SCREEN 500 STARTING AT 10 10.
   tit1 = ‘Cities again’.
   CALL SELECTION-SCREEN 1000 STARTING AT 10 10.

如何在SAP的Screen中编写List报表

1、相关命令
LEAVE TO LIST-PROCESSING [AND RETURN TO SCREEN <nnnn>].

LEAVE LIST-PROCESSING.

2、使用说明

3、推荐设计思路
设计一个空屏幕,在需要调用list的屏幕的逻辑流中使用CALL SCREEN调用空屏幕。空屏幕的next   screen设置为0,不需要PAI,只需要在PBO中设计一个Module,这个module的具体步骤如下:

  1. 首先调用LEAVE TO LIST-PROCESSING AND RETURN TO SCREEN 0.
  2. 为list设置GUI status ;如:设置SPACE 或者为自己定义的.
  3. 使用下列语句使空屏幕不输出:

    SUPPRESS DIALOG.

    or

    LEAVE SCREEN. 

  4. 进行List输出和处理.

4、示例
REPORT demo_leave_to_list_processing .
TABLES sdyn_conn.
DATA: wa_spfli TYPE spfli,
       flightdate TYPE sflight-fldate.

CALL SCREEN 100.

MODULE status_0100 OUTPUT.
   SET PF-STATUS ‘SCREEN_100’.
ENDMODULE.

MODULE cancel INPUT.
   LEAVE PROGRAM.
ENDMODULE.

MODULE user_command_0100.
   CALL SCREEN 500.
   SET SCREEN 100.
ENDMODULE.

MODULE call_list_500 OUTPUT.
   LEAVE TO LIST-PROCESSING AND RETURN TO SCREEN 0.
   SET PF-STATUS space.
   SUPPRESS DIALOG.
   SELECT   carrid connid cityfrom cityto
     FROM   spfli
     INTO   CORRESPONDING FIELDS OF wa_spfli
     WHERE carrid = sdyn_conn-carrid.
     WRITE: / wa_spfli-carrid, wa_spfli-connid,
              wa_spfli-cityfrom, wa_spfli-cityto.
     HIDE: wa_spfli-carrid, wa_spfli-connid.
   ENDSELECT.
   CLEAR: wa_spfli-carrid.
ENDMODULE.

TOP-OF-PAGE.
   WRITE text-001 COLOR COL_HEADING.
   ULINE.

TOP-OF-PAGE DURING LINE-SELECTION.
   WRITE sy-lisel COLOR COL_HEADING.
   ULINE.

AT LINE-SELECTION.
   CHECK not wa_spfli-carrid is initial.
   SELECT   fldate
     FROM   sflight
     INTO   flightdate
     WHERE carrid = wa_spfli-carrid AND
           connid = wa_spfli-connid.
      WRITE / flightdate.
   ENDSELECT.
   CLEAR: wa_spfli-carrid.

该示例屏幕100只包含一个输入字段(SDYN_CONN-CARRID ),在屏幕100的逻辑流中会调用list处理.屏幕100的逻辑流如下:

PROCESS BEFORE OUTPUT.
   MODULE STATUS_0100.

PROCESS AFTER INPUT.
   MODULE CANCEL AT EXIT-COMMAND.
   MODULE USER_COMMAND_0100.

在屏幕100的PAI的module USER_COMMAND_100 将使用CALL SCREEN调用屏幕500。屏幕500的逻辑流中封装了所有List处理,屏幕500的逻辑流如下:

PROCESS BEFORE OUTPUT.
   MODULE CALL_LIST_500.

PROCESS AFTER INPUT.

屏幕500的PBO的module CALL_LIST_500定义了list处理代码。另外由于屏幕500的屏幕流设置为0,该程序从List处理返回时到屏幕100的USER_COMMAND_100的调用CALL SCREEN 500的语句之后.

该程序会在list处理时显示一个明细List,具体代码参见AT LINE-SELECTION、TOP-OF-PAGE 和 TOP-OF-PAGE DURING LINE-SELECTION事件代码.