1

I try to dynamically build a toolbar depending on given criterias thanks to "toolbar" event in class cl_gui_alv_grid.

The problem is that the toolbar is getting my custom button but as soon as my criteria does not match anymore and that I'd like to remove one button, the toolbar is not getting refreshed. Example here below

"Refresh button


CLEAR ls_toolbar.
  ls_toolbar-icon        = icon_refresh.
  ls_toolbar-quickinfo   = TEXT-ref.
  ls_toolbar-butn_type   = 0.
  ls_toolbar-text        = TEXT-ref.
  ls_toolbar-function    = 'REFRESH'.
  ls_toolbar-disabled    = abap_false.
  APPEND ls_toolbar      TO e_object->mt_toolbar.

  "Save button
  IF my_criteria is false.
    DELETE e_object->mt_toolbar WHERE function EQ 'SAVE'.
  ELSE.
    CLEAR ls_toolbar.
    ls_toolbar-icon        = icon_system_save.
    ls_toolbar-quickinfo   = TEXT-sav.
    ls_toolbar-butn_type   = 0.
    ls_toolbar-text        = TEXT-sav.
    ls_toolbar-function    = 'LAMA_SAVE'.
    ls_toolbar-disabled    = abap_false.
    APPEND ls_toolbar      TO e_object->mt_toolbar.

  ENDIF.

When I create my ALV I do call method lo_alv->set_toolbar_interactive( ).

When I go into debug mode I see that the save button is added or removed but it is not getting reflected on the screen once the toolbar has been displayed at leat once.

1
  • Did you attach event handler before calling the grid?
    – Suncatcher
    Sep 18, 2018 at 9:27

4 Answers 4

0

I can achieve the functionality that you're looking for, but it's not clear what is your exact flow, there could be several origins of the issue. So, here's a short demo how you should do it (the program displays one button in the toolbar, you click it and it disappears, the rest of the ALV remains unchanged).

REPORT zdemo.
CLASS lcl_app DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS pbo.
    CLASS-METHODS on_user_command FOR EVENT user_command OF cl_gui_alv_grid
            IMPORTING sender e_ucomm.
    CLASS-METHODS handle_toolbar FOR EVENT toolbar OF cl_gui_alv_grid
            IMPORTING e_object e_interactive.
    CLASS-DATA alv_grid TYPE REF TO cl_gui_alv_grid.
    CLASS-DATA flights TYPE TABLE OF sflight.
ENDCLASS.

CLASS lcl_app IMPLEMENTATION.
  METHOD pbo.
    DATA: it_layout  TYPE lvc_s_layo,
          it_fcat    TYPE lvc_t_fcat,
          ls_exclude TYPE ui_func,
          gs_variant TYPE disvariant,
          lt_exclude TYPE ui_functions.

    IF alv_grid IS NOT BOUND.
      SELECT * FROM sflight INTO TABLE flights.

      ls_exclude = cl_gui_alv_grid=>mc_fc_excl_all.
      APPEND ls_exclude TO lt_exclude.

      CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
        EXPORTING i_structure_name = 'SFLIGHT'
        CHANGING ct_fieldcat = it_fcat
        EXCEPTIONS OTHERS = 3.
      CHECK sy-subrc = 0.

      CREATE OBJECT alv_grid
        EXPORTING
          i_parent = cl_gui_container=>screen0.

      SET HANDLER handle_toolbar FOR alv_grid.
      SET HANDLER on_user_command FOR alv_grid.
      CALL METHOD alv_grid->set_table_for_first_display
        EXPORTING
          is_layout            = it_layout
          it_toolbar_excluding = lt_exclude
          is_variant           = gs_variant
        CHANGING
          it_outtab            = flights
          it_fieldcatalog      = it_fcat
        EXCEPTIONS
          OTHERS               = 4.
    ENDIF.
  ENDMETHOD.

  METHOD handle_toolbar.
    DATA ls_toolbar TYPE stb_button.
    IF e_interactive = abap_false.
      " Added the first time only
      ls_toolbar-function  = 'DEMO'.
      ls_toolbar-text      = 'Click me and I will disappear'.
      APPEND ls_toolbar TO e_object->mt_toolbar.
    ENDIF.
  ENDMETHOD.

  METHOD on_user_command.
    IF e_ucomm = 'DEMO'.
      sender->set_toolbar_interactive( ).
    ENDIF.
  ENDMETHOD.
ENDCLASS.

PARAMETERS dummy.

AT SELECTION-SCREEN OUTPUT.
  lcl_app=>pbo( ).
0

If I'm not mistaken, refreshing the ALV grid does not rebuild the toolbar.

I think you have to reinitialise the ALV grid with 'SET_TABLE_FOR_FIRST_DISPLAY' in order to have the additional toolbar functionality.

1
  • Re-displaying everything is the last thing to do. Only the toolbar needs to be refreshed, and this can be achieved (the question is just to know what @flowx1710 did and what he missed). Sep 18, 2018 at 11:41
0

Sandra Rosssi is right. There was two different instances of the ALVG and it got messy with the handler that's why the toolbar was not getting refreshed correctly.

Thanks all,

0

If I create an ALV grid instance (1), add it to an parent custom container, set its handlers, free it with instance1->free() and free instance1, and then ... add a new identical ALV grid instance (2) to the same custom container, with handlers, the handlers of (2) are not working (the handlers of (1) neither!), and the toolbar of (1) is kept being displayed.

To escape from this unloved state, and to get a dynamic tool bar ...

If I put an simple cl_gui_cfw=>flush( ) in between the creation and freeing of (1) and the creation of (2), the handlers of (2) are working as expected, and the toolbar reflects the icons of instance (2), as expected.

As said, this is only relevant for the case of two instances created "in one go". The problem does not occur having only one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.