ABAP Alv Varient问题:可以更改alv字段布局然后存到Varient中
如何把alv做成标准程序那样可以根据varient更改layout布局,实现增加减少字段,做成自己需要字段的布局,每个人展示不一样的内容。
1 在屏幕上定义上Varient字段和一些Varient相关的字段
PARAMETERS: alv_def LIKE disvariant-variant.
DATA: g_save TYPE c,
g_exit TYPE c,
g_variant TYPE disvariant,
gx_variant TYPE disvariant.
2 Varient初始化,得到该用户设置的默认的Varient,没有取到的话就是空。
INITIALIZATION.
g_save = 'A'.
g_variant-report = sy-cprog.
gx_variant = g_variant.
CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
EXPORTING
i_save = g_save
CHANGING
cs_variant = gx_variant
EXCEPTIONS
not_found = 2.
IF sy-subrc = 0.
alv_def = gx_variant-variant.
ENDIF.
3 Varient F4帮助
AT SELECTION-SCREEN ON VALUE-REQUEST FOR alv_def.
CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
EXPORTING
is_variant = g_variant
i_save = g_save
IMPORTING
e_exit = g_exit
es_variant = gx_variant
EXCEPTIONS
not_found = 2.
IF sy-subrc = 2.
MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
IF g_exit = space.
alv_def = gx_variant-variant.
ENDIF.
ENDIF.
4 在PAI中加上根据Varient 和程序,获取该Varient的其他信息的代码
AT SELECTION-SCREEN.
IF alv_def IS NOT INITIAL.
MOVE g_variant TO gx_variant.
MOVE alv_def TO gx_variant-variant.
CALL FUNCTION 'REUSE_ALV_VARIANT_EXISTENCE'
EXPORTING
i_save = g_save
CHANGING
cs_variant = gx_variant.
IF sy-subrc = 0.
g_variant = gx_variant.
ENDIF.
ELSE.
ENDIF.
5 在状态栏定义这两个按钮,用来保存布局。存完之后前面的取值就能取到该Varient。
6 最后输出alv,有两点需要特别注意,g_save = ‘A’ g_variant这个值要传入Function
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
EXPORTING
i_callback_program = sy-repid
* i_callback_pf_status_set = 'PF_STATUS'
* i_callback_user_command = 'FRM_USER_COMMAND'
is_variant = g_variant
i_save = g_save
is_layout_lvc = gs_layo
it_fieldcat_lvc = gt_fcat
TABLES
t_outtab = gt_out
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
ENDIF.