ABAP学习(17):OOSALV学习 Table ALV
3.1alv显示方式
OOSALV实现alv显示主要使用cl_salv_table类,调用cl_salv_table的factory工厂方法获取类的实例lo_table,调用实例display方法显示alv。
子程序display_fullscreen,直接调用display全屏显示,默认grid形式;
子程序display_grid,在指定屏幕中显示alv;
子程序display_list,显示list形式alv;
示例:
"显示内表 DATA:t_sflight TYPE TABLE OF sflight. "通过cl_salv_table类对象显示alv DATA:lo_table TYPE REF TO cl_salv_table. "容器对象 DATA:lo_container TYPE REF TO cl_gui_custom_container. "function对应类 DATA:lo_functions TYPE REF TO cl_salv_functions_list. START-OF-SELECTION. PERFORM select_data. * PERFORM display_fullscreen. * PERFORM display_grid. PERFORM display_list. END-OF-SELECTION. "查询数据 FORM select_data. SELECT * FROM sflight INTO TABLE t_sflight UP TO 20 ROWS. ENDFORM.
全屏显示grid形式alv。
示例:
"直接全屏显示,默认grid显示 FORM display_fullscreen. TRY . cl_salv_table=>factory( IMPORTING r_salv_table = lo_table CHANGING t_table = t_sflight ). CATCH cx_salv_msg. ENDTRY. "使用默认function "获取cl_alv_functions_list类对象 lo_functions = lo_table->get_functions( ). lo_functions->set_all( abap_true ). "显示alv lo_table->display( ). ENDFORM.
显示list形式alv。
示例:
"显示list形式 FORM display_list. "显示list,设置list_display:true TRY . cl_salv_table=>factory( EXPORTING list_display = abap_true IMPORTING r_salv_table = lo_table CHANGING t_table = t_sflight ). CATCH cx_salv_msg. ENDTRY. "使用默认function "获取cl_alv_functions_list类对象 lo_functions = lo_table->get_functions( ). lo_functions->set_all( abap_true ). "显示alv lo_table->display( ). ENDFORM.
指定屏幕显示alv。
1.创建子屏幕,选中program,右键选择creat->screen;
2.屏幕layout,创建一个custom control区域,name为container;
3.选择program,右键选择create->GUI STATUS,创建GUI STATUS;
4.实现屏幕input,output的module子程序;
示例:
MODULE STATUS_0100 OUTPUT. SET PF-STATUS 'STATUS100'. PERFORM creat_alv. * SET TITLEBAR 'xxx'. ENDMODULE. " STATUS_0100 OUTPUT MODULE USER_COMMAND_0100 INPUT. case okcode. when 'BACK' or 'EXIT' or 'CANC'. set screen 0. leave screen. endcase. ENDMODULE. " USER_COMMAND_0100 INPUT
5.alv在屏幕输出前绑定屏幕容器;
示例:
"创建alv FORM creat_alv. "是否绑定容器 IF lo_container IS NOT BOUND. CREATE OBJECT lo_container EXPORTING container_name = 'CONTAINER'. ENDIF. TRY . cl_salv_table=>factory( EXPORTING r_container = lo_container container_name = 'CONTAINER' IMPORTING r_salv_table = lo_table CHANGING t_table = t_sflight ). CATCH cx_salv_msg. ENDTRY. "使用默认function "获取cl_alv_functions_list类对象 lo_functions = lo_table->get_functions( ). lo_functions->set_all( abap_true ). "显示alv lo_table->display( ). ENDFORM.
6.直接调用100屏幕显示;
示例:
"在100屏显示alv FORM display_grid. CALL SCREEN 100. ENDFORM.
3.2设置alv工具栏
通过cl_salv_functions_list类对象设置alv的工具栏。
1、cl_salv_table类对象:lo_table的get_functions方法获取cl_salv_functions_list类对象:lo_functions;
2、调用lo_functions的set_all方法,设置所有alv工具栏。或者使用set_default方法,设置默认alv工具栏;
示例1:
"设置functions FORM set_functions. "function对应类 DATA:lo_functions TYPE REF TO cl_salv_functions_list. "使用默认function "获取cl_alv_functions_list类对象 lo_functions = lo_table->get_functions( ). "设置所有function lo_functions->set_all( abap_true ). "设置默认function * lo_functions->set_default( ). ENDFORM.
示例2:设置GUI_STATUS,设置function
"设置function:gui_status FORM set_function. lo_table->set_screen_status( pfstatus = 'SALV_STANDARD' report = sy-repid set_functions = lo_table->c_functions_all ). * "自定义function,这种方式只限于在自定义子屏幕container方式 * DATA:lo_functions type ref to cl_salv_functions_list. * lo_functions = lo_table->get_functions( ). * lo_functions->set_all( abap_true ). * try. * lo_functions->add_function( * name = 'NEW_DATA' * icon = '@06@' * text = 'new data' * tooltip = 'new data' * position = if_salv_c_function_position=>right_of_salv_functions ). * catch cx_salv_wrong_call cx_salv_existing. * endtry. ENDFORM.
3.3设置alv显示
通过cl_salv_display_settings类对象设置alv显示效果。
1、cl_salv_table类对象:lo_table的get_display_settings方法获取cl_salv_display_settings类对象:lo_display;
2、调用lo_display相关方法,设置alv显示。
通过set_list_header方法设置alv标题;
通过set_vertical_lines设置竖直线是否显示;
通过set_horizontal_lines方法设置横线是否显示;
通过set_striped_pattern方法设置是否显示斑马纹背景;
通过set_list_header_size设置标题大小;
示例:
"设置display FORM set_display. "alv显示设置 DATA:lo_display TYPE REF TO cl_salv_display_settings. lo_display = lo_table->get_display_settings( ). TRY . "设置alv 标题 lo_display->set_list_header( 'alv' ). "设置是否有竖直分割线 lo_display->set_vertical_lines( abap_false ). "设置是否右横向分割线 lo_display->set_horizontal_lines( abap_false ). "设置是否显示斑马条纹背景 lo_display->set_striped_pattern( abap_true ). "设置标题大小 lo_display->set_list_header_size( cl_salv_display_settings=>c_header_size_small ). "Specify Whether Empty Data Table Displayed lo_display->set_suppress_empty_data( abap_true ). "是否sort后不合并单元格 lo_display->set_no_merging( abap_true ). CATCH cx_no_check. ENDTRY. ENDFORM.
3.4设置sort排序
通过cl_salv_sorts类对象设置sort排序。
1、cl_salv_table类对象:lo_table的get_sorts方法获取cl_salv_sorts类对象:lo_sorts;
2、调用lo_sorts的clear方法,清空sort规则;
3、调用lo_sorts的add_sort方法,添加sort规则;
示例:
"设置sort FORM set_sort. "sort information DATA:lo_sorts TYPE REF TO cl_salv_sorts. lo_sorts = lo_table->get_sorts( ). "remove sort information lo_sorts->clear( ). TRY . lo_sorts->add_sort( columnname = 'CARRID' position = 1 sequence = if_salv_c_sort=>sort_up ). lo_sorts->add_sort( columnname = 'CONNID' position = 1 subtotal = abap_true group = if_salv_c_sort=>group_with_underline sequence = if_salv_c_sort=>sort_up ). CATCH cx_salv_not_found cx_salv_existing cx_salv_data_error. ENDTRY. ENDFORM.
3.5设置aggregation聚合
通过cl_salv_aggregations类对象设置聚合。
1、cl_salv_table类对象:lo_table的get_aggregations方法获取cl_salv_aggregations类对象:lo_aggregations;
2、通过lo_aggregations对象调用clear方法清空聚合规则;
3、通过lo_aggregations对象调用add_aggregation方法添加聚合规则;
其中add_aggregation方法有aggregation参数指定聚合使用方法,默认是加总方式:
加总IF_SALV_C_AGGREGATION~TOTAL;
最小值IF_SALV_C_AGGREGATION~MINIMUM;
最大值IF_SALV_C_AGGREGATION~MAXIMUM;
均值IF_SALV_C_AGGREGATION~AVERAGE;
无IF_SALV_C_AGGREGATION~NONE;
示例:
"设置aggregation FORM set_aggregation. "aggregations对象 DATA:lo_aggregations TYPE REF TO cl_salv_aggregations. lo_aggregations = lo_table->get_aggregations( ). "清空aggregations lo_aggregations->clear( ). try. lo_aggregations->add_aggregation( columnname = 'PAYMENTSUM' ). catch cx_salv_not_found cx_salv_data_error cx_salv_existing. endtry. ENDFORM.
3.6设置filter过滤器
通过cl_salv_filters类对象设置聚合。
1、cl_salv_table类对象:lo_table的get_filters方法获取cl_salv_filters类对象:lo_filters;
2、通过lo_filters对象的clear方法清空过滤规则;
3、通过lo_filters对象的add_filter方法添加过滤规则;
方法add_filter参数sign,option,low,high这几个参数和Range Table类似。
示例:
"设置filter FORM set_filter. "设置filter对象 DATA:lo_filters TYPE REF TO cl_salv_filters. lo_filters = lo_table->get_filters( ). lo_filters->clear( ). try. lo_filters->add_filter( columnname = 'CURRENCY' low = 'USD' ). catch cx_salv_not_found cx_salv_data_error cx_salv_existing. endtry. ENDFORM.
3.7设置layout
通过cl_salv_layout设置alv的layout。
1、cl_salv_table类对象:lo_table的get_layout方法获取cl_salv_layout类对象:lo_layout;
2、通过lo_layout对象调用对应方法设置layout;
示例:
"设置layout FORM set_layout. "设置头 DATA:lo_header type ref to cl_salv_form_header_info. create object lo_header exporting text = 'This is my Header'. lo_table->set_top_of_list( lo_header ). "设置尾 create object lo_header exporting text = 'This is my Footer'. lo_table->set_end_of_list( lo_header ). "设置layout DATA:lo_layout type ref to cl_salv_layout. DATA:ls_key type salv_s_layout_key. lo_layout = lo_table->get_layout( ). ls_key-report = sy-repid. lo_layout->set_key( ls_key ). "设置使用默认layout lo_layout->set_default( abap_true ). "设置保存 "if_salv_c_layout=>restrict_none. "if_salv_c_layout=>restrict_user_dependant. "if_salv_c_layout=>restrict_user_independant. lo_layout->set_save_restriction( if_salv_c_layout=>restrict_user_independant ). "设置初始化layout "lo_layout->set_initial_layout( ). ENDFORM.
3.8设置弹窗显示
1、通过cl_salv_table对象调用set_screen_popup方法,将内表显示成弹窗。
示例:
"设置弹窗显示, "也可通过function点击调用,1.生成cl_salv_table对象, "2.调用set_screen_popup方法, "3.调用cl_salv_table的display方法, FORM set_popup. lo_table->set_screen_popup( start_column = 1 end_column = 100 start_line = 1 end_line = 20 ). ENDFORM.
3.9设置关闭窗口
1、通过cl_salv_table对象调用close_screen方法,关闭窗口。
示例:
"关闭窗口 FORM close_screen. "关闭窗口 lo_table->close_screen( ). ENDFORM.
3.10设置列控制
通过cl_salv_columns_table类对象和cl_salv_column_table类对象控制列。
示例:
TYPES:BEGIN OF s_out. INCLUDE TYPE sflight. TYPES: exception type char1, "交通灯 checkbox type sap_bool, "选择框 icon type icon_d, "icon symbol type icon_d, "icon button type icon_d, hyperlink type s_url, t_color type lvc_t_scol, t_celltype type salv_t_int4_column, t_hyperlink type salv_t_int4_column, t_dropdown type salv_t_int4_column, END OF s_out. TYPES: begin of s_hyperlink, handle type salv_de_hyperlink_handle, hyperlink type service_rl, end of s_hyperlink. DATA:gt_hyperlink TYPE TABLE OF s_hyperlink. DATA:wa_hyperlink LIKE LINE OF gt_hyperlink. "显示内表 DATA:t_sflight TYPE STANDARD TABLE OF s_out. "通过cl_salv_table类对象显示alv DATA:lo_table TYPE REF TO cl_salv_table. "容器对象 DATA:lo_container TYPE REF TO cl_gui_custom_container. START-OF-SELECTION. PERFORM select_data. PERFORM display_fullscreen. END-OF-SELECTION. "查询数据 FORM select_data. field-symbols: <ls_out> type s_out. DATA:lt_color type lvc_t_scol. DATA:ls_color type lvc_s_scol. DATA:lt_celltype type salv_t_int4_column. DATA:ls_celltype type salv_s_int4_column. "超链接 DATA:lt_hyperlink type salv_t_int4_column. DATA:ls_hyperlink type salv_s_int4_column. SELECT * FROM sflight INTO CORRESPONDING FIELDS OF TABLE t_sflight UP TO 20 ROWS. LOOP AT t_sflight ASSIGNING <ls_out>. "设置exception值 <ls_out>-exception = sy-tabix mod 3 + 1. "设置icon值,include <icon>,TYPE-POOLS: icon. "其中有icon值 <ls_out>-icon = icon_overview. "设置symb,include <symbol>,TYPE-POOLS: sym. "symbol值 <ls_out>-symbol = sym_document. "设置button <ls_out>-button = icon_refresh. "设置hyperlink <ls_out>-hyperlink = 'https://www.baidu.com/'. "设置checkbox <ls_out>-checkbox = abap_false. "使用一列,作为颜色控制列, "整体控制设置单元格颜色 clear lt_color. clear ls_color. ls_color-fname = 'PLANETYPE'. ls_color-color-col = col_positive. ls_color-color-int = 0. ls_color-color-inv = 0. append ls_color to lt_color. <ls_out>-t_color = lt_color. "控制列类型:symbol,icon,text,button,checkbox等 clear lt_celltype. clear ls_celltype. ls_celltype-columnname = space. ls_celltype-value = if_salv_c_cell_type=>text. append ls_celltype to lt_celltype. <ls_out>-t_celltype = lt_celltype. "设置hyperlink列 clear lt_hyperlink. clear ls_hyperlink. ls_hyperlink-columnname = 'HYPERLINK'. ls_hyperlink-value = sy-tabix. append ls_hyperlink to lt_hyperlink. <ls_out>-t_hyperlink = lt_hyperlink. "设置链接到外部网页 wa_hyperlink-hyperlink = 'https://www.baidu.com/'. wa_hyperlink-handle = sy-tabix. APPEND wa_hyperlink TO gt_hyperlink. ENDLOOP. ENDFORM. "默认grid显示 FORM display_fullscreen. TRY . cl_salv_table=>factory( IMPORTING r_salv_table = lo_table CHANGING t_table = t_sflight ). CATCH cx_salv_msg. ENDTRY. "设置alv PERFORM set_column. "显示alv lo_table->display( ). ENDFORM. "设置column FORM set_column. "设置column * DATA:lo_columns TYPE REF TO cl_salv_columns. DATA:lo_columns_tab TYPE REF TO cl_salv_columns_table. DATA:lo_column TYPE REF TO cl_salv_column_table. TRY . "整体columns控制,cl_salv_columns_table,cl_salv_columns "cl_salv_columns_table可以设置方法更多,cl_salv_columns方法很少, * lo_columns = lo_table->get_columns( ). lo_columns_tab = lo_table->get_columns( ). "设置自动优化列 lo_columns_tab->set_optimize( abap_true ). "整体颜色控制列生效,可以实现单独单元格颜色 lo_columns_tab->set_color_column( 'T_COLOR' ). "设置整体列类型控制列生效,设置对应列类型,这个会覆盖单独设置的列类型 * lo_columns_tab->set_cell_type_column( 'T_CELLTYPE' ). "设置exception列,显示交通灯 "默认显示在第一列,值为1,2,3,对应红,黄,绿 lo_columns_tab->set_exception_column( 'EXCEPTION' ). "通过functional setting设置整体值的tooltip DATA:lo_functional_settings type ref to cl_salv_functional_settings. DATA:lo_tooltips type ref to cl_salv_tooltips. DATA:l_value type lvc_value. lo_functional_settings = lo_table->get_functional_settings( ). "添加tooltip规则,type:列类型,value:对应值显示tooltip,tooltip:提示内容 lo_tooltips = lo_functional_settings->get_tooltips( ). lo_tooltips->add_tooltip( type = cl_salv_tooltip=>c_type_exception value = '1' tooltip = 'My Red Light Tooltip' ). "设置链接到外部网页 DATA:lo_hyperlinks type ref to cl_salv_hyperlinks. lo_columns_tab->set_hyperlink_entry_column( 'T_HYPERLINK' ). lo_hyperlinks = lo_functional_settings->get_hyperlinks( ). loop at gt_hyperlink into wa_hyperlink. lo_hyperlinks->add_hyperlink( handle = wa_hyperlink-handle hyperlink = wa_hyperlink-hyperlink ). endloop. "设置单独列 lo_column ?= lo_columns_tab->get_column( 'MANDT' ). "设置成technical column,彻底不显示,作为功能列 lo_column->set_technical( abap_true ). "设置是否显示,设置不显示,但是设置里面可以看到有该列 lo_column ?= lo_columns_tab->get_column( 'FLDATE' ). lo_column->set_visible( abap_true ). "设置对齐方式 "left:IF_SALV_C_ALIGNMENT~LEFT; "right:IF_SALV_C_ALIGNMENT~RIGHT; "center:IF_SALV_C_ALIGNMENT~CENTERED; lo_column->set_alignment( cl_salv_column_table=>IF_SALV_C_ALIGNMENT~LEFT ). "设置单列颜色 lo_column ?= lo_columns_tab->get_column( 'PRICE' ). DATA:lv_color TYPE lvc_s_colo. "使用include <color>,颜色值,TYPE-POOLS: col. "col_1 col_background '0'. * col_1 col_heading '1'. * col_1 col_normal '2'. * col_1 col_total '3'. * col_1 col_key '4'. * col_1 col_positive '5'. * col_1 col_negative '6'. * col_1 col_group '7'. " lv_color-col = col_group. lo_column->set_color( lv_color ). "设置单列tooltip lo_column->set_tooltip( 'price列提示' ). lo_column->set_tooltip( 'price列提示' ). "设置单列标题文本 lo_column->set_long_text( 'long price' ). lo_column->set_medium_text( 'medium price' ). lo_column->set_short_text( 'shortpri' ). "设置单列f4帮助 lo_column->set_f4( abap_true ). "获取单列,设置特殊类型 lo_column ?= lo_columns_tab->get_column( 'EXCEPTION' ). "设置列标题 lo_column->set_short_text( 'exception' ). lo_column ?= lo_columns_tab->get_column( 'CHECKBOX' ). lo_column->set_short_text( 'check' ). "设置column,checkbox类型 lo_column->set_cell_type( if_salv_c_cell_type=>checkbox ). lo_column ?= lo_columns_tab->get_column( 'ICON' ). lo_column->set_short_text( 'icon' ). "设置column,icon类型 lo_column->set_icon( abap_true ). lo_column ?= lo_columns_tab->get_column( 'SYMBOL' ). lo_column->set_short_text( 'symbol' ). "设置column,symbol类型 lo_column->set_symbol( abap_true ). lo_column ?= lo_columns_tab->get_column( 'BUTTON' ). lo_column->set_short_text( 'button' ). "设置column,button类型,这个会出错?? * lo_column->set_cell_type( if_salv_c_cell_type=>button ). lo_column ?= lo_columns_tab->get_column( 'HYPERLINK' ). "设置column,link类型 lo_column->set_cell_type( if_salv_c_cell_type=>link ). lo_column->set_short_text( 'link' ). CATCH cx_salv_not_found cx_salv_data_error cx_salv_existing. ENDTRY. ENDFORM.
3.11设置行列选择
通过cl_salv_selections设置行列选择或者选择单元格。
1、通过cl_salv_table类对象lo_table的get_selections方法获取cl_salv_selections类对象:lo_selections;
2、通过lo_selections对象的set_selection_mode方法设置选择模式;
3、通过lo_selections对象的set_selected_columns方法设置选择列;
4、通过lo_selections对象的set_selected_rows方法设置选择行;
5、通过lo_selections对象的set_current_cell方法设置选择单元格;
示例:
"设置选中列 FORM set_selection. DATA:lo_selections type ref to cl_salv_selections. DATA:lt_column type salv_t_column. DATA:lt_rows TYPE salv_t_row. DATA:ls_cell TYPE salv_s_cell. DATA:mode TYPE I. append 'PRICE' to lt_column. APPEND 1 to lt_rows. ls_cell-row = 5. ls_cell-columnname = 'FLDATE'. lo_selections = lo_table->get_selections( ). "设置选择模式 "single=1,multiple=2,row_column=4,cell=3,none=0 lo_selections->set_selection_mode( if_salv_c_selection_mode=>multiple ). "获取选择模式 mode = lo_selections->get_selection_mode( ). "设置选中列 lo_selections->set_selected_columns( lt_column ). "获取选择列 "lo_selections->get_selected_columns( ). "设置选中行 lo_selections->set_selected_rows( lt_rows ). "获取选中行 "lo_selections->get_selected_rows( ). "设置选择单元格 lo_selections->set_current_cell( ls_cell ). "获取选择单元格 " lo_selections->get_current_cell( ). ENDFORM.
3.12设置刷新显示
通过cl_salv_table类对象lo_table调用refresh方法刷新table显示。
示例:
"设置刷新 FORM set_refresh. * "设置新的数据,设置不了,需要判断r_controller->is_new_data_allowed( ) = true * SELECT * INTO CORRESPONDING FIELDS OF TABLE new_tab FROM sflight. * try. * lo_table->set_data( * CHANGING * t_table = new_tab ). * catch CX_SALV_NO_NEW_DATA_ALLOWED. * endtry. "设置屏幕刷新,显示新增数据 lo_table->refresh( refresh_mode = if_salv_c_refresh=>full ). ENDFORM.
3.13设置事件响应
1、定义事件类cl_handle_events,并且实现;
2、通过cl_salv_table类对象lo_table的方法get_event方法获取cl_salv_events_table类对象lo_events_tab;
3、创建cl_handle_events类对象,lo_events_handle;
4、将lo_events_handle事件注册给lo_events_tab;
示例:
"事件类定义 class cl_handle_events definition. public section. methods: on_user_command for event added_function of cl_salv_events importing e_salv_function, on_before_salv_function for event before_salv_function of cl_salv_events importing e_salv_function, on_after_salv_function for event after_salv_function of cl_salv_events importing e_salv_function, on_double_click for event double_click of cl_salv_events_table importing row column, on_link_click for event link_click of cl_salv_events_table importing row column. endclass. "事件类实现 class cl_handle_events implementation. method on_user_command. "可以获取用户点击function,实现自定义function message i000(0k) with 'user command event' e_salv_function. PERFORM set_selection. PERFORM set_refresh. endmethod. "on_user_command method on_before_salv_function. message i000(0k) with 'before alv function event' e_salv_function. endmethod. "on_before_salv_function method on_after_salv_function. message i000(0k) with 'after alv function event' e_salv_function. endmethod. "on_after_salv_function method on_double_click. message i000(0k) with row column 'double click event'. endmethod. "on_double_click method on_link_click. message i000(0k) with row column 'sigle click event'. endmethod. "on_single_click endclass. "设置events FORM set_events. DATA:lo_events_tab type ref to cl_salv_events_table. DATA:lo_events_handle TYPE REF TO cl_handle_events. lo_events_tab = lo_table->get_event( ). CREATE OBJECT lo_events_handle. "注册事件 set handler lo_events_handle->on_user_command for lo_events_tab. set handler lo_events_handle->on_before_salv_function for lo_events_tab. set handler lo_events_handle->on_after_salv_function for lo_events_tab. set handler lo_events_handle->on_double_click for lo_events_tab. set handler lo_events_handle->on_link_click for lo_events_tab. "设置列为hotspot类型,单击响应 try. DATA:lo_columns type ref to cl_salv_columns. DATA:lo_column type ref to cl_salv_column_table. lo_columns = lo_table->get_columns( ). lo_column ?= lo_columns->get_column( 'CARRID' ). lo_column->set_cell_type( if_salv_c_cell_type=>hotspot ). catch cx_salv_not_found. endtry. ENDFORM.
本文来自博客园,作者:渔歌晚唱,转载请注明原文链接:https://www.cnblogs.com/tangToms/p/12103996.html