OO方式实现ALV: cl_salv_table
这里总结最近用cl_salv_table实现ALV遇到问题和解决办法
FORM set_alv2 . DATA: lv_syrepid TYPE syrepid. lv_syrepid = sy-cprog. cl_salv_table=>factory( EXPORTING list_display = abap_false r_container = cl_gui_container=>screen0 IMPORTING r_salv_table = gr_alv CHANGING t_table = gt_doc ). “传输需要显示的内表 gr_functions = gr_alv->get_functions( ). "获取常用的ALV功能按钮 DATA lv_icon TYPE string. lv_icon = icon_refresh. gr_functions->add_function( "添加自定义按钮 EXPORTING name = 'DOWNLOAD' icon = '@M9@' text = '下载文档' tooltip = '下载文档' position = if_salv_c_function_position=>right_of_salv_functions ). "往右边排 gr_functions->add_function( EXPORTING name = 'SELECT_ALL' icon = '@4B@' text = '全选' tooltip = '全选' position = if_salv_c_function_position=>left_of_salv_functions ). "往右边排 gr_functions->add_function( EXPORTING name = 'DESELECT_ALL' icon = '@4D@' text = '取消全选' tooltip = '取消全选' position = if_salv_c_function_position=>left_of_salv_functions ). "往右边排 gr_functions->set_all( abap_true ). ”设置所有alv按钮(自带的+自定义的) "=====获取事件对象 DATA: gr_event TYPE REF TO cl_salv_events_table. gr_event = gr_alv->get_event( ). "=====事件注册 DATA: gr_handle_event TYPE REF TO cl_handle_events. CREATE OBJECT gr_handle_event. SET HANDLER gr_handle_event->on_user_command FOR gr_event. SET HANDLER gr_handle_event->on_checkbox FOR gr_event. DATA: gr_column_list TYPE REF TO cl_salv_column_list. gr_columns = gr_alv->get_columns( ). "获取列的class gr_column = gr_columns->get_column('ACTION_NO'). "获取该列 gr_column->set_short_text('需求号'). "调用修改列名称的方法 gr_columns->set_optimize( abap_true ). "调用设置列最优化设置方法这边用到列实例是gr_colums不是gr_colum gr_column = gr_columns->get_column('FLAG'). gr_column->set_short_text('选择'). gr_columnt ?= gr_column. "这边很重要 gr_columnt->set_cell_type( 6 ). "设置选择列为checkbox,6代表checkbox gr_display = gr_alv->get_display_settings( ). "alv设置 gr_display->set_list_header('需求号&文档编号报表'). "alv抬头设置 gr_alv->display( )."调用实例方法显示表报 "调用显示alv方法展示ALV报表 ENDFORM. " SET_ALV2
下面是是事件的class的定义
CLASS cl_handle_events DEFINITION. PUBLIC SECTION. METHODS: on_user_command FOR EVENT added_function OF cl_salv_events_table IMPORTING e_salv_function, on_checkbox FOR EVENT link_click OF cl_salv_events_table IMPORTING row column. ENDCLASS. CLASS cl_handle_events IMPLEMENTATION. METHOD on_user_command. PERFORM handle_user_command USING e_salv_function. ENDMETHOD. METHOD on_checkbox. PERFORM handle_clickbox USING row column. ENDMETHOD. ENDCLASS.
事件的方法定义
FORM handle_user_command USING p_function TYPE salv_de_function. CASE p_function. WHEN 'DOWNLOAD'. PERFORM down_doc. WHEN 'DESELECT_ALL'. gs_doc-flag = ' '. MODIFY gt_doc FROM gs_doc TRANSPORTING flag WHERE flag = 'X'. WHEN 'SELECT_ALL'. gs_doc-flag = 'X'. MODIFY gt_doc FROM gs_doc TRANSPORTING flag WHERE flag = ' '. ENDCASE. gr_alv->refresh( ). ENDFORM.