Sunday, December 19, 2010

Adding 'Select All' button in standard ALV toolbar for selecting all checkboxes

Hi All,

Many of us must have faced the problem of adding an extra button to the existing standard toolbar of ALV.In this requirement ALV is displayed with Checkboxes.Two additional buttons have been added one for selecting all and another for deselecting all checkboxes.

Following image shows the screen shot of  standard ALV toolbar.

And following image shows the screen shot of  ALV toolbar in which two extra buttons has been added.Those two buttons are highlighted.

I did this through Object Oriented approach.
In  object oriented alv we have to prepare container,grid and field catalogue and fill the internal table which will have data.

For adding buttons define a local class.

This is the definition of local class.
  *----------------------------------------------------------------------*
*       CLASS gcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
CLASS gcl_event_receiver DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS:
    handle_toolbar
        FOR EVENT toolbar OF cl_gui_alv_grid
            IMPORTING e_object e_interactive,
    handle_menu_button
        FOR EVENT menu_button OF cl_gui_alv_grid
            IMPORTING e_object e_ucomm,
    handle_user_command
        FOR EVENT user_command OF cl_gui_alv_grid
            IMPORTING e_ucomm.
ENDCLASS.                    "gcl_event_receiver DEFINITION


Following is the implementation of local class.

  *----------------------------------------------------------------------*
CLASS gcl_event_receiver IMPLEMENTATION.

  METHOD handle_toolbar.
    PERFORM handle_toolbar USING e_object.

"Icons can be selected from table ICON
  FORM handle_toolbar USING e_object TYPE REF TO cl_alv_event_toolbar_set.

*Add Button 'Select All' to the AlV toolbar
  CLEAR gs_toolbar.
  MOVE gc_selall TO gs_toolbar-function.
* --> This function code is evaluated in 'handle_menu_button'
  MOVE icon_select_all TO gs_toolbar-icon.
  MOVE  text-101 TO gs_toolbar-quickinfo.
  MOVE space TO gs_toolbar-disabled.
  APPEND gs_toolbar TO e_object->mt_toolbar.

*Add Button 'Deselct All' to the AlV toolbar
  CLEAR gs_toolbar.
  MOVE gc_deselal TO gs_toolbar-function.
* --> This function code is evaluated in 'handle_menu_button'
  MOVE icon_deselect_block TO gs_toolbar-icon.
  MOVE text-102 TO gs_toolbar-quickinfo.
  MOVE space TO gs_toolbar-disabled.
  APPEND gs_toolbar TO e_object->mt_toolbar.

ENDFORM
 
  ENDMETHOD.                    "handle_toolbar
*----------------------------------------------------------------------*
  METHOD handle_menu_button.
    PERFORM handle_menu_button USING e_ucomm
                                     e_object.

  FORM handle_menu_button  USING e_ucomm  TYPE        sy-ucomm
                               e_object TYPE REF TO cl_ctmenu.
*  query e_ucomm to find out which menu button has been clicked on
  IF e_ucomm = gc_selall.
    CALL METHOD e_object->add_function
      EXPORTING
        fcode = 'SELALL'
        text  = text-101"Select All
  ELSEIF e_ucomm = gc_deselal.
    CALL METHOD e_object->add_function
      EXPORTING
        fcode = 'DESELAL'
        text  = text-102"De Select All
  ENDIF.
ENDFORM.                    " HANDLE_MENU_BUTTON
 
  ENDMETHOD.                    "handle_menu_button

  METHOD handle_user_command.
    PERFORM f21000_select_all_checkbox USING e_ucomm.

  FORM f21000_select_all_checkbox USING x_ucomm.
" git_ftable is internal table containing data for ALV.
  CASE x_ucomm.
    WHEN 'SELALL'."When Select all is clicked
      LOOP AT git_ftable INTO wa_ftable.
        wa_ftable-chkbx  = 'X'.
        MODIFY git_ftable INDEX sy-tabix FROM wa_ftable.
        CLEAR:wa_ftable.
      ENDLOOP.
      CALL SCREEN 100.
    WHEN 'DESELAL'.."When De Select all is clicked
      LOOP AT git_ftable INTO wa_ftable.
        wa_ftable-chkbx  = ''.
        MODIFY git_ftable INDEX sy-tabix FROM wa_ftable.
        CLEAR:wa_ftable.
      ENDLOOP.
      CALL SCREEN 100.
  ENDCASE.
ENDFORM.                    " F21000_SELECT_ALL_CHECKBOX
 
  ENDMETHOD.                           "handle_user_command
ENDCLASS.                    "gcl_event_receiver IMPLEMENTATION


"Since we are using events we have to register the events after calling the method ' set_table_for_first_display '

Events are to be registered by following syntax

  *Events Are being registered for buttons 'Select All' and 'De Select all'
    SET HANDLER gcl_event_receiver=>handle_user_command
                gcl_event_receiver=>handle_menu_button
                gcl_event_receiver=>handle_toolbar FOR ALL INSTANCES.
* raise event TOOLBAR:
    CALL METHOD r_grid1->set_toolbar_interactive.


"Once ALV has been displayed with checkboxes.We  have to call the method ' refresh_table_display '
  *This is to display the screen again when 'Select All' button on ALV toolbar has been clicked
    ls_stable-row = abap_true.
    ls_stable-col = abap_true.

    CALL METHOD r_grid1->refresh_table_display
      EXPORTING
        is_stable = ls_stable
      EXCEPTIONS
        finished  = 1
        OTHERS    = 2.


Evreything is done.

2 comments:

  1. endmethod is missing error is occuring how to remove it

    ReplyDelete
  2. endmethod is missing error is occuring how to remove it

    ReplyDelete