Posted: Wed Sep 05, 2007 6:01 pm Post subject: ALV-Editing and saving the edited values in Database
Author: J.Jayanthi
Description:
This document describes how to make the column as editable get the edited values in internal table save the edited values in database and refreshing the grid for display in ALV using OOPS method.
This document considers a scenario that after output is displayed, user edits the Material description and then he will select the edited rows. Once he presses save, that edited values will be updated in database.
Applies to
SAP R/3 Enterprise Version
Procedure
Step 1: Make the column as Editable by modifying the fieldcatalog table.
* Making the column as ediable
LOOP AT i_fieldcat ASSIGNING <fs_fieldcat>.
CASE <fs_fieldcat>-fieldname.
WHEN 'MAKTX'.
* Making a column as Editable
<fs_fieldcat>-edit = 'X'.
ENDCASE.
ENDLOOP.
Step 2: In PBO, code should be written to register the edit.
Step 3: In PAI, write the code to capture the selected rows in internal table and then save it to database(once SAVE button is clicked).
MODULE user_command_9000 INPUT.
DATA lv_ucomm TYPE sy-ucomm.
lv_ucomm = sy-ucomm.
CASE lv_ucomm.
.....
WHEN 'SAVE'.
perform save_database.
CALL METHOD O_GRID->REFRESH_TABLE_DISPLAY.
ENDCASE.
ENDMODULE. " USER_COMMAND_9000 INPUT
FORM save_database .
* Getting the selected rows index
CALL METHOD O_GRID->GET_SELECTED_ROWS
IMPORTING
ET_INDEX_ROWS = i_selected_rows.
* Through the index capturing the values of selected rows
loop at i_selected_rows into w_selected_rows.
read table itab into wa index w_selected_rows-index.
if sy-subrc eq 0.
move-corresponding wa to w_modified.
append w_modified to i_modified.
endif.
endloop.
modify zzzmaterial from table i_modified.
endform. " save_database
Complete Code
Screen 9000, GUI Status ZSTATUS and GUI Title ZTITLE should be created and in Flow logic of the screen, PBO and PAI should be uncommented.
Code:
* Data declarations
DATA : itab TYPE STANDARD TABLE OF zzzmaterial,"Output table
i_fieldcat TYPE STANDARD TABLE OF lvc_s_fcat,"Field catalog
i_selected_rows TYPE lvc_t_row,"Selected Rows
w_selected_rows TYPE lvc_s_row,
i_modified TYPE STANDARD TABLE OF zzzmaterial,"For getting modified rows
w_modified TYPE zzzmaterial,
wa TYPE zzzmaterial,
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
FIELD-SYMBOLS : <fs_fieldcat> TYPE lvc_s_fcat.
TABLES mara.
SELECT-OPTIONS s_matnr FOR mara-matnr.
SELECT * FROM zzzmaterial INTO TABLE itab WHERE matnr IN s_matnr.
CALL SCREEN 9000.
*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
* PBO
*----------------------------------------------------------------------*
MODULE status_9000 OUTPUT.
IF o_docking IS INITIAL.
SET PF-STATUS 'ZSTATUS'. "GUI Status
SET TITLEBAR 'ZTITLE'. "Title
* Creating Docking Container and grid
PERFORM create_object.
* Filling the fieldcatalog table
PERFORM create_fieldcat.
* Modifying the fieldcatalog table
PERFORM modify_fieldcat.
* Registering edit
PERFORM register_edit.
* Displaying the output
PERFORM display_output.
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.
WHEN 'SAVE'.
PERFORM save_database.
CALL METHOD o_grid->refresh_table_display.
ENDCASE.
ENDMODULE. " USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
*& Form create_object
*&---------------------------------------------------------------------*
* Creating Docking Container and grid
*----------------------------------------------------------------------*
FORM create_object .
* 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.
ENDFORM. " create_object
*&---------------------------------------------------------------------*
*& Form create_fieldcat
*&---------------------------------------------------------------------*
* Filling the fieldcatalog table
*----------------------------------------------------------------------*
FORM create_fieldcat .
* Filling the fieldcatalog table
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'ZZZMATERIAL'
CHANGING
ct_fieldcat = i_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
ENDFORM. " create_fieldcat
*&---------------------------------------------------------------------*
*& Form modify_fieldcat
*&---------------------------------------------------------------------*
* Making the column as ediable
*----------------------------------------------------------------------*
FORM modify_fieldcat .
LOOP AT i_fieldcat ASSIGNING <fs_fieldcat>.
CASE <fs_fieldcat>-fieldname.
WHEN 'MAKTX'.
* Making a column as Editable
<fs_fieldcat>-edit = 'X'.
ENDCASE.
ENDLOOP.
ENDFORM. " modify_fieldcat
*&---------------------------------------------------------------------*
*& Form register_edit
*&---------------------------------------------------------------------*
* Registering Edit
*----------------------------------------------------------------------*
FORM register_edit .
CALL METHOD o_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified.
ENDFORM. " register_edit
*&---------------------------------------------------------------------*
*& Form display_output
*&---------------------------------------------------------------------*
* Displaying the output
*----------------------------------------------------------------------*
FORM display_output .
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.
ENDFORM. " display_output
*&---------------------------------------------------------------------*
*& 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
*&---------------------------------------------------------------------*
*& Form save_database
*&---------------------------------------------------------------------*
* Save in database
*----------------------------------------------------------------------*
FORM save_database .
* Getting the selected rows index
CALL METHOD o_grid->get_selected_rows
IMPORTING
et_index_rows = i_selected_rows.
* Through the index capturing the values of selected rows
LOOP AT i_selected_rows INTO w_selected_rows.
READ TABLE itab INTO wa INDEX w_selected_rows-index.
IF sy-subrc EQ 0.
MOVE-CORRESPONDING wa TO w_modified.
APPEND w_modified TO i_modified.
ENDIF.
ENDLOOP.
MODIFY zzzmaterial FROM TABLE i_modified.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.