Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Submitted: Oct 25, 2007

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: 

Error rendering macro 'code': Invalid value specified for parameter 'lang'
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.