简体   繁体   中英

Enable standard toolbar for ALV grid

I created a screen for displaying ALV output but I am not able to show standard toolbar buttons (save, exit, back, etc.).

Can any one suggest how to enable them?

DATA: it_zztstudent type STANDARD TABLE OF zztstudent,
      it_fcat TYPE STANDARD TABLE OF lvc_s_fcat,
      i_selected_rows TYPE lvc_t_row,
      w_selected_rows type lvc_s_row,
      it_modified type STANDARD TABLE OF zztstudent,
      lw_modified type zztstudent,
      lw_zztstudent type zztstudent,
      w_variant   TYPE disvariant,
      o_docking type REF TO cl_gui_docking_container,
      o_grid type ref to cl_gui_alv_grid.

FIELD-SYMBOLS: <fs_fieldcat> type lvc_s_fcat.
tables: zztstudent.
select-OPTIONS: sst_id for zztstudent-st_id.
select * from zztstudent
     into table it_zztstudent
     where st_id in sst_id.
if sy-subrc NE 0.
   message e001(zmsgpr).
ENDIF.

call screen 9000.

module status_9000 OUTPUT.
   if o_docking is initial.
     set PF-STATUS 'ZSTATUS'.
     set titlebar 'ZTITLE'.
   "Creating Docking Container and grid
   PERFORM create_object.
   "filling the fieldcatalog table
   PERFORM create_fieldcat.
   "Modifying the fieldcatalog table
   PERFORM modify_fieldcat.
   "Registering edit
   PERFORM register_edit.
   "displaying the output
   PERFORM display_output.
  ENDIF.
endmodule.

MODULE user_command_9000 INPUT.
  Data: lv_ucomm TYPE sy-ucomm.
  lv_ucomm = sy-ucomm.
  CASE lv_ucomm.
    WHEN 'CANCEL' oR 'EXIT'.
      PERFORM free_objects.
      leave program.
    WHEN 'BACK'.
      PERFORM free_objects.
      SET SCREEN '0'.
      leave SCREEN.
    WHEN 'SAVE'.
      PERFORM save_database.
       CALL METHOD o_grid->refresh_table_display.
  ENDCASE.

ENDMODULE.
Form create_object.
  "create docking container
  create object o_docking
      exporting
          ratio = '95'.
  if sy-subrc eq 0.
    "create grid
    create OBJECT o_grid
       exporting
         i_parent = o_docking.
  endif.
ENDFORM.

FORM create_fieldcat.
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
   EXPORTING
*     I_BUFFER_ACTIVE              =
     I_STRUCTURE_NAME             =  'ZZTSTUDENT'
*     I_CLIENT_NEVER_DISPLAY       = 'X'
*     I_BYPASSING_BUFFER           =
*     I_INTERNAL_TABNAME           =
    CHANGING
      CT_FIELDCAT                  = IT_FCAT
   EXCEPTIONS
     INCONSISTENT_INTERFACE       = 1
     PROGRAM_ERROR                = 2
     OTHERS                       = 3
            .
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.
  ENDFORM.
"making the column as editable
FORM modify_fieldcat.
   loop at it_fcat ASSIGNING <fs_fieldcat>.
     CASE <fs_fieldcat>-fieldname.
      WHEN 'ST_NAME'.
        <fs_fieldcat>-edit = 'X'.
        WHEN 'ST_CITY'.
        <fs_fieldcat>-edit = 'X'.
     ENDCASE.
   ENDLOOP.
ENDFORM.

FORM register_edit.
  call METHOD o_grid->register_edit_event
     exporting
        i_event_id = cl_gui_alv_grid=>mc_evt_modified.
ENDFORM.

FORM display_output.
   w_variant-report = sy-repid.
   call METHOD o_grid->set_table_for_first_display
       EXPORTING
            is_variant = w_variant
            i_save     = 'A'
       CHANGING
            it_outtab  = it_zztstudent
            IT_FIELDCATALOG = it_fcat
       EXCEPTIONS
            invalid_parameter_combination = 1
            program_error = 2
            too_many_lines = 3
            others = 4.
   if sy-subrc <> 0.
     message e001(zmsgpr).
   endif.
ENDFORM.

FORM free_objects.
  call method o_grid->free
     EXCEPTIONS
        cntl_error = 1
        cntl_system_error = 2
        others = 3.
  if sy-subrc <> 0.
    message e001(zmsgpr).
  endif.

  call method o_docking->free
     EXCEPTIONS
       cntl_error = 1
       cntl_system_error = 2
       others = 3.
  if sy-subrc <> 0.
     message e001(zmsgpr).
  endif.

ENDFORM.

First you must create a method, in your class definition like this:

METHODS on_toolbar
  FOR EVENT toolbar
              OF  cl_gui_alv_grid
  IMPORTING e_object.

After that you must create an event object and there you must set the handler for the alvgrid object, like these:

IF gcl_container IS INITIAL.

  CREATE OBJECT gcl_container
    EXPORTING
      container_name = 'CONTAINER'.

  CREATE OBJECT gcl_grid
    EXPORTING
      i_parent = gcl_container.

  "erstellt einen Handler der alle ereignisse aufnimmt
  "created object and set a handler of all events
  CREATE OBJECT gcl_event.
  SET HANDLER gcl_event->on_toolbar
      FOR gcl_grid.
ENDIF.

Hope it helps you. :)

First double click on set PF-STATUS 'ZSTATUS' on ZSTATUS and create object.

After that you will get here:

屏幕按钮

Click in functions Keys and add what code you want for each button. Finally in a case statement, check if sy-ucomm has the value of the code you entered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM