ALV grid is normally used to display the report/program output. But
there are cases where you need edit functionality in ALV grid.
Having
a editable column/ field is very simple in ALV grid. During field
catalog population just set the edi property of the column.
wa_fieldcat-edit = 'X'.
Now , Not so easy task is to code the logic to save the data after edit operation.
We have to use events for the same.
i_callback_user_command = gt_callback_subroutine
is
used to activate a subroutine 'USER_COMMAND' which will be triggered on
each user action on ALV (Mouse click , Button click etc).We need to
code in this subroutine.
SAVE button is having a function code '&DATA_SAVE'. So we need to check if sy-ucom = '&DATA_SAVE'.
Whenever
any edit is done in ALV grid , the same is reflected back in the
internal table used. So we can use the latest contents of internal table
to update/ do our operations. To get only those fields which are
modified , better to copy the internal table to some temp table and
compare each row with the new contents.
SO, here is the program incorporating all things explained.
TYPE-POOLS:icon. " Just include this |
TYPES: BEGIN OF ty_matnr, |
status TYPE char4, " Reserve a field for traffic lights |
rowcolor(4) TYPE c, "row colour |
DATA : wa_fieldcat TYPE slis_fieldcat_alv, "workspace |
it_fieldcat TYPE slis_t_fieldcat_alv, " Table |
it_mara TYPE STANDARD TABLE OF ty_matnr, |
it_maracp TYPE STANDARD TABLE OF ty_matnr, |
it_changes TYPE STANDARD TABLE OF ty_matnr, |
g_top_of_page TYPE slis_formname VALUE 'F_TOP_OF_PAGE', "for avl header. |
it_layout TYPE slis_layout_alv, |
gt_callback_subroutine TYPE slis_formname VALUE 'USER_COMMAND', |
SELECT-OPTIONS : s_matnr FOR mara-matnr. |
PARAMETERS: rd1 RADIOBUTTON GROUP rb DEFAULT 'X', |
rd2 RADIOBUTTON GROUP rb . |
IF s_matnr IS NOT INITIAL. |
IF it_mara IS NOT INITIAL. |
LOOP AT it_mara INTO wa_mara. |
wa_mara-rowcolor = 'C300'. |
wa_mara-status = icon_red_light. "Update the traffic lights as you wish, just go through the type pool you will find many colors and icons |
MODIFY it_mara FROM wa_mara. |
wa_mara-rowcolor = 'C600'. |
wa_mara-status = icon_green_light. |
MODIFY it_mara FROM wa_mara . |
* it_layout-info_fieldname = 'ROWCOLOR'. |
* it_layout-info_fieldname = 'ROWCOLOR'. |
*&---------------------------------------------------------------------* |
*&---------------------------------------------------------------------* |
*----------------------------------------------------------------------* |
wa_fieldcat-fieldname = 'MATNR'. |
wa_fieldcat-seltext_m = 'Material Value'. |
wa_fieldcat-hotspot = 'X'. |
APPEND wa_fieldcat TO it_fieldcat. |
wa_fieldcat-fieldname = 'ERSDA'. |
wa_fieldcat-seltext_m = 'Created On'. |
APPEND wa_fieldcat TO it_fieldcat. |
wa_fieldcat-fieldname = 'ERNAM'. |
wa_fieldcat-seltext_m = 'Created by'. |
APPEND wa_fieldcat TO it_fieldcat. |
wa_fieldcat-fieldname = 'MTART'. |
wa_fieldcat-seltext_m = 'Material Type'. |
APPEND wa_fieldcat TO it_fieldcat. |
wa_fieldcat-fieldname = 'STATUS'. |
wa_fieldcat-seltext_m = 'Status'. |
wa_fieldcat-icon = 'X'. " Display the field as ICON |
APPEND wa_fieldcat TO it_fieldcat. |
ENDFORM. "f_build_catalog |
*&---------------------------------------------------------------------* |
*&---------------------------------------------------------------------* |
*----------------------------------------------------------------------* |
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' |
* I_INTERFACE_CHECK = ' ' |
* I_BYPASSING_BUFFER = ' ' |
i_callback_program = sy-repid |
* I_CALLBACK_PF_STATUS_SET = ' ' |
i_callback_user_command = gt_callback_subroutine |
i_callback_top_of_page = g_top_of_page |
* I_CALLBACK_HTML_TOP_OF_PAGE = ' ' |
* I_CALLBACK_HTML_END_OF_LIST = ' ' |
it_fieldcat = it_fieldcat |
* I_SCREEN_START_COLUMN = 0 |
* I_SCREEN_STARlv_line = 0 |
* I_SCREEN_END_COLUMN = 0 |
* IR_SALV_FULLSCREEN_ADAPTER = |
* E_EXIT_CAUSED_BY_CALLER = |
* ES_EXIT_CAUSED_BY_USER = |
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno |
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. |
*&---------------------------------------------------------------------* |
*&---------------------------------------------------------------------* |
*----------------------------------------------------------------------* |
*----------------------------------------------------------------------* |
FORM user_command USING p_ucomm LIKE sy-ucomm |
p_selfield TYPE slis_selfield. |
"p_ucomm will hold user action like double click, clicking a button ,etc |
WHEN '&IC1'. " SAP standard code for double-clicking |
READ TABLE it_mara INTO wa_mara INDEX p_selfield-tabindex. " Getting Row data |
CASE p_selfield-fieldname. |
WHEN 'MATNR'. " Column data |
SET PARAMETER ID: 'MAT' FIELD p_selfield-value. |
CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN. |
MESSAGE p_selfield-value TYPE 'S'. |
MESSAGE 'Trying to save' TYPE 'S'. |
"It_changes hold all the change made in the ALV |
*&---------------------------------------------------------------------* |
*&---------------------------------------------------------------------* |
*----------------------------------------------------------------------* |
LOOP AT it_mara INTO wa_mara. |
READ TABLE it_maracp INTO wa_maracp index sy-tabix. |
APPEND wa_mara TO it_changes. |
*&---------------------------------------------------------------------* |
*&---------------------------------------------------------------------* |
*----------------------------------------------------------------------* |
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY' |
* I_INTERFACE_CHECK = ' ' |
i_callback_program = sy-cprog |
* I_CALLBACK_PF_STATUS_SET = ' ' |
i_callback_user_command = gt_callback_subroutine |
it_fieldcat = it_fieldcat |
* I_SCREEN_START_COLUMN = 0 |
* I_SCREEN_STARlv_line = 0 |
* I_SCREEN_END_COLUMN = 0 |
* I_SUPPRESS_EMPTY_DATA = ABAP_FALSE |
* E_EXIT_CAUSED_BY_CALLER = |
* ES_EXIT_CAUSED_BY_USER = |
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno |
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. |
*&---------------------------------------------------------------------* |
*&---------------------------------------------------------------------* |
*----------------------------------------------------------------------* |
DATA: it_header TYPE slis_t_listheader, |
wa_header TYPE slis_listheader, |
lv_line LIKE wa_header-info, |
wa_header-info = 'MARA Table Report'. |
APPEND wa_header TO it_header. |
wa_header-key = 'Date: '. |
CONCATENATE sy-datum+6(2) '.' |
sy-datum(4) INTO wa_header-info. "todays date |
APPEND wa_header TO it_header. |
* Total No. of Records Selected |
DESCRIBE TABLE it_mara LINES ld_lines. |
CONCATENATE 'Total No. of Records Selected: ' ld_linesc |
INTO lv_line SEPARATED BY space. |
wa_header-info = lv_line. |
APPEND wa_header TO it_header. |
CLEAR: wa_header, lv_line. |
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE' |
it_list_commentary = it_header. |
ENDFORM. "TOP-OF-PAGE
Serkan AKKAVAK
Computer Engineer
serkurumsal@yandex.com
Social Plugin