Custom Button, Select All, DeSelect All & Filter option in OOPS ALV – Using CL_GUI_ALV_GRID

This post describes about how to add Custom button in ALV Toolbar using Class CL_GUI_ALV_GRID. Sometimes we get scenarios to select all the records / deselect all the selected records. Selecting all the records one by one is not a good idea and we can provide a custom button to select all the records. In this case if we put filter for any value in any of the columns and ‘Select All’ button is clicked, then all the records will be selected instead of filtered records. To over come this we have to write code to get the filtered records.

In event handler method of ALV Class, if we write below code, then ‘Select all’ and ‘deselect all’ works well. But if we filter the output with some condition and press on select all – then it selects all records instead of filtered records.

  METHOD me_handle_user_command.

    CASE e_ucomm.
      WHEN ‘SELALL’.
        UNASSIGN <fs_sflight>.
        LOOP AT gt_sflight ASSIGNING <fs_sflight>.
          <fs_sflight>checkbox abap_true.
        ENDLOOP.

*  Refresh output
        PERFORM f_refresh_alv.

      WHEN ‘DESELALL’.
        UNASSIGN <fs_sflight>.
        LOOP AT gt_sflight ASSIGNING <fs_sflight>.
          <fs_sflight>checkbox abap_false.
        ENDLOOP.

*  Refresh output
        PERFORM f_refresh_alv.

    ENDCASE.
  ENDMETHOD.

1

Now if we hit on ‘Select All’ then, all records are selected irrespective of filter criteria

2

Using below piece of code, we can select only filtered records:

  METHOD me_handle_user_command.

    CASE e_ucomm.
      WHEN ‘SELALL’.
        DATA i_filter_entries TYPE lvc_t_fidx“Filtered entries,
               gv_sel_valid     TYPE char1,
               gv_sel_tabix     TYPE sytabix.


        IF gr_alv IS NOT INITIAL.
          CALL METHOD gr_alv->check_changed_data
            IMPORTING
              e_valid gv_sel_valid.
        ENDIF.

        IF gv_sel_valid EQ ‘X’.
          CALL METHOD gr_alv->get_filtered_entries
            IMPORTING
              et_filtered_entries i_filter_entries.

          UNASSIGN <fs_sflight>.
          LOOP AT gt_sflight ASSIGNING <fs_sflight>.
            gv_sel_tabix sytabix.
            READ TABLE i_filter_entries FROM gv_sel_tabix TRANSPORTING NO FIELDS.
            IF sysubrc IS NOT INITIAL.
              <fs_sflight>checkbox abap_true.
            ENDIF.
          ENDLOOP.

*  Refresh output
          PERFORM f_refresh_alv.
        ENDIF.

      WHEN ‘DESELALL’.
        UNASSIGN <fs_sflight>.
        LOOP AT gt_sflight ASSIGNING <fs_sflight>.
          <fs_sflight>checkbox abap_false.
        ENDLOOP.

*  Refresh output
        PERFORM f_refresh_alv.

    ENDCASE.
  ENDMETHOD.                    “me_handle_user_command

3.jpg

 

Report Source Code:

*&———————————————————————*
*& Report ZMANTEST
*&———————————————————————*

REPORT zmantest.
TABLES sflight.
SELECT-OPTIONS s_fldate FOR sflightfldate.

* For ALV output
TYPES BEGIN OF ty_sflight,
          carrid    TYPE s_carr_id,
          connid    TYPE s_conn_id,
          fldate    TYPE s_date,
          price     TYPE s_price,
          currency  TYPE s_currcode,
          planetype TYPE s_planetye,
          checkbox  TYPE char1,
        END OF ty_sflight.

DATA gt_sflight TYPE STANDARD TABLE OF ty_sflight,
       gt_fcat    TYPE        lvc_t_fcat,
       gw_layout  TYPE        lvc_s_layo,
       gr_alv     TYPE REF TO cl_gui_alv_grid,
       gt_exclude TYPE        ui_functions.

FIELD-SYMBOLS <fs_sflight> TYPE ty_sflight.

* Event handler class for ALV
CLASS lcl_alvhandler DEFINITION.
  PUBLIC SECTION.
    METHODS:

      me_handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
        IMPORTING e_object,

      me_handle_user_command FOR EVENT user_command OF cl_gui_alv_grid
        IMPORTING e_ucomm.

ENDCLASS.                    “lcl_alvhandler DEFINITION

DATA gr_alvhandler TYPE REF TO lcl_alvhandler.


START-OF-SELECTION.
* Extracting to populate ALV table
  SELECT carrid
         connid
         fldate
         price
         currency
         planetype
    FROM sflight INTO TABLE gt_sflight
  WHERE fldate IN s_fldate.
  IF sysubrc IS INITIAL.

*   Populate Fieldcatalog and Exclude alv buttons which are not required
    PERFORM f_field_cat.

*   Calling screen 9000 to display ALV
    CALL SCREEN 9000.

  ENDIF.

*&———————————————————————*
*&      Form  F_FCAT
*&———————————————————————*
FORM f_field_cat.
  DATA lv_pos TYPE i.
  REFRESH :gt_fcat[],gt_exclude[].

  APPEND cl_gui_alv_grid=>mc_fc_loc_delete_row TO gt_exclude.
  APPEND cl_gui_alv_grid=>mc_fc_views TO gt_exclude.
  APPEND cl_gui_alv_grid=>mc_fc_loc_insert_row TO gt_exclude.
  APPEND cl_gui_alv_grid=>mc_fc_loc_append_row TO gt_exclude.
  APPEND cl_gui_alv_grid=>mc_fc_loc_paste_new_row TO gt_exclude.
  APPEND cl_gui_alv_grid=>mc_fc_loc_copy_row TO gt_exclude.
  APPEND cl_gui_alv_grid=>mc_fc_loc_copy TO gt_exclude.
  APPEND cl_gui_alv_grid=>mc_fc_loc_cut TO gt_exclude.
  APPEND cl_gui_alv_grid=>mc_fc_refresh TO gt_exclude.
  APPEND cl_gui_alv_grid=>mc_mb_paste TO gt_exclude.
  APPEND cl_gui_alv_grid=>mc_fc_loc_undo TO gt_exclude.
  APPEND cl_gui_alv_grid=>mc_fc_check TO gt_exclude.

  lv_pos =  lv_pos + 1.
  PERFORM f_fcat USING lv_pos ‘CHECKBOX’  ‘Select’.
  lv_pos =  lv_pos + 1.
  PERFORM f_fcat USING lv_pos ‘CARRID’    ‘Airline Code’.
  lv_pos =  lv_pos + 1.
  PERFORM f_fcat USING lv_pos ‘CONNID’    ‘Connection Number’.
  lv_pos =  lv_pos + 1.
  PERFORM f_fcat USING lv_pos ‘FLDATE’    ‘Flight date’.
  lv_pos =  lv_pos + 1.
  PERFORM f_fcat USING lv_pos ‘PRICE’     ‘Airfare’.
  lv_pos =  lv_pos + 1.
  PERFORM f_fcat USING lv_pos ‘CURRENCY’  ‘Currency ‘.
  lv_pos =  lv_pos + 1.
  PERFORM f_fcat USING lv_pos ‘PLANETYPE’ ‘Aircraft Type’.

ENDFORM.


*&———————————————————————*
*&      Form  F_FCAT
*&———————————————————————*
FORM f_fcat  USING    p_pos
                      p_fname
                      p_text.

  DATA gw_fcat TYPE lvc_s_fcat.

  CLEAR gw_fcat.
  gw_fcatfieldname  p_fname.
  gw_fcatcol_pos    p_pos.
  gw_fcatcoltext    p_text.
  IF p_fname ‘CHECKBOX’.
    gw_fcatedit     abap_true.
    gw_fcatcheckbox abap_true.
    gw_fcatkey      abap_true.
  ELSEIF p_fname ‘CARRID’ OR
         p_fname ‘CONNID’ OR
         p_fname =  ‘FLDATE’.
    gw_fcatkey abap_true.
  ENDIF.

  APPEND gw_fcat TO gt_fcat.
  CLEAR gw_fcat.
ENDFORM.                    ” F_FCAT



*&———————————————————————*
*&      Module  STATUS_9000  OUTPUT
*&———————————————————————*
*       text
*———————————————————————-*
MODULE status_9000 OUTPUT.

  SET PF-STATUS ‘Z9000’.
  SET TITLEBAR ‘Z9000’.

  PERFORM f_display.

ENDMODULE.

FORM f_display .

  IF gr_alv IS INITIAL.
    CREATE OBJECT gr_alv
      EXPORTING
        i_parent          cl_gui_container=>screen0
      EXCEPTIONS
        error_cntl_create 1
        error_cntl_init   2
        error_cntl_link   3
        error_dp_create   4
        OTHERS            5.
    IF sysubrc <> 0.
      MESSAGE ID symsgid TYPE symsgty NUMBER symsgno
                 WITH symsgv1 symsgv2 symsgv3 symsgv4.
    ENDIF.

    IF gr_alv IS NOT INITIAL.
      CALL METHOD gr_alv->register_edit_event
        EXPORTING
          i_event_id cl_gui_alv_grid=>mc_evt_modified.

      IF gr_alvhandler IS INITIAL.
        CREATE OBJECT gr_alvhandler.
      ENDIF.

      SET HANDLER gr_alvhandler->me_handle_toolbar        FOR gr_alv.
      SET HANDLER gr_alvhandler->me_handle_user_command   FOR gr_alv.

      gw_layoutcwidth_opt    abap_true.
      gw_layoutsel_mode ‘A’.

      CALL METHOD gr_alv->set_table_for_first_display
        EXPORTING
          i_save                        ‘A’
          is_layout                     gw_layout
          it_toolbar_excluding          gt_exclude
        CHANGING
          it_outtab                     gt_sflight
          it_fieldcatalog               gt_fcat
        EXCEPTIONS
          invalid_parameter_combination 1
          program_error                 2
          too_many_lines                3
          OTHERS                        4.
      IF sysubrc <> 0.
        MESSAGE ID symsgid TYPE symsgty NUMBER symsgno
                WITH symsgv1 symsgv2 symsgv3 symsgv4.
      ENDIF.
    ENDIF.
  ELSE.
    CALL METHOD gr_alv->refresh_table_display.
  ENDIF.

ENDFORM.                    “f_display


CLASS lcl_alvhandler IMPLEMENTATION.
  METHOD me_handle_toolbar.
    DATAls_toolbar  TYPE stb_button.

* Append a separator to normal toolbar
    CLEAR ls_toolbar.
    MOVE TO ls_toolbarbutn_type.
    APPEND ls_toolbar TO e_object->mt_toolbar.
    CLEAR ls_toolbar.


* Append Button & Icon for your function
    MOVE ‘SELALL’ TO ls_toolbarfunction.
    MOVE icon_select_all TO ls_toolbaricon.
    MOVE ‘Select All’ TO ls_toolbarquickinfo.
    MOVE ‘Select All’ TO ls_toolbartext.
    MOVE ‘ ‘ TO ls_toolbardisabled.
    APPEND ls_toolbar TO e_object->mt_toolbar.
    CLEAR ls_toolbar.

    MOVE TO ls_toolbarbutn_type.
    APPEND ls_toolbar TO e_object->mt_toolbar.
    CLEAR ls_toolbar.

    MOVE ‘DESELALL’ TO ls_toolbarfunction.
    MOVE icon_deselect_all TO ls_toolbaricon.
    MOVE ‘Deselect All’ TO ls_toolbarquickinfo.
    MOVE ‘Deselect All’ TO ls_toolbartext.
    MOVE ‘ ‘ TO ls_toolbardisabled.
    APPEND ls_toolbar TO e_object->mt_toolbar.
    CLEAR ls_toolbar.

  ENDMETHOD.                    “me_handle_toolbar

  METHOD me_handle_user_command.

    CASE e_ucomm.
      WHEN ‘SELALL’.
        DATA i_filter_entries TYPE lvc_t_fidx“Filtered entries,
               gv_sel_valid     TYPE char1,
               gv_sel_tabix     TYPE sytabix.


        IF gr_alv IS NOT INITIAL.
          CALL METHOD gr_alv->check_changed_data
            IMPORTING
              e_valid gv_sel_valid.
        ENDIF.

        IF gv_sel_valid EQ ‘X’.
          CALL METHOD gr_alv->get_filtered_entries
            IMPORTING
              et_filtered_entries i_filter_entries.

          UNASSIGN <fs_sflight>.
          LOOP AT gt_sflight ASSIGNING <fs_sflight>.
            gv_sel_tabix sytabix.
            READ TABLE i_filter_entries FROM gv_sel_tabix TRANSPORTING NO FIELDS.
            IF sysubrc IS NOT INITIAL.
              <fs_sflight>checkbox abap_true.
            ENDIF.
          ENDLOOP.

*  Refresh output
          PERFORM f_refresh_alv.
        ENDIF.

      WHEN ‘DESELALL’.
        UNASSIGN <fs_sflight>.
        LOOP AT gt_sflight ASSIGNING <fs_sflight>.
          <fs_sflight>checkbox abap_false.
        ENDLOOP.

*  Refresh output
        PERFORM f_refresh_alv.

    ENDCASE.
  ENDMETHOD.                    “me_handle_user_command

ENDCLASS.                    “lcl_alvhandler IMPLEMENTATION

*&———————————————————————*
*&      Form  F_REFRESH_ALV
*&———————————————————————*
FORM f_refresh_alv.

  DATA lw_stable TYPE lvc_s_stbl.

  CLEAR lw_stable.
  lw_stablerow abap_true.
  lw_stablecol abap_true.

  gr_alv->refresh_table_display(
   EXPORTING
    is_stable      lw_stable
  EXCEPTIONS
    finished       1
    OTHERS         ).
  IF sysubrc <> 0.
    MESSAGE ID symsgid TYPE symsgty NUMBER symsgno
               WITH symsgv1 symsgv2 symsgv3 symsgv4.
  ENDIF.

ENDFORM.                    “f_refresh_alv


*&———————————————————————*
*&      Module  USER_COMMAND_9000  INPUT
*&———————————————————————*
MODULE user_command_9000 INPUT.

  CASE syucomm.
    WHEN ‘BACK’ OR ‘CANCEL’ OR ‘EXIT’.
      LEAVE TO SCREEN 0.
  ENDCASE.

ENDMODULE.

 

Screen: 9000

4


 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s