Skip to Content
Technical Articles
Author's profile photo Jigang Zhang 张吉刚

Editable ALV with F4 Help support Multiple Selection

It’s a small requirement that asks to provide multiple selections for one customized field at editable ALV grid of one customized screen:

  • All selected entries of the popup list from F4 should be concatenated into one string separated by a comma. For example, if the user ticks the checkbox for 01&03, then the value should be 01,03.
  • If the user press F4 for the field which already been maintained, need to make those entries as selected at the popup selection list. Otherwise, users have to reselect even if for modification which will be very annoying if many entries have been chosen.

It’s a kind of combination of Editable ALV with F4 Help and Multiple Selection F4 HELP (With Check Boxes). It looks simple but still costs me some time to make it work, as those experiences are others, not mine~ Get hands dirty and memory recorded~ at least last longer a little bit…

Class definition

CLASS main_event_receiver DEFINITION.
  PUBLIC SECTION.
    METHODS: on_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 Implementation

CLASS main_event_receiver IMPLEMENTATION.
  METHOD on_f4.
    TYPES: BEGIN OF t_scope,
            zcategory	TYPE zsd_category,
            zdesc	TYPE zsd_description,
           END OF t_scope.
    TYPES: BEGIN OF tp_bus_scope,
           zcategory TYPE zsd_category,
           END OF tp_bus_scope.
    DATA: lt_list TYPE TABLE OF t_scope,
          ls_list LIKE LINE OF lt_list,
          lt_bus_scope TYPE TABLE OF tp_bus_scope,
          ls_bus_scope TYPE tp_bus_scope.
    DATA: lt_ret    TYPE STANDARD TABLE OF ddshretval,
          ls_sel    LIKE LINE OF lt_ret,
          i_mark    TYPE ddshmarks,
          ls_mark   LIKE LINE OF i_mark,
          ld_string TYPE string,
          ls_modi   TYPE lvc_s_modi,
          ls_stable TYPE lvc_s_stbl.

    FIELD-SYMBOLS: <lfs_extract> TYPE zsd_cqm_ctf_alv.

    CASE e_fieldname.
      WHEN 'ZBUS_SCOPE'.
        "all entry for select
        IF lt_list[] IS INITIAL.
          SELECT zcategory zdesc
            INTO TABLE lt_list
            FROM zsd_cqm_category
            WHERE langu EQ sy-langu.
        ENDIF.

        " get marked for F4 from current value of business scope
        REFRESH i_mark.
        READ TABLE gt_ctf INDEX es_row_no-row_id ASSIGNING <lfs_extract>.
        IF sy-subrc EQ 0 AND <lfs_extract>-zbus_scope IS NOT INITIAL.
          ld_string = <lfs_extract>-zbus_scope.
          SPLIT ld_string AT ',' INTO TABLE lt_bus_scope.
          SORT lt_bus_scope.
          LOOP AT lt_bus_scope INTO ls_bus_scope.
            LOOP AT lt_list INTO ls_list
              WHERE zcategory EQ ls_bus_scope-zcategory.
              ls_mark = sy-tabix.
              APPEND ls_mark TO i_mark.
            ENDLOOP.
          ENDLOOP.
        ENDIF.


        CLEAR ld_string.
        "Call the function module to display the custom F4 values
        CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
          EXPORTING
            retfield        = 'ZBUS_SCOPE'
            dynpprog        = 'SAPLZMM_VENDOR_ENH'
            dynpnr          = '0100'
            dynprofield     = 'ZBUS_SCOPE'
            window_title    = text-009
            value_org       = 'S'
            multiple_choice = 'X'
            mark_tab        = i_mark  "Marked table
          TABLES
            value_tab       = lt_list[] "F4 value list table
            return_tab      = lt_ret[]  "user selected table
          EXCEPTIONS
            parameter_error = 1
            no_values_found = 2
            OTHERS          = 3.

        IF sy-subrc = 0.
          READ TABLE gt_ctf INDEX es_row_no-row_id ASSIGNING <lfs_extract>.
          IF sy-subrc EQ 0.
            LOOP AT lt_ret INTO ls_sel.
              CLEAR ls_list.
              "get code from value
              READ TABLE lt_list INTO ls_list
                   WITH KEY zdesc = ls_sel-fieldval.
              IF ld_string IS INITIAL.
                ld_string = ls_list-zcategory.
              ELSE.
                CONCATENATE ld_string ls_list-zcategory
                INTO ld_string SEPARATED BY ','.
              ENDIF.
            ENDLOOP.
            IF ld_string IS NOT INITIAL.
              <lfs_extract>-zbus_scope = ld_string.
            ENDIF.
            IF grid_main IS BOUND.
              ls_stable-row = abap_true.
              ls_stable-col = abap_true.
              CALL METHOD grid_main->refresh_table_display
                EXPORTING
                  is_stable = ls_stable
                EXCEPTIONS
                  finished  = 1
                  OTHERS    = 2.
            ENDIF.

          ENDIF.
        ENDIF.
    ENDCASE.
    "(to inform grid that f4 was handled manually)
    er_event_data->m_event_handled = abap_true.
  ENDMETHOD.
ENDCLASS.

PBO of customized screen

MODULE status_0100 OUTPUT.
*TYPES: gtt_t_f4    TYPE STANDARD TABLE OF lvc_s_f4.
data: lt_f4           TYPE lvc_t_f4,
      lt_f4_tmp       TYPE gtt_t_f4.
  IF main_container IS INITIAL.
* create a custom container control for ALV Control
    CREATE OBJECT main_container
      EXPORTING
        container_name              = cont_on_main
      EXCEPTIONS
        cntl_error                  = 1
        cntl_system_error           = 2
        create_error                = 3
        lifetime_error              = 4
        lifetime_dynpro_dynpro_link = 5.

* create an instance of alv control
    CREATE OBJECT grid_main
      EXPORTING
        i_parent = main_container.
*    Prepare F4 help ON_F4 EVENT
    PERFORM prepare_f4 USING 'ZBUS_SCOPE' CHANGING lt_f4_tmp.
    if main_event is INITIAL.
      CREATE OBJECT main_event.
    endif.
    SET HANDLER main_event->on_f4 for grid_main.
*    Register for F4 event
    lt_f4[] = lt_f4_tmp[].
    CALL METHOD grid_main->register_f4_for_fields
      EXPORTING
        it_f4 = lt_f4.
* category setting 
...
for field which need F4
pt_fieldcat-f4availabl = 'X'. "to show F4 icon! Very important!
...
CALL METHOD grid_main->set_table_for_first_display.
...
CALL METHOD grid_main->set_ready_for_input.
...

FORM prepare_f4 USING pv_fieldname TYPE lvc_fname
             CHANGING ct_f4        TYPE gtt_t_f4.
  DATA: ls_f4 LIKE LINE OF ct_f4.
  ls_f4-fieldname  = pv_fieldname.
  ls_f4-register   = abap_true.
  ls_f4-getbefore  = space.
  ls_f4-chngeafter = space.
  APPEND ls_f4 TO ct_f4.
ENDFORM.


Be cautious that mark_tab of FM:‘F4IF_INT_TABLE_VALUE_REQUEST’ is sorted table. If the user maintains this field manually instead of multiply selection from F4, have to sort the user-maintained value first otherwise dump ‘ITAB_ILLEGAL_SORT_ORDER’ is waiting.

 

Assigned tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.