Archive

Archive for November, 2008

Making ALV Grid Editable

November 18th, 2008

Actually this subject is included in BCALV_EDIT* in SAP/R3. I just want to make several conclusions, and make some notes on how to implement an ALV Editable Grid with the minimum method required to make it work. In this example I use a dynamic ALV Grid, so here are the steps:

1. You must create a screen (e.g. 9000) and create a CUSTOM_CTRL on that screen.
2. Add these variables to your program:

DATA: GRID TYPE REF TO CL_GUI_ALV_GRID,

CUST_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER,

G_LAYOUT TYPE LVC_S_LAYO,

G_FIELDCAT TYPE LVC_S_FCAT OCCURS 0,

GS_FIELDCAT TYPE LVC_S_FCAT.

3. You must define a class derived from CL_GUI_ALV_GRID to make custom methods that you can edit according to your needs.
To see the methods (and parameters needed) available for CL_GUI_ALV_GRID, you can double click on the class.
The code will look like this:

CLASS LCL_9000_GRIDEVENT DEFINITION.
PUBLIC SECTION.
METHODS:
HANDLE_DATACHANGE FOR EVENT DATA_CHANGED OF
CL_GUI_ALV_GRID IMPORTING
ER_DATA_CHANGED
E_ONF4
E_ONF4_BEFORE
E_ONF4_AFTER,

HANDLE_USERCOMMAND FOR EVENT USER_COMMAND OF
CL_GUI_ALV_GRID IMPORTING E_UCOMM.

ENDCLASS.

CLASS LCL_9000_GRIDEVENT IMPLEMENTATION.

METHOD HANDLE_DATACHANGE.
perform f_datachange using er_data_changed.
ENDMETHOD.

METHOD HANDLE_USERCOMMAND.
perform f_usercommand using e_ucomm.
ENDMETHOD.

ENDCLASS.

4. Create a variable that refers to the class created.

DATA: LCL_9000_GRIDEVENT TYPE REF TO LCL_9000_GRIDEVENT.

5. Create PBO for your screen. For example your PBO method name is PBO_9000. The code will look like this:

MODULE PBO_9000 OUTPUT.

IF CUST_CONTAINER IS INITIAL.

CREATE OBJECT CUST_CONTAINER
EXPORTING
CONTAINER_NAME = ‘CS_CTRL’.

CREATE OBJECT GRID
EXPORTING
I_PARENT = CUST_CONTAINER.

GS_REPID = SY-REPID.

*—This part will costumize the display of your grid
G_LAYOUT-GRID_TITLE = ‘Put your title here’.
G_LAYOUT-CWIDTH_OPT = ‘X’.
G_LAYOUT-no_rowins = ‘X’.
*—————————————————-

*—Build your field catalog for your internal table displayed
PERFORM F_9000_BUILD.
*—————————————————-

CALL METHOD GRID->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = G_LAYOUT
CHANGING
IT_OUTTAB = T_DATA
IT_FIELDCATALOG = G_FIELDCAT.

*—–These are the key methods called for editable ALV Grid to *—–work properly
CALL METHOD GRID->SET_READY_FOR_INPUT
EXPORTING
I_READY_FOR_INPUT = 1.

call method grid->register_edit_event
exporting
i_event_id = cl_gui_alv_grid=>mc_evt_enter.
*———————————————————————–

*——————attach your grid to the EVENT HANDLER
CREATE OBJECT LCL_9000_GRIDEVENT.
set handler LCL_9000_GRIDEVENT->handle_datachange for grid.
set handler LCL_9000_GRIDEVENT->handle_usercommand for grid.
*———————————————————

ENDIF.

CALL METHOD GRID->SET_FRONTEND_FIELDCATALOG
EXPORTING
IT_FIELDCATALOG = G_FIELDCAT.
CALL METHOD GRID->REFRESH_TABLE_DISPLAY.

CALL METHOD CL_GUI_CONTROL=>SET_FOCUS EXPORTING CONTROL = GRID.

ENDMODULE. ” PBO_9000 OUTPUT

6. You need to fill in G_FIELDCAT variable. For example, you want to make an internal table contains material id and text. Material ID will be editable but not the text because material text will be obtained from MAKT table. This is the implementation looks like:

FORM F_9000_BUILD.
clear GS_FIELDCAT.
GS_FIELDCAT-fieldname = ‘MATNR’.
GS_FIELDCAT-ref_table = ‘MARA’.
GS_FIELDCAT-ref_field = ‘MATNR’.
GS_FIELDCAT-EDIT = ‘X’.
append GS_FIELDCAT TO G_FIELDCAT.

CLEAR GS_FIELDCAT.
GS_FIELDCAT-fieldname = ‘MAKTX’.
GS_FIELDCAT-ref_table = ‘MAKT’.
GS_FIELDCAT-ref_field = ‘MAKTX’.
append GS_FIELDCAT TO G_FIELDCAT.
ENDFORM

Done! You’re Editable ALV Grid is ready to be used. Enjoy!

vera ABAP ,