Thursday, October 18, 2007

Sample ABAP Program of ALV Grid control using OO methods

*&---------------------------------------------------------------------*
*& Report Z_ALV_SKELETON
*&
*& Author: Sheila Titchener
*& Date: December 2006
*&---------------------------------------------------------------------*
*& ALV Grid control skeleton using OO Methods
*&
*&---------------------------------------------------------------------*
REPORT Z_ALV_SKELETON .
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
* internal tables
DATA: i_tab TYPE TABLE OF zchangereq,
w_tab TYPE zchangereq.
* display field details
DATA: w_field_cat_wa TYPE lvc_s_fcat. " Field Catalog work area
*
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-020.
*&---------------------------------------------------------------------*
PARAMETERS: p_outs RADIOBUTTON GROUP g1,
p_comp RADIOBUTTON GROUP g1.
SELECTION-SCREEN END OF BLOCK b1.
*&---------------------------------------------------------------------*
*MACROS
*&---------------------------------------------------------------------*
DEFINE add_field.
* &1 = FIELDNAME &2 = HEADING &3 = Key field flag
clear w_field_cat_wa.
w_field_cat_wa-fieldname = &1.
* lw_field_cat_wa-ref_table = p_ref_table.
* lw_field_cat_wa-inttype = p_inttype.
* lw_field_cat_wa-decimals = p_decimals.
* lw_field_cat_wa-coltext = p_coltext.
* lw_field_cat_wa-seltext = p_seltext.
* lw_field_cat_wa-do_sum = p_do_sum.
* lw_field_cat_wa-no_out = p_no_out.
* lw_field_cat_wa-col_pos = p_col_pos.
* lw_field_cat_wa-reptext = p_coltext.
* lw_field_cat_wa-colddictxt = p_colddictxt.
w_field_cat_wa-scrtext_m = &2.
w_field_cat_wa-key = &3.
append w_field_cat_wa to i_field_cat.
END-OF-DEFINITION.
*-----------------------------------------------------------------------
* ALV specific Declarations...........................................
*-----------------------------------------------------------------------
* ALV specific Internal table declarations.............................
DATA: i_field_cat TYPE lvc_t_fcat, " Field catalogue
i_alv_sort TYPE lvc_t_sort. " Sort table

* ALV variables........................................................
DATA: w_alv_layout TYPE lvc_s_layo, " ALV Layout
w_alv_save TYPE c, " ALV save
w_alv_variant TYPE disvariant. " ALV Variant

* ALV Class definitions................................................
*-----------------------------------------------------------------------
CLASS lcl_event_handler DEFINITION.
*-----------------------------------------------------------------------

PUBLIC SECTION.
METHODS: handle_double_click
FOR EVENT double_click OF cl_gui_alv_grid
IMPORTING e_row e_column.
METHODS: handle_hotspot
FOR EVENT hotspot_click OF cl_gui_alv_grid
IMPORTING e_row_id e_column_id.
ENDCLASS. " CLASS LCL_EVENT_HANDLER DEF..

* ALV Class implementation............................................
* In the Event of a Double Click drill down to the corresponding CHANGE REQUEST
*-----------------------------------------------------------------------
CLASS lcl_event_handler IMPLEMENTATION.
*-----------------------------------------------------------------------

METHOD handle_double_click.
DATA: l_tab LIKE LINE OF i_tab.
CHECK e_row-rowtype(1) EQ space.
READ TABLE i_tab INDEX e_row-index INTO l_tab.
SET PARAMETER ID 'ZTY' FIELD l_tab-ztype.
SET PARAMETER ID 'ZCR' FIELD l_tab-zcref.
CALL TRANSACTION 'ZCRT' AND SKIP FIRST SCREEN.
ENDMETHOD. " HANDLE_DOUBLE_CLICK

* not working yet - hotspot seems to stay in the gui!!
METHOD handle_hotspot.
DATA: l_tab LIKE LINE OF i_tab.
CHECK e_row_id-rowtype(1) EQ space.
READ TABLE i_tab INDEX e_row_id-index INTO l_tab.
SET PARAMETER ID 'ZTY' FIELD l_tab-ztype.
SET PARAMETER ID 'ZCR' FIELD l_tab-zcref.
CALL TRANSACTION 'ZCRT' AND SKIP FIRST SCREEN.
ENDMETHOD. " HANDLE_DOUBLE_CLICK

ENDCLASS. " CLASS LCL_EVENT_HANDLER IMPL...

* ALV Grid Control definitions........................................
DATA:
* ALV Grid Control itself
o_grid TYPE REF TO cl_gui_alv_grid,

* Container to hold the ALV Grid Control
o_custom_container TYPE REF TO cl_gui_custom_container,

* Event handler (defined in the class above)
o_event_handler TYPE REF TO lcl_event_handler.

*&----------------------------------------------------------------------*
INITIALIZATION.
*&----------------------------------------------------------------------*
PERFORM create_field_catalogue. " Create ALV Field Catalog

*&---------------------------------------------------------------------*
START-OF-SELECTION.
*&---------------------------------------------------------------------*
* Outstanding requests
IF p_outs = 'X'.
SELECT * FROM zchangereq
INTO TABLE i_tab
WHERE zstatus < 99.
ELSE.
* completed requests
SELECT * FROM zchangereq
INTO TABLE i_tab
WHERE zstatus = 99.
ENDIF.
*&---------------------------------------------------------------------*
END-OF-SELECTION.
*&---------------------------------------------------------------------*
PERFORM list_output_to_alv. " Perform ALV Output operations
*----------------------------------------------------------------------*
* FORM LIST_OUTPUT_TO_ALV *
*----------------------------------------------------------------------*
* This subroutine is used to call the screen for ALV Output. *
*----------------------------------------------------------------------*
* There are no interface parameters to be passed to this subroutine. *
*----------------------------------------------------------------------*
FORM list_output_to_alv.

CALL SCREEN 100.

*
ENDFORM. " LIST_OUTPUT_TO_ALV
*----------------------------------------------------------------------*
* Module STATUS_0100 OUTPUT *
*----------------------------------------------------------------------*
* This is the PBO module which will be processed befor displaying the *
* ALV Output. *
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.

SET PF-STATUS '0100'. " PF Status for ALV Output Screen
IF p_outs = 'X'.
SET TITLEBAR 'STD'.
ELSE.
* completed requests
SET TITLEBAR 'COMP'.
ENDIF.

CREATE OBJECT: o_custom_container
EXPORTING container_name = 'SC_GRID',
o_grid
EXPORTING i_parent = o_custom_container.

PERFORM define_alv_layout. " ALV Layout options definitions

PERFORM save_alv_layout_options. " save ALV layout options

PERFORM call_alv_grid. " Call ALV Grid Control

ENDMODULE. " STATUS_0100 OUTPUT

*----------------------------------------------------------------------*
* Module USER_COMMAND_0100 INPUT *
*----------------------------------------------------------------------*
* This is the PAI module which will be processed when the user performs*
* any operation from the ALV output. *
*----------------------------------------------------------------------*

MODULE user_command_0100 INPUT.

CASE sy-ucomm.
WHEN 'EXIT' OR 'BACK' OR 'CANC'.
* may need to do this so display is refreshed if other report selected
CALL METHOD o_custom_container->free.
SET SCREEN 0.
ENDCASE.

ENDMODULE. " USER_COMMAND_0100 INPUT

*----------------------------------------------------------------------*
* FORM DEFINE_ALV_LAYOUT *
*----------------------------------------------------------------------*
* This subroutine is used to Define the ALV layout. *
*----------------------------------------------------------------------*
FORM define_alv_layout .

w_alv_layout-numc_total = 'X'. " Numc total line
w_alv_layout-cwidth_opt = 'X'. " Optimal column width
w_alv_layout-detailinit = 'X'. " Show values that are initial in
" detail list.
w_alv_layout-sel_mode = 'A'. " Column selection mode
w_alv_layout-no_merging = 'X'. " No merging while sorting columns
w_alv_layout-keyhot = 'X'.
ENDFORM. " DEFINE_ALV_LAYOUT
*----------------------------------------------------------------------*
* FORM SAVE_ALV_LAYOUT_OPTIONS *
*----------------------------------------------------------------------*
* This subroutine is used to Save the ALV layout options. *
*----------------------------------------------------------------------*
FORM save_alv_layout_options.

* See the ALV grid control documentation for full list of options

w_alv_save = 'A'.
w_alv_variant-report = sy-repid.


ENDFORM. " SAVE_ALV_LAYOUT_OPTIONS
*----------------------------------------------------------------------*
* FORM CALL_ALV_GRID *
*----------------------------------------------------------------------*
* This subroutine is used to call ALV Grid control for processing. *
*----------------------------------------------------------------------*
FORM call_alv_grid.

CALL METHOD o_grid->set_table_for_first_display
EXPORTING
is_layout = w_alv_layout
i_save = w_alv_save
is_variant = w_alv_variant
CHANGING
it_outtab = i_tab[]
it_sort = i_alv_sort
it_fieldcatalog = i_field_cat.
* Link used Events and Event Handler Methods
CREATE OBJECT o_event_handler.

* Set handler o_event_handler->handle_top_of_page for o_grid.
SET HANDLER o_event_handler->handle_double_click FOR o_grid.

ENDFORM. " CALL_ALV_GRID
*&---------------------------------------------------------------------*
*& Form create_field_catalogue
*&---------------------------------------------------------------------*
* set up field catalogue
*&---------------------------------------------------------------------*
FORM create_field_catalogue .
* Fieldname Heading Key?
*eg add_field 'ZTYPE' 'Type' 'X'.


ENDFORM. " create_field_catalogue

No comments: