01 功能说明
本系列将通过模拟用户与开发者之间的对话场景,来逐步演示SALV的使用。
在本篇中,我们将延续前文内容,解决用户最新提出的需求。今天我们的主题是SALV模型中的另外两个功能:如何 刷新ALV以及如何添加按钮并 弹框展示数据。相信通过亲自操作,你能够快速的理解和掌握这些知识点。
让我们一起动手尝试一下,看看如何实现这些功能吧!
02 功能效果
第20天 用户说:刷新ALV
上次添加的复选框点击不起作用,需要修复一下。
CASE column.
WHEN 'CBOX'.
CHECK p_day20 = 'X'.
FIELD-SYMBOLS <spfli> LIKE gs_spfli.
READ TABLE gt_spfli ASSIGNING <spfli> INDEX row.
IF <spfli>-cbox = 'X' .
<spfli>-cbox = ''.
ELSE.
<spfli>-cbox = 'X'.
ENDIF.
"刷新ALV
DATA ls_stable TYPE lvc_s_stbl.
ls_stable-row = 'X'.
ls_stable-col = 'X'.
go_salv->refresh(
EXPORTING
s_stable = ls_stable
refresh_mode = if_salv_c_refresh=>soft
).
WHEN OTHERS.
ENDCASE.
第21天 用户说:添加按钮弹出ALV
工具栏新增航班信息按钮,点击展示所选航线对应的航班信息。
IF p_day21 = 'X'.
lo_functions->add_function(
EXPORTING
name = 'DISP'
icon = '@7T@'
text = '航班信息'
tooltip = '所选航线的航班信息'
position = if_salv_c_function_position=>right_of_salv_functions
).
ENDIF.
CASE e_salv_function.
WHEN 'DISP'.
CHECK p_day21 = 'X'.
DATA lt_sflight TYPE TABLE OF sflight.
LOOP AT gt_spfli INTO gs_spfli WHERE cbox = 'X'.
SELECT * FROM sflight
APPENDING TABLE lt_sflight
WHERE carrid = gs_spfli-carrid
AND connid = gs_spfli-connid.
ENDLOOP.
DATA lo_salv TYPE REF TO cl_salv_table.
DATA lo_functions_list TYPE REF TO cl_salv_functions_list.
DATA lo_columns_table TYPE REF TO cl_salv_columns_table.
TRY .
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_salv
CHANGING
t_table = lt_sflight
).
CATCH cx_salv_msg.
ENDTRY.
lo_salv->set_screen_popup(
EXPORTING
start_column = 5
end_column = 100
start_line = 5
end_line = 25
).
lo_functions_list = lo_salv->get_functions( ).
lo_columns_table = lo_salv->get_columns( ).
lo_functions_list->set_default( 'X' ).
lo_columns_table->set_optimize( 'X' ).
lo_salv->display( ).
WHEN OTHERS.
ENDCASE.
03 源码分享
REPORT zsalv_demo NO STANDARD PAGE HEADING.
TYPES:
BEGIN OF gty_spfli_incl,
cbox TYPE c,
END OF gty_spfli_incl.
DATA:
BEGIN OF gs_spfli.
INCLUDE TYPE spfli.
INCLUDE TYPE gty_spfli_incl.
DATA:
END OF gs_spfli,
gt_spfli LIKE TABLE OF gs_spfli,
go_salv TYPE REF TO cl_salv_table.
*----------------------------------------------------------------------*
* CLASS lcl_evt_handler DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_evt_handler DEFINITION.
PUBLIC SECTION.
METHODS on_link_click "单击(热点)
FOR EVENT link_click OF cl_salv_events_table
IMPORTING row column.
METHODS on_added_function "新增按钮点击
FOR EVENT added_function OF cl_salv_events_table
IMPORTING e_salv_function.
ENDCLASS. "lcl_evt_handler DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_evt_handler IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_evt_handler IMPLEMENTATION.
METHOD on_link_click.
MESSAGE i001(00) WITH 'on_link_click:' row ' - ' column.
CASE column.
WHEN 'CBOX'.
FIELD-SYMBOLS <spfli> LIKE gs_spfli.
READ TABLE gt_spfli ASSIGNING <spfli> INDEX row.
IF <spfli>-cbox = 'X' .
<spfli>-cbox = ''.
ELSE.
<spfli>-cbox = 'X'.
ENDIF.
"刷新ALV
DATA ls_stable TYPE lvc_s_stbl.
ls_stable-row = 'X'.
ls_stable-col = 'X'.
go_salv->refresh(
EXPORTING
s_stable = ls_stable
refresh_mode = if_salv_c_refresh=>soft
).
WHEN OTHERS.
ENDCASE.
ENDMETHOD. "on_link_click
METHOD on_added_function.
MESSAGE i001(00) WITH 'on_added_function:' e_salv_function.
CASE e_salv_function.
WHEN 'DISP'.
DATA lt_sflight TYPE TABLE OF sflight.
LOOP AT gt_spfli INTO gs_spfli WHERE cbox = 'X'.
SELECT * FROM sflight
APPENDING TABLE lt_sflight
WHERE carrid = gs_spfli-carrid
AND connid = gs_spfli-connid.
ENDLOOP.
DATA lo_salv TYPE REF TO cl_salv_table.
DATA lo_functions_list TYPE REF TO cl_salv_functions_list.
DATA lo_columns_table TYPE REF TO cl_salv_columns_table.
TRY .
cl_salv_table=>factory(
IMPORTING
r_salv_table = lo_salv
CHANGING
t_table = lt_sflight
).
CATCH cx_salv_msg.
ENDTRY.
lo_salv->set_screen_popup(
EXPORTING
start_column = 5
end_column = 100
start_line = 5
end_line = 25
).
lo_functions_list = lo_salv->get_functions( ).
lo_columns_table = lo_salv->get_columns( ).
lo_functions_list->set_default( 'X' ).
lo_columns_table->set_optimize( 'X' ).
lo_salv->display( ).
WHEN OTHERS.
ENDCASE.
ENDMETHOD. "on_added_function
ENDCLASS. "lcl_evt_handler IMPLEMENTATION
INITIALIZATION.
sy-title = 'ABAP SALV演示程序-公众号(ABAP猿)'.
START-OF-SELECTION.
SELECT * FROM spfli INTO CORRESPONDING FIELDS OF TABLE gt_spfli UP TO 30 ROWS.
cl_salv_table=>factory(
EXPORTING
r_container = cl_gui_container=>default_screen
IMPORTING
r_salv_table = go_salv
CHANGING
t_table = gt_spfli
).
*------------------------------------*
DATA lo_display TYPE REF TO cl_salv_display_settings.
lo_display = go_salv->get_display_settings( ).
lo_display->set_striped_pattern( 'X' ).
*------------------------------------*
DATA lo_functions TYPE REF TO cl_salv_functions.
lo_functions = go_salv->get_functions( ).
lo_functions->set_all( 'X' ).
lo_functions->add_function(
EXPORTING
name = 'DISP'
icon = '@7T@'
text = '航班信息'
tooltip = '所选航线的航班信息'
position = if_salv_c_function_position=>right_of_salv_functions
).
*------------------------------------*
DATA lo_events_table TYPE REF TO cl_salv_events_table.
lo_events_table = go_salv->get_event( ).
DATA lo_evt_handler TYPE REF TO lcl_evt_handler.
CREATE OBJECT lo_evt_handler.
SET HANDLER lo_evt_handler->on_link_click FOR lo_events_table.
SET HANDLER lo_evt_handler->on_added_function FOR lo_events_table.
*------------------------------------*
DATA lo_columns TYPE REF TO cl_salv_columns_table .
DATA lo_column TYPE REF TO cl_salv_column_table .
lo_columns = go_salv->get_columns( ).
lo_columns->set_optimize( 'X' ).
lo_columns->set_column_position( columnname = 'CBOX' position = 1 ).
lo_column ?= lo_columns->get_column( columnname = 'CBOX' ).
lo_column->set_short_text( '选择' ).
lo_column->set_medium_text( '选择' ).
lo_column->set_long_text( '选择' ).
lo_column->set_alignment( if_salv_c_alignment=>centered ).
lo_column->set_cell_type( if_salv_c_cell_type=>checkbox_hotspot ).
*------------------------------------*
go_salv->display( ).
cl_abap_list_layout=>suppress_toolbar( ).
WRITE: space.
小贴士:
- 可使用BC_DATA_GEN生成数据,以便查看演示效果;
- 以上纯属个人总结,如有纰漏请联系我;
以上是今日分享,希望对你有所帮助~~
04 关注我
更多干货请扫码关注微信公众号:ABAP猿