Update transparent table through ALV Grid.
Define Insert the data in simple table ZTEST_EDIT with following fields
- MANDTNUMC
- PERNR NUMC
- NAME CHAR
- SALARY CURR
Write Following Code in Report
REPORT ZMAKE_EDIT.
Tables:PA0001,ZTEST_EDIT.
TYPE-POOLS : slis.
Select-options: S_PERNR for PA0001-PERNR.
DATA: gstring TYPE c.
Data: stable TYPE lvc_s_stbl.
*Data declarations for ALV
DATA: c_ccont TYPE REF TO cl_gui_custom_container, "Custom container object
c_alvgd TYPE REF TO cl_gui_alv_grid, "ALV grid object
it_fcat TYPE lvc_t_fcat, "Field catalogue
it_layout TYPE lvc_s_layo.
Types:Begin of ty_edit,
*Include structure ZTEST_EDIT.
PERNR type ZTEST_EDIT-PERNR,
NAME type ZTEST_EDIT-NAME,
SALARY type ZTEST_EDIT-SALARY,
end of ty_edit.
Data:IT_EDIT type table of ty_edit,
WA_EDIT type ty_edit.
types:Begin of ty_edit1.
Include structure ZTEST_EDIT.
types: end of ty_edit1.
Data: IT_EDIT1 type table of ty_edit1,
WA_EDIT1 type ty_edit1.
START-OF-SELECTION.
select PERNR NAME SALARY into table it_edit from ZTEST_EDIT where PERNR in S_PERNR.
* Calling the ALV screen with custom container with name CCONT.
CALL SCREEN 0600.
*&---------------------------------------------------------------------*
*& Module STATUS_0600 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_0600 OUTPUT.
* SET PF-STATUS 'xxxxxxxx'.
* SET TITLEBAR 'xxx'.
"Creating objects of the container
CREATE OBJECT c_ccont
EXPORTING
container_name = 'CCONT'.
* create object for alv grid
create object c_alvgd
exporting
i_parent = c_ccont.
* SET field for ALV
PERFORM alv_build_fieldcat.
* Set ALV attributes FOR LAYOUT
PERFORM alv_report_layout.
CHECK NOT c_alvgd IS INITIAL.
* Call ALV GRID
CALL METHOD c_alvgd->set_table_for_first_display
EXPORTING
is_layout = it_layout
i_save = 'A'
CHANGING
it_outtab = IT_EDIT
it_fieldcatalog = it_fcat
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.
ENDMODULE. " STATUS_0600 OUTPUT
FORM alv_build_fieldcat.
DATA lv_fldcat TYPE lvc_s_fcat.
CLEAR lv_fldcat.
lv_fldcat-row_pos = '1'.
lv_fldcat-col_pos = '1'.
lv_fldcat-fieldname = 'PERNR'.
lv_fldcat-tabname = 'IT_EDIT'.
lv_fldcat-outputlen = 8.
lv_fldcat-scrtext_m = 'Employee Code'.
lv_fldcat-icon = 'X'.
APPEND lv_fldcat TO it_fcat.
CLEAR lv_fldcat.
lv_fldcat-row_pos = '1'.
lv_fldcat-col_pos = '2'.
lv_fldcat-fieldname = 'NAME'.
lv_fldcat-tabname = 'IT_EDIT'.
lv_fldcat-outputlen = 15.
lv_fldcat-scrtext_m = 'Name'.
lv_fldcat-icon = ''.
APPEND lv_fldcat TO it_fcat.
CLEAR lv_fldcat.
lv_fldcat-row_pos = '1'.
lv_fldcat-col_pos = '3'.
lv_fldcat-fieldname = 'SALARY'.
lv_fldcat-tabname = 'IT_EDIT'.
lv_fldcat-outputlen = 15.
lv_fldcat-scrtext_m = 'SALARY'.
lv_fldcat-edit = 'X'. "Make column Editable.
APPEND lv_fldcat TO it_fcat.
CLEAR lv_fldcat.
"ENDLOOP.
ENDFORM. "
FORM alv_report_layout.
it_layout-cwidth_opt = 'X'.
it_layout-col_opt = 'X'.
it_layout-zebra = 'X'.
ENDFORM.
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_0600 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0600 INPUT.
c_alvgd->check_changed_data( ).
*Based on the user input
*When user clicks 'SAVE;
CASE SY-UCOMM.
WHEN 'SAVE'.
*A pop up is called to confirm the saving of changed data
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
titlebar = 'SAVING DATA'
text_question = 'Do you want save the data?'
icon_button_1 = 'icon_booking_ok'
IMPORTING
answer = gstring
EXCEPTIONS
text_not_found = 1
OTHERS = 2.
IF sy-subrc NE 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
*When the User clicks 'YES'
IF ( gstring = '1' ).
MESSAGE ' Data Saved' TYPE 'S'.
**Now the changed data is stored in the it_pbo internal table
* "it_pbo = it_output.
* "clear it_table1.
loop at IT_edit into WA_EDIT.
wa_edit1-PERNR = wa_edit-PERNR.
wa_edit1-NAME = wa_edit-NAME.
wa_edit1-SALARY = wa_edit-SALARY.
append wa_edit1 to it_edit1.
clear wa_edit1.
endloop.
loop at IT_edit1 into WA_EDIT1.
*ZTEST_EDIT-PERNR = wa_edit1-PERNR.
* ZTEST_EDIT-NAME = wa_edit1-NAME.
*ZTEST_EDIT-SALARY = wa_edit1-SALARY.
update ZTEST_EDIT SET SALARY = wa_edit1-SALARY where PERNR = wa_edit1-PERNR.
commit work.
endloop.
*ZTEST_EDIT-pernr =
* UPDATE ZTEST_EDIT FROM TABLE it_edit1.
* commit work.
"break-point.
PERFORM redisplay.
ELSE.
*When user clicks NO or Cancel
MESSAGE 'Not Saved' TYPE 'S'.
ENDIF.
**When the user clicks the 'EXIT; he is out
WHEN 'EXIT'.
LEAVE PROGRAM.
ENDCASE.
CLEAR: sy-ucomm..
ENDMODULE. " PAI INPUT
FORM redisplay .
*Cells of the alv are made non editable after entering OK to save
CALL METHOD c_alvgd->set_ready_for_input
EXPORTING
i_ready_for_input = 0.
*Row and column of the alv are refreshed after changing values
stable-row = 'X'.
stable-col = 'X'.
*REfreshed ALV display with the changed values
*This ALV is non editable and contains new values
CALL METHOD c_alvgd->refresh_table_display
EXPORTING
is_stable = stable
EXCEPTIONS
finished = 1
OTHERS = 2.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.