面向对象 OO ALV 的创建与使用
创建 ALV 可能是 ABAP 世界中最常见的用途,目前已经很少使用 REUSE_ALV_GRID_DISPLAY
,推荐使用 CL_SALV_TABLE
或 CL_GUI_ALV_GRID
。
类定义部分
不使用面相对象 ALV 时,我们会在全局的头文件中定义我们需要的数据和变量,在类定义中,我们把数据定义写在类中,同时定义类包含的方法。
如下:
CLASS lcl_report DEFINITION.
PUBLIC SECTION.
TYPES: BEGIN OF lty_sflight,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
fldate TYPE s_date,
price TYPE s_price,
currency TYPE s_currcode,
planetype TYPE s_planetye,
seatsmax TYPE s_seatsmax,
seatsocc TYPE s_seatsocc,
END OF lty_sflight.
METHODS: get_data,
build_fieldcatlog,
display_alv.
DATA: t_report TYPE STANDARD TABLE OF lty_sflight,
t_fcat TYPE lvc_t_fcat.
ENDCLASS. "lcl_report DEFINITION
类实现部分
在这一部分,主要实现我们在声明中定义的方法,本例子中包含三个类方法:get_data
、 build_fieldcatlog
和 display_alv
,如下:
*----------------------------------------------------------------------*
* CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
* Get REPORT data
METHOD get_data.
SELECT carrid connid fldate price currency planetype seatsmax
seatsocc INTO TABLE me->t_report
FROM sflight
WHERE carrid = 'AA'.
ENDMETHOD. "get_data
* Build FieldCatlog
METHOD build_fieldcatlog.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
ct_fieldcat = t_fcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDMETHOD. "build_fieldcatlog
* Display ALV
METHOD display_alv.
CALL SCREEN 100.
ENDMETHOD. "display
ENDCLASS. "lcl_report IMPLEMENTATION
最后是我们的选择屏幕部分和类使用部分
START-OF-SELECTION.
DATA: lo_report TYPE REF TO lcl_report,
lo_container_100 TYPE REF TO cl_gui_custom_container,
lo_grid TYPE REF TO cl_gui_alv_grid.
* Create instance for the local class
CREATE OBJECT lo_report.
* Get the REPORT table data
lo_report->get_data( ).
* 1. Create container instance
CREATE OBJECT lo_container_100
EXPORTING
container_name = 'ALV'.
* 2. Create ALV grid instance by using the container instance
CREATE OBJECT lo_grid
EXPORTING
i_parent = lo_container_100.
* 3. Build Field Catlog
lo_report->build_fieldcatlog( ).
* 4. Call the ALV
CALL METHOD lo_grid->set_table_for_first_display
CHANGING
it_outtab = lo_report->t_report
it_fieldcatalog = lo_report->t_fcat
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3
OTHERS = 4.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
* Display the ALV
lo_report->display_alv( ).
新建 100 屏幕
让我们继续查看带有屏幕 100 dynpro 对象的示例 ABAP 程序代码。
双击 ABAP 语句 call screen 100
中的 100 以创建 dynpro 屏幕。
在下面的屏幕截图中,您可以看到 SAP Screen Painter 编辑器上的屏幕布局视图。
如果双击自定义控件显示元素属性,可以将控件命名为“CC_CONTAINER_GRID”
此 Screen Painter 示例显示了自定义控件 cl_gui_custom_container 对象和放置在该容器中的 ALV 网格对象 cl_gui_alv_grid。
请注意 Button 对象 BTNLIST 的 FctCode(功能代码)。 FctCode 设置为“BTNLIST”,并将在 PAI(输入后处理)模块中通过 ABAP CASE 语句控制 OK_CODE 的值进行控制。
屏幕 100 配置的最后一步是通过将 OK_CODE 变量添加到屏幕的元素列表中的一般属性来分配 OK_CODE 变量。
在所有上述屏幕元素属性设置后,添加下面的输出前处理(PBO)模块代码或屏幕100 的 ABAP 代码块。
MODULE STATUS_0100 OUTPUT.
SET PF-STATUS 'MAINSTATUS'.
SET TITLEBAR 'TITLE'.
IF g_Custom_Container IS INITIAL.
" Create CONTAINER object with reference to container name in the screen
CREATE OBJECT g_Custom_Container EXPORTING CONTAINER_NAME = g_Container.
" Create GRID object with reference to parent name
CREATE OBJECT g_Grid EXPORTING I_PARENT = g_Custom_Container.
PERFORM u_prepare_fieldcatalog.
gs_layout-ZEBRA = 'X'.
"gs_layout-edit = 'X'. " Makes all Grid editable
" SET_TABLE_FOR_FIRST_DISPLAY
CALL METHOD g_Grid->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
is_layout = gs_layout
CHANGING
it_fieldcatalog = gs_fieldcatalog
IT_OUTTAB = gt_item. " Data
ELSE.
CALL METHOD g_Grid->REFRESH_TABLE_DISPLAY.
ENDIF.
ENDMODULE. " STATUS_0100 OUTPUT
双击 TITLE 创建一个新的标题栏。
另外,双击 MAINSTATUS 以创建 PF-STATUS 对象。
如果您定义以下标准工具栏命令,则本 ABAP 教程足以在 ABAP 程序上导航。
您可以使用以下输入后处理 (PAI) 模块代码或 Screen 100 的 ABAP 代码块。
MODULE USER_COMMAND_0100 INPUT.
SAVE_OK = OK_CODE.
CLEAR OK_CODE.
CASE SAVE_OK.
WHEN 'EXIT' OR 'BACK' OR 'CNCL'.
LEAVE PROGRAM.
WHEN 'BTNLIST'.
PERFORM u_flight.
WHEN OTHERS.
ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
获取 SALV 的 cl_gui_alv_grid
实例
CLASS lcl_access_salv DEFINITION INHERITING FROM cl_salv_model_list FINAL.
PUBLIC SECTION.
CLASS-METHODS:
get_cl_gui_alv_grid IMPORTING io_salv TYPE REF TO cl_salv_model_list
RETURNING VALUE(ro_grid) TYPE REF TO cl_gui_alv_grid.
ENDCLASS.
CLASS lcl_access_salv IMPLEMENTATION.
METHOD get_cl_gui_alv_grid.
DATA:lo_salv TYPE REF TO cl_salv_table.
*--------------------------------------------------------------------*
* Must be bound
*--------------------------------------------------------------------*
IF io_salv IS NOT BOUND.
RETURN.
ENDIF.
*--------------------------------------------------------------------*
* Adapter must be bound
*--------------------------------------------------------------------*
IF io_salv->r_controller IS NOT BOUND
OR io_salv->r_controller->r_adapter IS NOT BOUND.
MESSAGE 'Minor programming fault: Call GET_GRID_FROM_SALV after SALV->DISPLAY( )!'(001)
TYPE 'S' DISPLAY LIKE 'W'.
lo_salv ?= io_salv. " Calling method has cl_salv_table typed in interface
lo_salv->display( ).
ENDIF.
IF io_salv->r_controller IS NOT BOUND
OR io_salv->r_controller->r_adapter IS NOT BOUND.
RETURN. " Still not bound --> can't do anything
ENDIF.
*--------------------------------------------------------------------*
* If method not present or wrong return type exception will handle this and we return unbound grid
*--------------------------------------------------------------------*
TRY.
" Works for CL_SALV_FULLSCREEN_ADAPTER as well as for CL_SALV_GRID_ADAPTER
CALL METHOD io_salv->r_controller->r_adapter->('GET_GRID')
RECEIVING
value = ro_grid.
CATCH cx_root ##CATCH_ALL. " don't dump
RETURN.
ENDTRY.
ENDMETHOD.
ENDCLASS.
- 点赞
- 收藏
- 关注作者
评论(0)