Powered By

Free XML Skins for Blogger

Powered by Blogger

Thursday, February 12, 2009

Adding custom buttons on ALV grid controls

This report shows you how to add a custom button to your ALV grid control.

DATA

* Predefine a local class for event handling to allow the

* declaration of a reference variable before the class is defined.

CLASS lcl_event_receiver DEFINITION DEFERRED.

DATA: custom_container1 TYPE REF TO cl_gui_custom_container,

cont_on_main TYPE scrfname VALUE 'ALV_GRID',

grid1 TYPE REF TO cl_gui_alv_grid,

event_receiver TYPE REF TO lcl_event_receiver,

okcode LIKE sy-ucomm.

DATA: gt_fieldcat TYPE slis_t_fieldcat_alv,

gt_fieldcat1 TYPE lvc_t_fcat,

gs_layout TYPE slis_layout_alv,

gs_layout1 TYPE lvc_s_layo,

gs_print TYPE slis_print_alv,

gt_sort TYPE slis_t_sortinfo_alv,

gt_sp_group TYPE slis_t_sp_group_alv,

gt_events TYPE slis_t_event,

gt_list_top_of_page TYPE slis_t_listheader.

DATA:t_out TYPE TABLE OF zstruct,

t_out_wa LIKE zstruct.

****************************************************************

* LOCAL CLASSES: Definition

****************************************************************

*===============================================================

* class lcl_event_receiver: local class to

* define and handle own functions.

* Definition:

* ~~~~~~~~~~~

CLASS lcl_event_receiver DEFINITION.

PUBLIC SECTION.

METHODS:

handle_toolbar

FOR EVENT toolbar OF cl_gui_alv_grid

IMPORTING e_object e_interactive,

handle_user_command

FOR EVENT user_command OF cl_gui_alv_grid

IMPORTING e_ucomm.

PRIVATE SECTION.

ENDCLASS.

****************************************************************

* LOCAL CLASSES: Implementation

****************************************************************

*===============================================================

* class lcl_event_receiver (Implementation)

CLASS lcl_event_receiver IMPLEMENTATION.

METHOD handle_toolbar.

* § 2.In event handler method for event TOOLBAR: Append own functions

* by using event parameter E_OBJECT.

DATA: ls_toolbar TYPE stb_button.

*....................................................................

* E_OBJECT of event TOOLBAR is of type REF TO CL_ALV_EVENT_TOOLBAR_SET.

* This class has got one attribute, namly MT_TOOLBAR, which

* is a table of type TTB_BUTTON. One line of this table is

* defined by the Structure STB_BUTTON (see data deklaration above).

*

* A remark to the flag E_INTERACTIVE:

* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* 'e_interactive' is set, if this event is raised due to

* the call of 'set_toolbar_interactive' by the user.

* You can distinguish this way if the event was raised

* by yourself or by ALV

* (e.g. in method 'refresh_table_display').

* An application of this feature is still unknown... :-)

* append a separator to normal toolbar

CLEAR ls_toolbar.

MOVE 3 TO ls_toolbar-butn_type.

APPEND ls_toolbar TO e_object->mt_toolbar.

* append an icon to show booking table

CLEAR ls_toolbar.

MOVE 'COMMENT' TO ls_toolbar-function.

MOVE icon_annotation TO ls_toolbar-icon.

MOVE 'Insert Comment'(001) TO ls_toolbar-quickinfo.

MOVE 'Notes'(004) TO ls_toolbar-text.

MOVE ' ' TO ls_toolbar-disabled.

APPEND ls_toolbar TO e_object->mt_toolbar.

MOVE 'MATNR' TO ls_toolbar-function.

MOVE icon_material TO ls_toolbar-icon.

MOVE 'View Material'(002) TO ls_toolbar-quickinfo.

MOVE 'Material'(003) TO ls_toolbar-text.

MOVE ' ' TO ls_toolbar-disabled.

APPEND ls_toolbar TO e_object->mt_toolbar.

ENDMETHOD.

*-------------------------------------------------------------------

METHOD handle_user_command.

* § 3.In event handler method for event USER_COMMAND: Query your

* function codes defined in step 2 and react accordingly.

DATA: lt_rows TYPE lvc_t_row.

CASE e_ucomm.

WHEN 'COMMENT'.

CALL METHOD grid1->get_selected_rows

IMPORTING et_index_rows = lt_rows.

CALL METHOD cl_gui_cfw=>flush.

IF sy-subrc NE 0.

* add your handling, for example

CALL FUNCTION 'POPUP_TO_INFORM'

EXPORTING

titel = g_repid

txt2 = sy-subrc

txt1 = 'Error in Flush'(500).

ELSE.

PERFORM get_comment TABLES lt_rows. “Perform action

ENDIF.

WHEN 'MATNR'.

CALL METHOD grid1->get_selected_rows

IMPORTING et_index_rows = lt_rows.

CALL METHOD cl_gui_cfw=>flush.

IF sy-subrc NE 0.

* add your handling, for example

CALL FUNCTION 'POPUP_TO_INFORM'

EXPORTING

titel = g_repid

txt2 = sy-subrc

txt1 = 'Error in Flush'(500).

ELSE.

READ TABLE lt_rows INDEX 1.

IF sy-subrc = 0.

READ TABLE t_out INTO t_out_wa INDEX lt_rows-index.

IF sy-subrc = 0.

SET PARAMETER ID 'MAT' FIELD t_out_wa-matnr.

CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN. "View Material

ELSE.

MESSAGE i999(b1) WITH 'Entry not found!'.

ENDIF.

ELSE.

MESSAGE i999(b1) WITH 'Please select a line first!'.

ENDIF.

ENDIF.

ENDCASE.

ENDMETHOD. "handle_user_command

*-----------------------------------------------------------------

ENDCLASS.

*

* lcl_event_receiver (Implementation)

*===================================================================

PBO

process before output.

MODULE STATUS_2000.

module init_data.

module create_container.

*&---------------------------------------------------------------------*

*& Module create_container OUTPUT

*&---------------------------------------------------------------------*

module create_container output.

if custom_container1 is initial.

* create a custom container control for our ALV Control

create object custom_container1

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.

if sy-subrc ne 0.

* add your handling, for example

call function 'POPUP_TO_INFORM'

exporting

titel = g_repid

txt2 = sy-subrc

txt1 = 'The control could not be created'(001).

endif.

* create an instance of alv control

create object grid1

exporting i_parent = custom_container1.

* allow to select single lines - Multilines = A

gs_layout1-sel_mode = 'B'.

t_out[] = t_out_mat[].

gt_fieldcat1[] = gt_fieldcat[].

* ZSTRUCT needs to be a structure or table in the data dictionary.

* The data elements are used for the column headings of your table control.

* T_OUT is your table for data.

call method grid1->set_table_for_first_display

exporting i_structure_name = 'ZSTRUCT'

is_layout = gs_layout1

changing it_outtab = t_out.

* ->Create Object to receive events and link them to handler methods.

* When the ALV Control raises the event for the specified instance

* the corresponding method is automatically called.

*

create object event_receiver.

set handler event_receiver->handle_user_command for grid1.

set handler event_receiver->handle_toolbar for grid1.

* § 4.Call method 'set_toolbar_interactive' to raise event TOOLBAR.

call method grid1->set_toolbar_interactive.

endif.

call method cl_gui_control=>set_focus exporting control = grid1.

endmodule. " create_container OUTPUT

1 comment:

Akkamahadevi said...
This comment has been removed by the author.

Archives