Wednesday, June 11, 2008

ALV Using Classes with Events




Screen 100.
create three buttons.
and give the function codes like this.

function code for 'ALV with double click event' button is DIALOG1

function code for 'ALV with hotspot event' button is DIALOG2

screen 101 ( popup screen):
create custom container and name it as ALV_CONTAINER

code:
REPORT ZTEST_ALV_CHECK.
class lcl_event_receiver definition deferred.

data: ok_code like sy-ucomm,
gt_sflight type table of sflight,
gt_sbook type table of sbook,
g_max type i value 100,
g_repid like sy-repid,
gs_layout type lvc_s_layo,
gt_fieldcat type lvc_t_fcat with header line,
cont_on_main type scrfname value 'ALV_CONTAINER',
grid1 type ref to cl_gui_alv_grid,
custom_container1 type ref to cl_gui_custom_container,
event_receiver type ref to lcl_event_receiver.

DATA: it_fieldcat TYPE lvc_t_fcat,
wa_fieldcat TYPE lvc_s_fcat.

call screen 100.
* class definition
class lcl_event_receiver definition.
public section.
* declaration for double click event.
methods handle_double_click
for event double_click of cl_gui_alv_grid
importing e_row e_column.
* declaration for Hotspot event.
methods on_hotspot_click
for event hotspot_click of cl_gui_alv_grid
importing
!e_row_id
!es_row_no .
endclass.
* class implementation
class lcl_event_receiver implementation.
* for hotspot event
method on_hotspot_click.
data: ls_sflight like line of gt_sflight.
read table gt_sflight index e_row_id-index into
ls_sflight.

perform select_table_sbook using ls_sflight
changing gt_sbook.
call transaction 'SE11'.

endmethod. "on_hotspot_click

* for double click event
method handle_double_click.
data: ls_sflight like line of gt_sflight.

read table gt_sflight index e_row-index into ls_sflight.

perform select_table_sbook using ls_sflight
changing gt_sbook.
call transaction 'SE11'.

endmethod. "handle_double_click
endclass.



*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------*
module USER_COMMAND_0100 input.
case ok_code.
when 'DIALOG'.
call screen 101 starting at 10 5 ending at 100 30. " popup window
when 'DIALOG1'.
call screen 101 starting at 10 5 ending at 100 30.
when 'EXIT'.
leave program.
endcase.
endmodule. " USER_COMMAND_0100 INPUT

*&---------------------------------------------------------------------*
*& Module STATUS_0101 OUTPUT
*&---------------------------------------------------------------------*
module STATUS_0101 output.
g_repid = sy-repid.
if custom_container1 is initial.
perform select_table_sflight.
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'(510).
endif.

* create an instance of alv control
create object grid1
exporting i_parent = custom_container1.

* change title
gs_layout-grid_title = 'Bookings'(101).

* adjust field catalog
* to suppress the output of already displayed key fields of sflight
perform mask_columns.
call method grid1->set_table_for_first_display
exporting
is_layout = gs_layout
changing
it_fieldcatalog = it_fieldcat[]
it_outtab = gt_sflight.

else.

call method grid1->refresh_table_display.

endif. "IF grid1 IS INITIAL.
* for hotspot and double click events.
if ok_code = 'DIALOG1'.
create object event_receiver.
set handler event_receiver->on_hotspot_click for grid1.
else.
create object event_receiver.
set handler event_receiver->handle_double_click for grid1.
endif.

call method cl_gui_control=>set_focus
exporting control = grid1.
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).
endif.

endmodule. " STATUS_0101 OUTPUT

*&---------------------------------------------------------------------*
*& Form SELECT_TABLE_SFLIGHT
*&---------------------------------------------------------------------*
form select_table_sflight .
select * from sflight into table gt_sflight up to g_max rows.
endform. " SELECT_TABLE_SFLIGHT

*&---------------------------------------------------------------------*
*& Form SELECT_TABLE_SBOOK
*&---------------------------------------------------------------------*
form select_table_sbook using p_ls_sflight like line of gt_sflight
changing p_gt_sbook like gt_sbook[].

select * from sbook into table p_gt_sbook
where carrid = p_ls_sflight-carrid
and connid = p_ls_sflight-connid
and fldate = p_ls_sflight-fldate.

endform. " SELECT_TABLE_SBOOK

*&---------------------------------------------------------------------*
*& Form mask_columns
*&---------------------------------------------------------------------*
form mask_columns.
CLEAR wa_fieldcat.
wa_fieldcat-col_pos = '1'.
wa_fieldcat-fieldname = 'CARRID'.
wa_fieldcat-coltext = 10.
wa_fieldcat-seltext = 'CARRID'.
if ok_code = 'DIALOG1'.
wa_fieldcat-hotspot = 'X'.
endif.
APPEND wa_fieldcat TO it_fieldcat.

CLEAR wa_fieldcat.
wa_fieldcat-col_pos = '2'.
wa_fieldcat-fieldname = 'CONNID'.
wa_fieldcat-outputlen = 10.
wa_fieldcat-coltext = 'CONNID'.
APPEND wa_fieldcat TO it_fieldcat.

CLEAR wa_fieldcat.
wa_fieldcat-col_pos = '3'.
wa_fieldcat-fieldname = 'FLDATE'.
wa_fieldcat-outputlen = 3.
wa_fieldcat-coltext = 'FLDATE'.
APPEND wa_fieldcat TO it_fieldcat.


endform. " mask_columns

*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0101 INPUT
*&---------------------------------------------------------------------*
module USER_COMMAND_0101 input.
case ok_code.
when 'BACK'.
clear ok_code.
leave to screen 0.
endcase.

endmodule. " USER_COMMAND_0101 INPUT

No comments: