02/08/2010

ALV Grid and the Event


*-----------------------------------------------------------------------
* CLASS myevent_receiv_001 DEFINITION
* This class is used to define buttons inside the toolbar of the ALV
* grid and the event when clicking on the toolbar.It defines a user
* command event and variable e_ucomm receives the value of the user
* command.
*-----------------------------------------------------------------------
CLASS myevent_receiv_001 DEFINITION.
PUBLIC SECTION.
METHODS :
handle_toolbar " Create toolbar button
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 " Handling user
IMPORTING e_ucomm. " command for button

ENDCLASS.


*----------------------------------------------------------------------
* CLASS myevent_receiv_002 DEFINITION
* With this definition, we´ll define a double click event on the grid
* and catch the line and column where it happens with help of
* variables e_row and e_column.
*----------------------------------------------------------------------
CLASS myevent_receiv_002 DEFINITION.
PUBLIC SECTION.
METHODS handle_double_click
FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING
e_row
e_column.
ENDCLASS.

*----------------------------------------------------------------------
* CLASS myevent_receiv_001 IMPLEMENTATION
* Implementation of a button inside Toolbar and a SY-UCOMM function
* assigned to it.
* Try to change line MOVE '3' TO new_button-butn_type from 1 to 5
* and see what happens
*----------------------------------------------------------------------
CLASS myevent_receiv_001 IMPLEMENTATION.

METHOD handle_toolbar.
CLEAR: new_button.

* append a separator to normal toolbar to split the
* new buttons from the standard buttons:
MOVE '3' TO new_button-butn_type.
APPEND new_button TO e_object->mt_toolbar.
CLEAR new_button.

* first button:
MOVE: 'Action' TO new_button-function,
'Action button' TO new_button-quickinfo,
'Some action' TO new_button-text,
' ' TO new_button-disabled.
APPEND new_button TO e_object->mt_toolbar.
CLEAR new_button.

* Second button:
MOVE: 'Action 2' TO new_button-function,
'Action 2 button' TO new_button-quickinfo,
'Some action 2' TO new_button-text,
' ' TO new_button-disabled.
APPEND new_button TO e_object->mt_toolbar.
CLEAR new_button.

* Third button:
MOVE: 'Save' TO new_button-function, "<- Save button
'Save data' TO new_button-quickinfo,
icon_system_save TO new_button-icon,
'5' TO new_button-butn_type,
' ' TO new_button-disabled.
APPEND new_button TO e_object->mt_toolbar.
ENDMETHOD.


* What should happen when the user presses any button ??
METHOD handle_user_command.
CASE: e_ucomm.
WHEN 'Save'.
PERFORM save_records.
WHEN 'Action'.
MESSAGE ID 'MJ' TYPE 'I' NUMBER 100 WITH
'Some message or action hier'.
WHEN 'Action 2'.
PERFORM selected_rows.
WHEN OTHERS.
* do nothing
ENDCASE.
ENDMETHOD.

ENDCLASS.

*----------------------------------------------------------------------
* CLASS myevent_receiv_002 IMPLEMENTATION
* We define a method for doble click on the grid. Where did the
* user make double click on the grid, can be determine with
* e_row-index and e_column, which were declared in CLASS
* myevent_receiv_002 DEFINITION
*----------------------------------------------------------------------
CLASS myevent_receiv_002 IMPLEMENTATION.

METHOD handle_double_click.
DATA mymessage(50) TYPE c.
CONCATENATE 'Click on line: ' e_row-index ' and column: ' e_column
INTO mymessage.
MESSAGE i100(mj) WITH mymessage.

READ TABLE mytable INDEX e_row-index INTO aux_table.
aux_table-icon_id = '@0A@'. " <-- Red light
MODIFY mytable FROM aux_table
INDEX e_row-index
TRANSPORTING icon_id.
CALL METHOD mygrid->refresh_table_display.
e_row-index = ' '.
e_column = ' '.
ENDMETHOD.

ENDCLASS.




*-----------------------------------------------------------------------
* Which rows were selected ??
*-----------------------------------------------------------------------
FORM selected_rows.

CLEAR index_rows.
DATA: sel_line(20) TYPE c.

CALL METHOD mygrid->get_selected_rows
IMPORTING
et_index_rows = index_rows.

DESCRIBE TABLE index_rows LINES line.
IF line = 0.
MESSAGE s100(mj) WITH 'Select at least 1 line'.
EXIT.
ENDIF.


LOOP AT index_rows INTO sel_row.
SHIFT sel_row-index LEFT DELETING LEADING '0'.
CONCATENATE sel_row-index sel_line INTO sel_line
SEPARATED BY space.
ENDLOOP.
MESSAGE s100(mj) WITH 'Your selected row is' sel_line.


ENDFORM.


*-----------------------------------------------------------------------
* If any change in the table (Grid is edit capable) takes place,
* the new data is trapped hier and can be saved, processed and
* so on. Contrary to PAI Module, where the original data is still
* untouched, the content of the internal table has changed.
* Depending on your needs, you can save the content of the internal
* table to a database table with instructions like INSERT, UPDATE
* and MODIFY.
*-----------------------------------------------------------------------
FORM save_records.

CLEAR line.
DESCRIBE TABLE mytable LINES line.
IF line = 0.
MESSAGE w100(mj) WITH 'The internal table has no records'.
ENDIF.

* insert
* update
* modify

ENDFORM.

Nenhum comentário:

Postar um comentário