June 28, 2009

ALV using OOPS method

This document describes how to create an ALV using OOPS method in few steps. It will help the beginners to start with.

Procedure

Step 1: Create a container. There are 2 containers. They are docking and custom.

For eg.. Create docking container.

Go to SE38.Create a program. Use Pattern button to create object for docking container. Click ABAP Object Pattern radio button.

CREATE OBJECT o_docking

EXPORTING

* PARENT =

* REPID =

* DYNNR =

* SIDE = DOCK_AT_LEFT

* EXTENSION = 50

* STYLE =

* LIFETIME = lifetime_default

* CAPTION =

* METRIC = 0

RATIO = '95'

* NO_AUTODEF_PROGID_DYNNR =

* NAME =

* EXCEPTIONS

* CNTL_ERROR = 1

* CNTL_SYSTEM_ERROR = 2

* CREATE_ERROR = 3

* LIFETIME_ERROR = 4

* LIFETIME_DYNPRO_DYNPRO_LINK = 5

* others = 6

.

IF sy-subrc <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Step 2: Create a grid inside the container.

Use Pattern button to create the same. Make the parent of grid as container.

CREATE OBJECT o_grid

EXPORTING

* I_SHELLSTYLE = 0

* I_LIFETIME =

i_parent = o_docking

* I_APPL_EVENTS = space

* I_PARENTDBG =

* I_APPLOGPARENT =

* I_GRAPHICSPARENT =

* I_NAME =

* EXCEPTIONS

* ERROR_CNTL_CREATE = 1

* ERROR_CNTL_INIT = 2

* ERROR_CNTL_LINK = 3

* ERROR_DP_CREATE = 4

* others = 5

.

IF sy-subrc <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Step 3: Call the function LVC_FIELDCATALOG_MERGE to get the fieldcatalog.

Pass the structure name.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

* I_BUFFER_ACTIVE =

I_STRUCTURE_NAME = 'MARA'

* I_CLIENT_NEVER_DISPLAY = 'X'

* I_BYPASSING_BUFFER =

* I_INTERNAL_TABNAME =

CHANGING

ct_fieldcat = i_fieldcat

* EXCEPTIONS

* INCONSISTENT_INTERFACE = 1

* PROGRAM_ERROR = 2

* OTHERS = 3

.

IF sy-subrc <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Step 4: Call the method of grid set_table_for_first_display to display the output.

w_variant-report = sy-repid.

CALL METHOD o_grid->set_table_for_first_display

EXPORTING

* I_BUFFER_ACTIVE =

* I_BYPASSING_BUFFER =

* I_CONSISTENCY_CHECK =

* I_STRUCTURE_NAME =

IS_VARIANT = w_variant

I_SAVE = 'A'

* I_DEFAULT = 'X'

* IS_LAYOUT =

* IS_PRINT =

* IT_SPECIAL_GROUPS =

* IT_TOOLBAR_EXCLUDING =

* IT_HYPERLINK =

* IT_ALV_GRAPHICS =

* IT_EXCEPT_QINFO =

CHANGING

it_outtab = itab

IT_FIELDCATALOG = i_fieldcat

* IT_SORT =

* IT_FILTER =

* EXCEPTIONS

* INVALID_PARAMETER_COMBINATION = 1

* PROGRAM_ERROR = 2

* TOO_MANY_LINES = 3

* others = 4

.

IF sy-subrc <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

Step 5: Fill the internal table itab with values by using logic.

select * from mara into table itab up to 100 rows.

call screen 9000.

Create a screen by double clicking 9000 in the above line. Fill the description for the screen. In the flow logic, uncomment the PBO and PAI module and create those in main program (for simplicity).

Step 6: Create GUI status as below. Create GUI Title if required.

Step 7: Free the memory occupied once the 'BACK, EXIT' or 'CANCEL' button is clicked. Use Pattern button to call the method 'FREE' of cl_gui_alv_grid.

Complete Code

data : itab type standard table of mara,"Output Internal table

i_fieldcat type standard table of lvc_s_fcat,"Field catalog

wa type mara,

w_variant type disvariant,

o_docking type ref to cl_gui_docking_container,"Docking Container

o_grid type ref to cl_gui_alv_grid."Grid

select * from mara into table itab up to 100 rows.

call screen 9000.

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

*& Module STATUS_9000 OUTPUT

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

* text

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

module STATUS_9000 output.
if o_docking is initial.
SET PF-STATUS 'ZSTATUS'. "GUI Status

SET TITLEBAR 'ZTITLE'. "Title

* Creating Docking Container

CREATE OBJECT o_docking

EXPORTING

RATIO = '95'.

IF sy-subrc eq 0.

* Creating Grid

CREATE OBJECT o_grid

EXPORTING

i_parent = o_docking.

ENDIF.

* Filling the fieldcatalog table

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

I_STRUCTURE_NAME = 'MARA'

CHANGING

ct_fieldcat = i_fieldcat

EXCEPTIONS

INCONSISTENT_INTERFACE = 1

PROGRAM_ERROR = 2

OTHERS = 3.

w_variant-report = sy-repid.

* Displaying the output

CALL METHOD o_grid->set_table_for_first_display

EXPORTING

IS_VARIANT = w_variant

I_SAVE = 'A'

CHANGING

it_outtab = itab

IT_FIELDCATALOG = i_fieldcat

EXCEPTIONS

INVALID_PARAMETER_COMBINATION = 1

PROGRAM_ERROR = 2

TOO_MANY_LINES = 3

others = 4.

IF sy-subrc <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

endif.

endmodule. " STATUS_9000 OUTPUT

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

*& Module USER_COMMAND_9000 INPUT

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

* PAI

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

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.

ENDCASE.

endmodule. " USER_COMMAND_9000 INPUT

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

*& Form free_objects

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

* Free Objects

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

form free_objects .

CALL METHOD o_grid->free

EXCEPTIONS

CNTL_ERROR = 1

CNTL_SYSTEM_ERROR = 2

others = 3.

IF sy-subrc <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

CALL METHOD o_docking->free

EXCEPTIONS

CNTL_ERROR = 1

CNTL_SYSTEM_ERROR = 2

others = 3.

IF sy-subrc <> 0.

MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

endform. " free_objects

source : https://wiki.sdn.sap.com/wiki/display/Snippets/ABAP-7%20Steps%20to%20create%20OOPS%20ALV(for%20beginners)


No comments:

Post a Comment