ALV除了可以实现列表的显示之外,它还有个编辑的功能,可以对ALV显示列表的内容进行增删改等动作。
ALV有三种方法实现,其中除了用CL_SALV_TABLE实现的ALV没有编辑的功能之外,另外两种都可以实现编辑功能,另外两种分别是用REUSE_ALV_GRID_DISPLAY和CL_GUI_ALV_GRID。
这次我们要讲的是用REUSE_ALV_GRID_DISPLAY实现的ALV编辑功能,其实很简单,只能在把它的一个参数(SLIS_LAYOUT_ALV-EDIT = 'X')打上勾就可以了,ALV列表就全部变成可编辑的状态了。我们还可以控制ALV列表的某一列可编辑,或者某一行某一个单元格可编辑,这个功能用REUSE_ALV_GRID_DISPLAY实现不了,需要用到它的改进版本REUSE_ALV_GRID_DISPLAY_LVC,下面的例子就是用这个Function实现的
源代码:
TYPE-POOLS slis.
DATA: BEGIN OF gs_style,
style TYPE lvc_t_styl,
END OF gs_style.
TYPES BEGIN OF ty_spfli.
INCLUDE STRUCTURE spfli.
INCLUDE STRUCTURE gs_style.
TYPES END OF ty_spfli.
TYPES ty_tab_spfli TYPE ty_spfli OCCURS 0.
DATA gt_spfli TYPE ty_tab_spfli.
DATA gr_grid TYPE REF TO cl_gui_alv_grid.
START-OF-SELECTION.
PERFORM f_display.
*----------------------------------------------------------------------*
* CLASS lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.
PUBLIC SECTION.
METHODS:
handle_modify
FOR EVENT data_changed_finished OF cl_gui_alv_grid
IMPORTING e_modified et_good_cells.
ENDCLASS. "LCL_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------------*
* CLASS LCL_EVENT_RECEIVER IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.
METHOD handle_modify.
DATA stbl TYPE lvc_s_stbl.
DATA lwa_cell LIKE LINE OF et_good_cells.
stbl-row = 'X'."
stbl-col = 'X'."
LOOP AT et_good_cells INTO lwa_cell WHERE fieldname = 'FLTIME'.
CALL METHOD gr_grid->refresh_table_display
EXPORTING
is_stable = stbl.
ENDLOOP.
ENDMETHOD. "HANDLE_MODIFY
ENDCLASS. "LCL_EVENT_RECEIVER IMPLEMENTATION
*&---------------------------------------------------------------------*
*& Form f_getdata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PT_SPFLI text
*----------------------------------------------------------------------*
FORM f_getdata TABLES pt_spfli TYPE ty_tab_spfli.
FIELD-SYMBOLS <fs_spfli> LIKE LINE OF pt_spfli.
DATA: lt_style TYPE lvc_t_styl,
lwa_style LIKE LINE OF lt_style.
SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE pt_spfli.
LOOP AT pt_spfli ASSIGNING <fs_spfli> WHERE countryto = 'US'.
CLEAR lwa_style.
REFRESH lt_style.
lwa_style-fieldname = 'COUNTRYTO'.
lwa_style-style = cl_gui_alv_grid=>mc_style_enabled.
APPEND lwa_style TO lt_style.
<fs_spfli>-style = lt_style.
ENDLOOP.
ENDFORM. "f_getdata
*&---------------------------------------------------------------------*
*& Form f_display
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM f_display.
DATA: lt_fcat TYPE lvc_t_fcat,
ls_layout TYPE lvc_s_layo,
lt_event TYPE slis_t_event,
ls_glay TYPE lvc_s_glay.
PERFORM f_getdata TABLES gt_spfli.
PERFORM f_layout_build USING ls_layout.
PERFORM f_fcat_build TABLES lt_fcat.
PERFORM f_events_build TABLES lt_event.
PERFORM f_glay USING ls_glay.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
i_callback_user_command = 'F_USER_COMMAND'
it_fieldcat_lvc = lt_fcat
it_events = lt_event
is_layout_lvc = ls_layout
i_grid_settings = ls_glay
TABLES
t_outtab = gt_spfli
EXCEPTIONS
program_error = 1.
ENDFORM. "f_display
*&---------------------------------------------------------------------*
*& Form f_events_build
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PT_EVENTS text
*----------------------------------------------------------------------*
FORM f_events_build TABLES pt_events TYPE slis_t_event.
DATA lwa_event LIKE LINE OF pt_events.
lwa_event-name = 'CALLER_EXIT'.
lwa_event-form = 'F_CALLER_EXIT_AT_START'.
APPEND lwa_event TO pt_events.
ENDFORM. "f_events_build
*&---------------------------------------------------------------------*
*& Form f_fcat_build
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PT_FCAT text
*----------------------------------------------------------------------*
FORM f_fcat_build TABLES pt_fcat TYPE lvc_t_fcat.
DATA ls_fcat LIKE LINE OF pt_fcat.
DEFINE set_column.
clear ls_fcat.
ls_fcat-fieldname = &1.
ls_fcat-scrtext_l = &2.
ls_fcat-edit = &3.
ls_fcat-do_sum = &4.
append ls_fcat to pt_fcat.
END-OF-DEFINITION.
set_column 'CARRID' 'Airline Code' '' ''.
set_column 'CONNID' 'Flight Connection Number' '' ''.
set_column 'COUNTRYFR' 'Country Key' '' ''.
set_column 'CITYFROM' 'Departure city' '' ''.
set_column 'AIRPFROM' 'Departure airport' '' ''.
set_column 'COUNTRYTO' 'Country Key' '' ''.
set_column 'CITYTO' 'Arrival city' '' ''.
set_column 'AIRPTO' 'Destination airport' '' ''.
set_column 'FLTIME' 'Flight time' 'X' 'X'.
set_column 'DEPTIME' 'Departure time' '' ''.
set_column 'ARRTIME' 'Arrival time' '' ''.
set_column 'DISTANCE' 'Distance' 'X' ''.
ENDFORM. "f_fcat_build
*&---------------------------------------------------------------------*
*& Form f_layout_build
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PT_LAYOUT text
*----------------------------------------------------------------------*
FORM f_layout_build USING p_layout TYPE lvc_s_layo.
* p_layout-edit = 'X'.
p_layout-zebra = 'X'.
p_layout-stylefname = 'STYLE'.
ENDFORM. "f_layout_build
*&---------------------------------------------------------------------*
*& Form f_user_command
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->UCOMM text
* -->RS_SELFIELD text
*----------------------------------------------------------------------*
FORM f_user_command USING ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
* rs_selfield-refresh = 'X'.
CASE ucomm.
WHEN '&DATA_SAVE'.
* do sth for save.
WHEN ''.
ENDCASE.
CLEAR ucomm.
ENDFORM. "f_user_command
*&---------------------------------------------------------------------*
*& Form F_CALLER_EXIT_AT_START
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->RS_DATA text
*----------------------------------------------------------------------*
FORM f_caller_exit_at_start USING rs_data TYPE slis_data_caller_exit.
DATA lr_grid TYPE REF TO cl_gui_alv_grid.
DATA lr_event TYPE REF TO lcl_event_receiver.
CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
IMPORTING
e_grid = gr_grid.
* CALL METHOD gr_grid->register_edit_event
* EXPORTING
* i_event_id = cl_gui_alv_grid=>MC_EVT_MODIFIED
* EXCEPTIONS
* error = 1
* OTHERS = 2.
CREATE OBJECT lr_event.
SET HANDLER lr_event->handle_modify FOR gr_grid.
ENDFORM. "F_CALLER_EXIT_AT_START
*&---------------------------------------------------------------------*
*& Form f_glay
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GLAY text
*----------------------------------------------------------------------*
FORM f_glay USING p_glay TYPE lvc_s_glay.
p_glay-edt_cll_cb = 'X'.
ENDFORM. "f_glay