ABAP项目砖家之旅-alv项目实战

ABAP项目砖家之旅-alv项目实战

  • 一、ALV相关介绍
  • 二、ALV使用步骤
    • (一)普通alv和扩展alv
      • 1.普通alv函数
        • 1.1函数参数解释
        • 1.2ALV状态栏STATUS
        • 1.3定义函数需要的输入参数
        • 1.4 select数据
        • 1.5 调用ALV函数
    • (二)树状和层次alv
      • 1. 层次结构 REUSE_ALV_HIERSEQ_LIST_DISPLAY
      • 2. 树状 RS_TREE_LIST_DISPLAY
    • (三)ooalv
      • 1. SALV:cl_gui_alv_grid 、 CL_SALV_TABLE
        • 1.1全屏salv:
        • 1.2 指定容器的salv(非必要)
      • 2. 层级ALV:CL_SALV_HIERSEQ_TABLE
      • 3. 树状ALV
  • 总结


# 前言

上一节“ABAP项目砖家之旅-screen和表单项目实战”,已经对我们项目中常用的自定义表录入界面和展示界面做了开发,快速开发的基础上也存在显示不美观、难以导出等缺陷。这一节我们使用alv报表对上一节的界面进行重写,写成一个带维护界面的alv报表。还是那句话,搬砖也要有急速成型的办法,不然赶不上项目要求,搞熟alv吧


一、ALV相关介绍

ALV-SAP LIST VIEW列表界面报表。主要是通过几个ALV专用的函数来实现数据展现,附带排序、筛选、打印、导出等工具栏和功能。按照abap行业共识,主要分两类有:简易的,两层ALV,分等级连续的列表和树形结构的ALV。本人姑且把用到的归纳一下,没用到的,其实大家可以按照项目要求网上找找高手的博文了解下。英文水平高的直接进去SE37看函数参数、结构、帮助也可以。

二、ALV使用步骤

主要原理剖析:alv使用本质是函数调用,alv函数通用参数有表头字段fieldcat(有自定结构类的,参照表结构…),数据表,布局格式参数等,下面举例和上代码:

(一)普通alv和扩展alv

1.普通alv函数

SE37函数为:REUSE_ALV_GRID_DISPLAY /REUSE_ALV_LIST_DISPLAY ,常用GRID函数

1.1函数参数解释
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
*          I_INTERFACE_CHECK                 = ' ' "检查接口一致性
*          I_BYPASSING_BUFFER                = ' ' "是否使用缓存
*          I_BUFFER_ACTIVE                   = ' ' "是否激活缓存
          I_CALLBACK_PROGRAM                = sy-repid  "调用ALV的程序,不填写默认当前程序,也可以SY-REPID
          I_CALLBACK_PF_STATUS_SET          = 'FRM_SET_STATUS' "alv工具栏的FRM,使用后,标准ALV的status会失效!
*          I_CALLBACK_USER_COMMAND           = ' ' "alv用户按键和菜单FRM
*          I_CALLBACK_TOP_OF_PAGE            = ' ' "抬头内容FRM
*          I_CALLBACK_HTML_TOP_OF_PAGE       = ' ' "HTML页眉内容FRM
*          I_CALLBACK_HTML_END_OF_LIST       = ' ' "HTML页脚frm
*          I_STRUCTURE_NAME                  =     "se11定义的结构名称
*          I_BACKGROUND_ID                   = ' ' "alv背景图案的id
*          I_GRID_TITLE                      =      "alv标题
*          I_GRID_SETTINGS                   =       "alvgrid信息
*          IS_LAYOUT                         =      "输出布局样式
*          IT_FIELDCAT                       =      "列标题、长度等
*          IT_EXCLUDING                      =       "要隐藏的alv标准工具栏
*          IT_SPECIAL_GROUPS                 =       "分组文本内表
*          IT_SORT                           =       "排序按钮的设置
*          IT_FILTER                         =        "筛选设置
*          IS_SEL_HIDE                       =        "替换/修改select-option的值
*          I_DEFAULT                         = 'X'    "可以定义默认布局,space标识不可以修改默认
*          I_SAVE                            = ' '    "是否可以保存ALV布局:X-保存全局变式 U-保存用户变式 A-都保存 space-不能保存
*          IS_VARIANT                        =        "表格布局变式
*          IT_EVENTS                         =        "设置事件 类型为slis_t_event
*          IT_EVENT_EXIT                     =        "事件执行后回调(增强出口)
*          IS_PRINT                          =        "后台打印参数设置
*          IS_REPREP_ID                      =        "
*          I_SCREEN_START_COLUMN             = 0      "对话框形式显示的开始列
*          I_SCREEN_START_LINE               = 0
*          I_SCREEN_END_COLUMN               = 0
*          I_SCREEN_END_LINE                 = 0
*          I_HTML_HEIGHT_TOP                 = 0
*          I_HTML_HEIGHT_END                 = 0
*          IT_ALV_GRAPHICS                   =        "是否可以在图表显示ALV
*          IT_HYPERLINK                      =        "使用超链接
*          IT_ADD_FIELDCAT                   =
*          IT_EXCEPT_QINFO                   =
*          IR_SALV_FULLSCREEN_ADAPTER        =
*        IMPORTING
*          E_EXIT_CAUSED_BY_CALLER           =
*          ES_EXIT_CAUSED_BY_USER            =
         TABLES
           T_OUTTAB                          = LT_ZTTEST "要显示的内表信息,结构必须和上面struct或者fieldcat一致
        EXCEPTIONS
          PROGRAM_ERROR                     = 1
          OTHERS                            = 2
                 .
1.2ALV状态栏STATUS

这里说一下ALV标准界面STATUS按钮和log,大家如用自己的状态栏,可以参考下面的按键添加
ABAP项目砖家之旅-alv项目实战_第1张图片
也可以通过SE90直接复制标准ALV的:
ABAP项目砖家之旅-alv项目实战_第2张图片

ABAP项目砖家之旅-alv项目实战_第3张图片
复制到我们开发的程序:
ABAP项目砖家之旅-alv项目实战_第4张图片

1.3定义函数需要的输入参数
*DEFINE fieldcat 宏脚本
  DEFINE build_fieldcat .
    ls_fieldcat-fieldname = &1 . "字段名
    ls_fieldcat-tabname = &2 .  "内表名
    ls_fieldcat-seltext_m = &3 . "字段描述
    ls_fieldcat-outputlen = &4 . "字段长度
    ls_fieldcat-no_zero = &5. "为0是否显示为空
    ls_fieldcat-just = &6 . "对齐 L R M
    ls_fieldcat-edit_mask = &7 . "
    ls_fieldcat-decimals_out = &8 ."小数位
    APPEND ls_fiELdcat TO lt_fieldcat .
    CLEAR ls_fieldcat .
  END-OF-DEFINITION.
 "1.定义fieldcat
      build_fieldcat 'MANDT' 'LT_ZTTEST' '客户端' 3 'X' 'L' '' 0.
      build_fieldcat 'ZNAME' 'LT_ZTTEST' '客户端' 5 'X' 'L' '' 0.
      build_fieldcat 'ZDESC' 'LT_ZTTEST' '客户端' 10 'X' 'L' '' 0.
      build_fieldcat 'ZDATE' 'LT_ZTTEST' '客户端' 10 'X' 'L' '' 0.
      build_fieldcat 'ZPSW'  'LT_ZTTEST' '客户端' 3 'X' 'L' '' 0.
      build_fieldcat 'ZAREA' 'LT_ZTTEST' '客户端' 3 'X' 'L' '' 0.
      build_fieldcat 'ZWERKS' 'LT_ZTTEST' '客户端' 3 'X' 'L' '' 0.
      "2.定义layout参数
      ls_layout-window_titlebar = '现金流量表'.
*      ls_layout-coltab_fieldname =  'S_COLOR'. "内表中存放4位颜色代码字段
      ls_layout-info_fieldname = 'S_COLOR' .
*    ls_layout-colwidth_optimize = 'X'. 跟下面设置 fieldcat 宽度冲突,任选一
      ls_layout-zebra = 'X'.
      ls_layout-numc_sum = 'X'.
      ls_layout-reprep = 'X'.
      ls_layout-expand_all = 'X'.
*      ls_layout-def_status = 'X'.
      ls_layout-totals_only = 'X'.

      "3.定义其他参数
      l_buffer = abap_false.
      l_save = 'A'.
      ls_variant-REPORT = sy-repid.
      ls_variant-handle = '0001'.
      ls_variant-username = sy-uname .
1.4 select数据
1.5 调用ALV函数
FORM GET_DATA .
  REFRESH  LT_ZTTEST.
  select * from zttest_01 into TABLE LT_ZTTEST
    where zname in s_zname
    and zdesc in s_zdesc .
ENDFORM.
 CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
        EXPORTING
*          I_INTERFACE_CHECK                 = ' ' "检查接口一致性
*          I_BYPASSING_BUFFER                = ' ' "是否使用缓存
*          I_BUFFER_ACTIVE                   = ' ' "是否激活缓存
          I_CALLBACK_PROGRAM                = sy-repid  "调用ALV的程序,不填写默认当前程序,也可以SY-REPID
*          I_CALLBACK_PF_STATUS_SET          = 'FRM_SET_STATUS' "alv工具栏的FRM,使用后,标准ALV的status会失效!
*          I_CALLBACK_USER_COMMAND           = ' ' "alv用户按键和菜单FRM
*          I_CALLBACK_TOP_OF_PAGE            = ' ' "抬头内容FRM
*          I_CALLBACK_HTML_TOP_OF_PAGE       = ' ' "HTML页眉内容FRM
*          I_CALLBACK_HTML_END_OF_LIST       = ' ' "HTML页脚frm
*          I_STRUCTURE_NAME                  =     "se11定义的结构名称
*          I_BACKGROUND_ID                   = ' ' "alv背景图案的id
*          I_GRID_TITLE                      =      "alv标题
*          I_GRID_SETTINGS                   =       "alvgrid信息
          IS_LAYOUT                         =   la_layout   "输出布局样式
          IT_FIELDCAT                       =   lt_fieldcat[] "列标题、长度等
*          IT_EXCLUDING                      =       "要隐藏的alv标准工具栏
*          IT_SPECIAL_GROUPS                 =       "分组文本内表
*          IT_SORT                           =       "排序按钮的设置
*          IT_FILTER                         =        "筛选设置
*          IS_SEL_HIDE                       =        "替换/修改select-option的值
*          I_DEFAULT                         = 'X'    "可以定义默认布局,space标识不可以修改默认
          I_SAVE                            = l_save "'A'    "是否可以保存ALV布局:X-保存全局变式 U-保存用户变式 A-都保存 space-不能保存
          IS_VARIANT                        = ls_variant      "表格布局变式
*          IT_EVENTS                         =        "设置事件 类型为slis_t_event
*          IT_EVENT_EXIT                     =        "事件执行后回调(增强出口)
*          IS_PRINT                          =        "后台打印参数设置
*          IS_REPREP_ID                      =        "
*          I_SCREEN_START_COLUMN             = 0      "对话框形式显示的开始列
*          I_SCREEN_START_LINE               = 0
*          I_SCREEN_END_COLUMN               = 0
*          I_SCREEN_END_LINE                 = 0
*          I_HTML_HEIGHT_TOP                 = 0
*          I_HTML_HEIGHT_END                 = 0
*          IT_ALV_GRAPHICS                   =        "是否可以在图表显示ALV
*          IT_HYPERLINK                      =        "使用超链接
*          IT_ADD_FIELDCAT                   =
*          IT_EXCEPT_QINFO                   =
*          IR_SALV_FULLSCREEN_ADAPTER        =
*        IMPORTING
*          E_EXIT_CAUSED_BY_CALLER           =
*          ES_EXIT_CAUSED_BY_USER            =
         TABLES
           T_OUTTAB                          = LT_ZTTEST[] "要显示的内表信息,结构必须和上面struct或者fieldcat一致
        EXCEPTIONS
          PROGRAM_ERROR                     = 1
          OTHERS                            = 2
                 .
       IF SY-SUBRC <> 0.
         MESSAGE ID SY-msgid TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 .
       ENDIF.

注意:一般table类的输入变量,最好使用[],避免定义的内表有表头而跟函数原来的变量结构不一致导致st22报错

(二)树状和层次alv

树状和层次ALV可以通过函数引用实现,也可以通过类实现,类实现的alv我们都放到ooalv介绍吧。

1. 层次结构 REUSE_ALV_HIERSEQ_LIST_DISPLAY

      DATA:BEGIN OF LT_HEADER OCCURS 0,
                 MANDT TYPE MANDT,
                 ZWERKS TYPE WERKS_D,
                END OF LT_HEADER ,
                BEGIN OF LT_ITEM OCCURS 0,
                  MANDT TYPE MANDT,
                  ZWERKS TYPE WERKS_D,
                  ZNAME TYPE zttest_01-ZNAME,
                  ZDESC TYPE zttest_01-ZDESC,
                  ZDATE TYPE zttest_01-ZDATE,
                  ZAREA TYPE zttest_01-ZAREA,
                END OF LT_ITEM ,
                GS_KEYINFO  type slis_keyinfo_alv,"支持5个关联关系
                LS_HEADER LIKE LINE OF LT_HEADER,
                LS_ITEM   LIKE LINE OF LT_ITEM .

          "1.fieldcat 构建
      build_fieldcat 'MANDT' 'LT_ITEM' '客户端' 3 'X' 'L' '' 0. "因为数据都是lt_item显示为主,故填写结构为LT_ITEM,表头是合并层次
      build_fieldcat 'ZNAME' 'LT_ITEM' '名称' 5 'X' 'L' '' 0.
      build_fieldcat 'ZDESC' 'LT_ITEM' '描述' 10 'X' 'L' '' 0.
      build_fieldcat 'ZDATE' 'LT_ITEM' '截止日期' 10 'X' 'L' '' 0.
*      build_fieldcat 'ZPSW'  'LT_ITEM' '密码文本' 3 'X' 'L' '' 0.
      build_fieldcat 'ZAREA' 'LT_ITEM' '区域' 3 'X' 'L' '' 0.
      build_fieldcat 'ZWERKS' 'LT_ITEM' '门店' 3 'X' 'L' '' 0.
          "2.其他参数(类似普通alv这里不重复说明)
          "3.KEY关联值
           CLEAR gs_keyinfo .
           gs_keyinfo-header01 = 'MANDT' .
           gs_keyinfo-item01   = 'MANDT'.
           gs_keyinfo-header02 = 'ZWERKS'.
           gs_keyinfo-item02   = 'ZWERKS'.

          "4.数据整理
                LOOP AT LT_ZTTEST ASSIGNING <FS>.
                  LS_HEADER-ZWERKS = <FS>-ZWERKS.
                  LS_HEADER-MANDT = <FS>-MANDT.
                  APPEND LS_HEADER TO LT_HEADER .
                  LS_ITEM-MANDT =    <FS>-MANDT .
                  LS_ITEM-ZWERKS = <FS>-ZWERKS.
                  LS_ITEM-ZNAME = <FS>-ZNAME.
                  LS_ITEM-zdesc = <fs>-zdesc.
                  ls_item-zdate = <fs>-zdate .
                  ls_item-zarea = <fs>-ZAREA .
                  APPEND LS_ITEM TO  LT_ITEM .
                ENDLOOP.
                DELETE ADJACENT DUPLICATES FROM LT_HEADER .
                DELETE ADJACENT DUPLICATES FROM LT_ITEM   .

           CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
             EXPORTING
*              I_INTERFACE_CHECK              = ' '
*              I_CALLBACK_PROGRAM             =
*              I_CALLBACK_PF_STATUS_SET       = ' '
*              I_CALLBACK_USER_COMMAND        = ' '
*              IS_LAYOUT                      =
               IT_FIELDCAT                    = lt_fieldcat[]
*              IT_EXCLUDING                   =
*              IT_SPECIAL_GROUPS              =
*              IT_SORT                        =
*              IT_FILTER                      =
*              IS_SEL_HIDE                    =
*              I_SCREEN_START_COLUMN          = 0
*              I_SCREEN_START_LINE            = 0
*              I_SCREEN_END_COLUMN            = 0
*              I_SCREEN_END_LINE              = 0
*              I_DEFAULT                      = 'X'
*              I_SAVE                         = ' '
*              IS_VARIANT                     =
*              IT_EVENTS                      =
*              IT_EVENT_EXIT                  =
               I_TABNAME_HEADER               = 'LT_HEADER'
               I_TABNAME_ITEM                 = 'LT_ITEM'
*              I_STRUCTURE_NAME_HEADER        =
*              I_STRUCTURE_NAME_ITEM          =
               IS_KEYINFO                     = gs_keyinfo
*              IS_PRINT                       =
*              IS_REPREP_ID                   =
*              I_BYPASSING_BUFFER             =
*              I_BUFFER_ACTIVE                =
*              IR_SALV_HIERSEQ_ADAPTER        =
*              IT_EXCEPT_QINFO                =
*              I_SUPPRESS_EMPTY_DATA          = ABAP_FALSE
*            IMPORTING
*              E_EXIT_CAUSED_BY_CALLER        =
*              ES_EXIT_CAUSED_BY_USER         =
             TABLES
               T_OUTTAB_HEADER                = LT_HEADER[]
               T_OUTTAB_ITEM                  = LT_ITEM[]
            EXCEPTIONS
              PROGRAM_ERROR                  = 1
              OTHERS                         = 2
                     .
           IF SY-SUBRC <> 0.
              MESSAGE ID SY-msgid TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 .
           ENDIF.

ABAP项目砖家之旅-alv项目实战_第5张图片

2. 树状 RS_TREE_LIST_DISPLAY

这个alv比较简单,主要是使用RS_TREE_CONSTRUCT写入node结构,然后调用RS_TREE_LIST_DISPLAY显示即可。功能不算强大

 DATA:LT_NODE TYPE TABLE OF SNODETEXT ,WA_NODE LIKE LINE OF LT_NODE .
           "1.构造node
           CLEAR :LT_NODE[].
           CLEAR WA_NODE.
           WA_NODE-TYPE = 'T'."TREE 一层
           WA_NODE-ID = 'T1'.
           WA_NODE-NAME = '顶层' .
           WA_NODE-tlevel = 1. "层次1
           WA_NODE-nlength = '15'.
           WA_NODE-COLOR = '4'.
           WA_NODE-TEXT = '顶层描述' .
           WA_NODE-tlength = '10'.
           WA_NODE-tcolor = 3.
           APPEND WA_NODE TO LT_NODE .

            SORT LT_ZTTEST BY MANDT ZWERKS ZNAME .
            DATA:L_TMP(3) TYPE C .
           LOOP AT LT_ZTTEST ASSIGNING <FS>.
             AT END OF ZWERKS .
               CLEAR WA_NODE.
               WA_NODE-TYPE = 'P'."TREE 一层
               WA_NODE-ID = SY-TABIX .
               WA_NODE-NAME = <FS>-ZWERKS .
               WA_NODE-tlevel = 2. "层次1
               WA_NODE-nlength = '15'.
               WA_NODE-COLOR = '1'.
               WA_NODE-TEXT = <FS>-ZDESC .
               WA_NODE-tlength = '10'.
               WA_NODE-tcolor = 3.
               APPEND WA_NODE TO LT_NODE .

               LOOP AT LT_ZTTEST INTO WA_ZTTEST WHERE MANDT = <FS>-MANDT AND ZWERKS = <FS>-ZWERKS.
                    CLEAR WA_NODE.
                    WA_NODE-TYPE = 'P'."TREE 一层
                    WA_NODE-ID = SY-TABIX .
                    WA_NODE-NAME = WA_ZTTEST-ZNAME .
                    WA_NODE-tlevel = 3. "层次1
                    WA_NODE-nlength = '15'.
                    WA_NODE-COLOR = '2'.
                    CONCATENATE WA_NODE-ID WA_ZTTEST-zname WA_ZTTEST-ZDESC WA_ZTTEST-ZAREA WA_ZTTEST-ZWERKS INTO WA_NODE-TEXT .
                    WA_NODE-tlength = '200'.
                    WA_NODE-tcolor = 4.
                    APPEND WA_NODE TO LT_NODE .
               ENDLOOP.
            ENDAT.

           ENDLOOP.

            CALL FUNCTION 'RS_TREE_CONSTRUCT'
*             EXPORTING
*               INSERT_ID                = '000000'
*               RELATIONSHIP             = ' '
*               LOG                      =
              TABLES
                NODETAB                  = LT_NODE
*             EXCEPTIONS
*               TREE_FAILURE             = 1
*               ID_NOT_FOUND             = 2
*               WRONG_RELATIONSHIP       = 3
*               OTHERS                   = 4
                      .
            IF SY-SUBRC <> 0.
* Implement suitable error handling here
            ENDIF.
           "node alv展示
            CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
             EXPORTING
               CALLBACK_PROGRAM                = sy-repid "当前窗口
*               CALLBACK_USER_COMMAND           =
*               CALLBACK_TEXT_DISPLAY           =
*               CALLBACK_MOREINFO_DISPLAY       =
*               CALLBACK_COLOR_DISPLAY          =
*               CALLBACK_TOP_OF_PAGE            =
*               CALLBACK_GUI_STATUS             =
*               CALLBACK_CONTEXT_MENU           =
               STATUS                          = 'IMPLICIT'
               CHECK_DUPLICATE_NAME            = '1'
               COLOR_OF_NODE                   = '4'
               COLOR_OF_MARK                   = '3'
               COLOR_OF_LINK                   = '1'
               COLOR_OF_MATCH                  = '5'
*               LOWER_CASE_SENSITIVE            = ' '
*               MODIFICATION_LOG                = ' '
               NODE_LENGTH                     = 30
               TEXT_LENGTH                     = 75
*               TEXT_LENGTH1                    = 0
*               TEXT_LENGTH2                    = 0
*               RETURN_MARKED_SUBTREE           = ' '
*               SCREEN_START_COLUMN             = 0
*               SCREEN_START_LINE               = 0
*               SCREEN_END_COLUMN               = 0
*               SCREEN_END_LINE                 = 0
*               SUPPRESS_NODE_OUTPUT            = ' '
*               LAYOUT_MODE                     = ' '
*               USE_CONTROL                     = STREE_USE_LIST
*             IMPORTING
*               F15                             =
                      .

      ENDIF.

ABAP项目砖家之旅-alv项目实战_第6张图片
这个树状ALV实现的功能有局限,后面ooalv也有树状类,可以实现更强大的功能,大家向下走吧。

(三)ooalv

oo alv主要是使用se24的alv相关的类来实现alv报表。主要类有:

1. SALV:cl_gui_alv_grid 、 CL_SALV_TABLE

1.1全屏salv:
 DATA : LCL_ALV TYPE REF TO CL_SALV_TABLE,
            gr_functions TYPE REF TO cl_salv_functions_list,
            gr_layout TYPE REF TO cl_salv_layout,gs_program TYPE salv_s_layout_key ,"该结构包含了布局变式所属程序名.
            gr_columns TYPE REF TO cl_salv_columns_table, gr_column TYPE REF TO Cl_salv_column_table..


        CL_SALV_TABLE=>FACTORY(
           EXPORTING
             list_display = abap_false "是否列表实现 false-grid  R_CONTAINER -控件 CONTAINER_NAME容器名
           IMPORTING
             R_SALV_TABLE = LCL_ALV
*             R_CONTAINER =
*             CONTAINER_NAME =''
           CHANGING
             T_TABLE = LT_ZTTEST[]
           ).
           gr_functions = LCL_ALV->GET_FUNCTIONS( ).
           gr_functions->SET_DEFAULT( ) . "激活ALV工具栏按钮

           "layout  视图保存功能
           gr_layout = LCL_ALV->get_layout( ) .
           gs_program-report = sy-repid ."指定程序
           gr_layout->set_key( gs_program ).
           gr_layout->set_save_restriction( cl_salv_layout=>restrict_none ) . "允许保存标识
           
           "fieldcat:column  设置标题 是否显示列(可以在layout找出来)
           gr_columns = LCL_ALV->get_columns( ) .
*           gr_column ?= gr_columns->get_column( 'MANDT' ). "需要处理的列,如隐藏
*           gr_column->set_visible( cl_salv_columns_table=>false )."隐藏

           clear gr_column .
              gr_column ?= gr_columns->get_column( 'MANDT' ).
              gr_column->set_long_text('客户端').
              gr_column->set_medium_text('客户端').
              gr_column->set_short_text('cnt').

              gr_column ?= gr_columns->get_column( 'ZNAME' ).
              gr_column->set_long_text('用户名编码').
              gr_column->set_medium_text('用户名').
              gr_column->set_short_text('用户').

              gr_column ?= gr_columns->get_column( 'ZDESC' ).
              gr_column->set_long_text('用户描述').
              gr_column->set_medium_text('用户名').
              gr_column->set_short_text('姓名').

              gr_column ?= gr_columns->get_column( 'ZDATE' ).
              gr_column->set_long_text('用户截止日期').
              gr_column->set_medium_text('截止日期').
              gr_column->set_short_text('截止日期').

              gr_column ?= gr_columns->get_column( 'ZPSW' ).
              gr_column->set_long_text('用户二级密码').
              gr_column->set_medium_text('用户密码').
              gr_column->set_short_text('密码').

              gr_column ?= gr_columns->get_column( 'ZAREA' ).
              gr_column->set_long_text('门店所在区域').
              gr_column->set_medium_text('区域').
              gr_column->set_short_text('区域').

              gr_column ?= gr_columns->get_column( 'ZWERKS' ).
              gr_column->set_long_text('门店编码').
              gr_column->set_medium_text('门店').
              gr_column->set_short_text('门店').

           LCL_ALV->DISPLAY( ).
1.2 指定容器的salv(非必要)

1.新建屏幕并定义停靠容器的类cl_gui_docking_container
2.在POB写入代码
3. CL_SALV_TABLE=>FACTORY()调用输入R_CONTAINER CONTAINER_NAME 参数,这部分知识后面再看

2. 层级ALV:CL_SALV_HIERSEQ_TABLE

  DATA: LT_BINDING TYPE  SALV_T_HIERSEQ_BINDING ,wa_binding like LINE OF LT_BINDING,
              GR_TB TYPE REF TO CL_SALV_HIERSEQ_TABLE ,
              BEGIN OF LT_H OCCURS 0,
                 MANDT TYPE MANDT,
                 ZWERKS TYPE WERKS_D,
                END OF LT_H ,
                BEGIN OF LT_I OCCURS 0,
                  MANDT TYPE MANDT,
                  ZWERKS TYPE WERKS_D,
                  ZNAME TYPE zttest_01-ZNAME,
                  ZDESC TYPE zttest_01-ZDESC,
                  ZDATE TYPE zttest_01-ZDATE,
                  ZAREA TYPE zttest_01-ZAREA,
                END OF LT_I ,
*                GS_KEYINFO  type slis_keyinfo_alv,"支持5个关联关系
                LS_H  LIKE LINE OF LT_H,
                LS_I   LIKE LINE OF LT_I .
          "1.KEY关联值
             CLEAR wa_binding.
             wa_binding-MASTER = 'MANDT'.
             wa_binding-SLAVE = 'MANDT'.
             APPEND wa_binding TO LT_BINDING .
             CLEAR wa_binding.
             wa_binding-MASTER = 'ZWERKS'.
             wa_binding-SLAVE = 'ZWERKS'.
             APPEND wa_binding TO LT_BINDING .

          "2.数据整理
                LOOP AT LT_ZTTEST ASSIGNING <FS>.
                  LS_H-ZWERKS = <FS>-ZWERKS.
                  LS_H-MANDT = <FS>-MANDT.
                  APPEND LS_H TO LT_H .
                  LS_I-MANDT =    <FS>-MANDT .
                  LS_I-ZWERKS = <FS>-ZWERKS.
                  LS_I-ZNAME = <FS>-ZNAME.
                  LS_I-zdesc = <fs>-zdesc.
                  ls_i-zdate = <fs>-zdate .
                  ls_i-zarea = <fs>-ZAREA .
                  APPEND LS_I TO  LT_I .
                ENDLOOP.
                DELETE ADJACENT DUPLICATES FROM LT_H .
                DELETE ADJACENT DUPLICATES FROM LT_I   .
*         TRY.
         CALL METHOD CL_SALV_HIERSEQ_TABLE=>FACTORY  "这是标准的静态调用方法,通过se38的模式可以自动生成;二上面->是快速的调用方法,在模式显示不出来
           EXPORTING
             T_BINDING_LEVEL1_LEVEL2 = LT_BINDING[]
           IMPORTING
             R_HIERSEQ               = GR_TB
           CHANGING
             T_TABLE_LEVEL1          = LT_H[]
             T_TABLE_LEVEL2          = LT_I[]
             .
*          CATCH CX_SALV_DATA_ERROR .
*          CATCH CX_SALV_NOT_FOUND .
*         ENDTRY.
         "3.定义显示字段、格式等 GET_COLUMNS  GET_COLUMNS GET_SORTS GET_LAYOUT等(略)
         "4.执行显示函数
          GR_TB->DISPLAY( ) .

ABAP项目砖家之旅-alv项目实战_第7张图片

3. 树状ALV

CL_GUI_ALV_TREE或者salv:cl_salv_tree + cl_salv_tree_settings 这里的树状alv相对上面FM的树状比较实用,可以调整很多内容,还是老套路,FACTORY实例化,然后调用其他方法,最后调用DISPLAY().
cl_gui_custom_container


 IF C_1 = 'X'. "salv全屏不加container
        data: gr_tree   type ref to cl_salv_tree.
        data: gt_outtab type standard table of zttest_01.
        data: lo_nodes type ref to cl_salv_nodes.
        data: lo_node type ref to cl_salv_node.
        data: l_relate_key type lvc_nkey.
        "1.根据数据创建salv树实例
       try.
         cl_salv_tree=>factory(
          importing
            r_salv_tree = gr_tree
          changing
            t_table      = gt_outtab ). "空的表
        catch CX_SALV_NO_NEW_DATA_ALLOWED CX_SALV_ERROR.
          MESSAGE 'FACTORY ERR' type 'S' DISPLAY LIKE 'E'.
         exit.
        endtry.

         "2.添加GUI 状态和按键事件
         gr_tree->set_screen_status(
         pfstatus      =  'ZTEST_DYNPROSTA0100' "'SALV_STANDARD'
         report        =  SY-repid
         set_functions =  gr_tree->c_functions_all ).


         "3.FIELDCAT列名和显示格式等
      data: lr_columns type ref to cl_salv_columns_tree,
         lr_column type ref to cl_salv_column.
         "格式
         lr_columns = gr_tree->get_columns( ).
         lr_columns->set_optimize( ABAP_TRUE )."自动调整
         "字段名
         lr_column ?= lr_columns->get_column( 'MANDT' ).
         lr_column->set_technical( if_salv_c_bool_sap=>true )."直接使用技术名称(domain的名称)

         lr_column ?= lr_columns->get_column( 'ZNAME' ).
         lr_column->set_long_text('用户名编码'). "指定名称(因为使用char'等非sapdomain,没有技术名称
         lr_column->set_medium_text('用户名').
         lr_column->set_short_text('用户').

         lr_column ?= lr_columns->get_column( 'ZDESC' ).
         lr_column->set_long_text('用户描述').
         lr_column->set_medium_text('用户名').
         lr_column->set_short_text('姓名').

         lr_column ?= lr_columns->get_column( 'ZDATE' ).
         lr_column->set_long_text('用户截止日期').
         lr_column->set_medium_text('截止日期').
         lr_column->set_short_text('截止日期').

         lr_column ?= lr_columns->get_column( 'ZPSW' ).
         lr_column->set_long_text('用户二级密码').
         lr_column->set_medium_text('用户密码').
         lr_column->set_short_text('密码').

         lr_column ?= lr_columns->get_column( 'ZAREA' ).
         lr_column->set_long_text('门店所在区域').
         lr_column->set_medium_text('区域').
         lr_column->set_short_text('区域').

         lr_column ?= lr_columns->get_column( 'ZWERKS' ).
         lr_column->set_long_text('门店编码').
         lr_column->set_medium_text('门店').
         lr_column->set_short_text('门店').
         "4.添加node和数据
         lo_nodes = gr_tree->get_nodes( ) .
         LOOP AT LT_ZTTEST ASSIGNING <fs>.
           on CHANGE OF <fs>-zwerks. "主 node
            lo_node = lo_nodes->add_node(
              related_node = '' "关联的node
              data_row = <fs>   "数据行
              relationship = cl_salv_nodes=>IF_SALV_C_NODE_RELATION~LAST_CHILD "关联关系 下一层子层
            ).
              l_relate_key = lo_node->get_key( ).
           endon .

           on change of <fs>-zname .
            lo_node = lo_nodes->add_node(
              related_node = l_relate_key "关联的node ,副本node
              data_row = <fs>   "数据行
              relationship = cl_salv_nodes=>IF_SALV_C_NODE_RELATION~LAST_CHILD "关联关系 下一层子层
            ).
           endon.
         ENDLOOP.

         "5.display the table
         gr_tree->display( ).

ABAP项目砖家之旅-alv项目实战_第8张图片
可以使用set_function来处理按键事件,这里不作重复介绍了

总结

这一节内容到此为止,后面继续找时间总结下增强等进阶知识。ALV的种类就这么多中,但是里面的内容非常多也很实用,大家项目中可以根据业务需求,在se24里面看看细节,修行靠个人。此外sap本身有很多demo程序,可以在se38输入关键字+demo找找用来学习。

你可能感兴趣的:(SAP,ABAP浅尝截止,sap,abap)