首页 > 其他分享 >ABAP基础一:ALV样例

ABAP基础一:ALV样例

时间:2023-01-05 17:34:17浏览次数:47  
标签:gt show fieldcat data ty 样例 ABAP ALV TYPE

REPORT zly_report.

*********Report Demo****************************************
*本程序主要将普通的ALV报表做拆分讲解
*一个简单的ALV包括以下一个部分
*1.数据定义部分:这部分用来定义全局变量,包括表,结构,工作区,宏,变量等
*2.选择屏幕定义部分:这部分是用来定义选择屏幕,包括屏幕的布局等
*3.数据处理部分:这部分包括数据查询和数据处理两块,根据不同的需求和查询条件将查询结果处理,最红汇总到需要展示的内表种
*4.字段目录设置:这部分是设置ALV需要展示的字段,字段描述以及alv的属性等
*5.数据展示:调用alv函数,展示内表数据
*6.功能处理:对ALV特定功能处理

*********数据定义部分***************************************

TABLES:zhsb_order_index."用tables关键字定义一个全局的工作区/结构
TYPES:BEGIN OF ty_show,
        guid         TYPE crmt_object_guid,
        object_id    TYPE crmt_object_id_db,
        process_type TYPE crmt_process_type,
        description  TYPE crmt_process_description,
        posting_date TYPE crmt_posting_date,
        sales_org    TYPE crmt_sales_org,
        created_by   TYPE crmt_created_by,
        provider     TYPE bu_partner,
        stat_user    TYPE crm_j_status,
        zzzserialno  TYPE zserialno,
        zzzmodeno    TYPE zmodeno,
        zzfld00000n  TYPE zdtel00003y,
        name_org1    TYPE bu_nameor1,
        name_org2    TYPE bu_nameor2,
        country      TYPE land1,
        region       TYPE regio,
        city1        TYPE ad_city1,
        street       TYPE ad_street,
      END OF ty_show,
      BEGIN OF ty_but000,
        partner    TYPE bu_partner,
        name_org1  TYPE bu_nameor1,
        name_org2  TYPE bu_nameor2,
        addrnumber TYPE ad_addrnum,
      END OF ty_but000,
      BEGIN OF ty_adrc,
        addrnumber TYPE ad_addrnum,
        country    TYPE land1,
        region     TYPE regio,
        city1      TYPE ad_city1,
        street     TYPE ad_street,
      END OF ty_adrc.

DATA:gt_show   TYPE TABLE OF ty_show, "展示的内表
     gw_show   LIKE LINE OF gt_show, "数据处理对应的工作区
     vt_show   TYPE TABLE OF ty_show, "临时表
     gt_but000 TYPE TABLE OF ty_but000, "相关表
     gw_but000 LIKE LINE OF gt_but000, "相关表的工作区
     gt_adrc   TYPE TABLE OF ty_adrc, "相关表
     gw_adrc   LIKE LINE OF gt_adrc. "相关表的工作区

DATA:gt_fieldcat TYPE lvc_t_fcat, "字段目录内表
     gs_fieldcat LIKE LINE OF gt_fieldcat, "字段目录工作区
     ls_layout   TYPE lvc_s_layo. "布局

DATA:lr_grid TYPE REF TO cl_gui_alv_grid.

DATA:gt_event TYPE slis_t_event,
     gw_event LIKE LINE OF gt_event.

DATA:gw_grid_setting TYPE lvc_s_glay.



DEFINE append_fieldcat .
  CLEAR gs_fieldcat.
  gs_fieldcat-fieldname = &1."字段
  gs_fieldcat-scrtext_l = &2."显示的描述
  gs_fieldcat-outputlen = &3."输出长度
  gs_fieldcat-no_zero = &4."不显示前导0
*  gs_fieldcat-just = 'C'."居中对齐,R,L
  IF &1 = 'ZZZSERIALNO'.
    gs_fieldcat-edit = 'X'.
  ENDIF.
  APPEND gs_fieldcat TO gt_fieldcat.
END-OF-DEFINITION.





*********选择屏幕************************************************

SELECTION-SCREEN:BEGIN OF BLOCK blk01 WITH FRAME TITLE text-001.
SELECT-OPTIONS:s_id FOR zhsb_order_index-object_id,
               s_type FOR zhsb_order_index-process_type,
               s_date FOR zhsb_order_index-posting_date,
               s_sale FOR zhsb_order_index-sales_org.
SELECTION-SCREEN END OF BLOCK blk01.
SELECTION-SCREEN:BEGIN OF BLOCK blk02 WITH FRAME TITLE text-002.
SELECT-OPTIONS:s_prv FOR zhsb_order_index-provider.
SELECTION-SCREEN END OF BLOCK blk02.



START-OF-SELECTION.
**********数据处理*************************************************
  PERFORM frm_get_data."数据查询
  PERFORM frm_pro_data."数据处理

  PERFORM frm_set_field."设置字段目录,布局

  PERFORM frm_show.

*END-OF-SELECTION."非逻辑数据库不需要



*&---------------------------------------------------------------------*
*&      Form  FRM_GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_get_data .
  "取主表数据
  SELECT  guid
          object_id
          process_type
          description
          posting_date
          sales_org
          created_by
          provider
          stat_user
          zzzserialno
          zzzmodeno
          zzfld00000n
    INTO CORRESPONDING FIELDS OF TABLE gt_show
    FROM zhsb_order_index
    WHERE object_id IN s_id
    AND   process_type IN s_type
    AND   posting_date IN s_date
    AND   sales_org IN s_sale
    AND   provider IN s_prv.

  "临时表数据准备
  vt_show[] = gt_show[].
  SORT vt_show BY provider.
  DELETE ADJACENT DUPLICATES FROM vt_show COMPARING provider.
  DELETE vt_show WHERE provider = ''.

  IF vt_show[] IS NOT INITIAL.
    "取副表数据
    SELECT but000~partner
           but000~name_org1
           but000~name_org2
           but020~addrnumber
      INTO TABLE gt_but000
      FROM but000
      INNER JOIN but020
      ON but000~partner = but020~partner
      FOR ALL ENTRIES IN vt_show
      WHERE but000~partner = vt_show-provider.

    IF gt_but000[] IS NOT INITIAL.
      "取副表数据
      SELECT addrnumber
        country
        region
        city1
        street
        INTO TABLE gt_adrc
        FROM adrc
        FOR ALL ENTRIES IN gt_but000
        WHERE addrnumber = gt_but000-addrnumber.
    ENDIF.

    REFRESH:vt_show[].
  ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_PRO_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_pro_data .

  LOOP AT gt_show INTO gw_show.
    READ TABLE gt_but000 INTO gw_but000 WITH KEY partner = gw_show-provider.
    IF sy-subrc = 0.
      gw_show-name_org1 = gw_but000-name_org1.
      gw_show-name_org2 = gw_but000-name_org2.
      READ TABLE gt_adrc INTO gw_adrc WITH KEY addrnumber = gw_but000-addrnumber.
      IF sy-subrc = 0.
        gw_show-country = gw_adrc-country.
        gw_show-region = gw_adrc-region.
        gw_show-city1 = gw_adrc-city1.
        gw_show-street = gw_adrc-street.
      ENDIF.
    ENDIF.
    MODIFY gt_show FROM gw_show.
  ENDLOOP.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_SET_FIELD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_set_field .
  append_fieldcat:'OBJECT_ID'             '服务单号'               10   'X',
                  'PROCESS_TYPE'          '单据类型'               4    '',
                  'DESCRIPTION'           '单据描述'               40   '',
                  'POSTING_DATE'          '创建日期'               10   '',
                  'SALES_ORG'             '销售组织'               14   '',
                  'CREATED_BY'            '创建用户'               12   '',
                  'PROVIDER'              '服务商'                 10   'X',
                  'STAT_USER'             '单据状态'               5    '',
                  'ZZZSERIALNO'           '序列号'                 30   '',
                  'ZZZMODENO'             '型号'                   40   '',
                  'ZZFLD00000N'           '服务类型'               2    '',
                  'NAME_ORG1'             '服务商名称1'            40   '',
*                  'NAME_ORG2'             '服务商名称2'            40   '',
                  'COUNTRY'               '国家'                   3    '',
                  'REGION'                '省份'                   3    '',
                  'CITY1'                 '城市'                   40   '',
                  'STREET'                '街道'                   60   ''.

  ls_layout-cwidth_opt    = 'X'. "优化列宽选项是否设置
  ls_layout-zebra                = 'X'.

  gw_event-form = 'DATA_CHANGED_METHOD'.
  gw_event-name = 'DATA_CHANGED'.
  APPEND gw_event TO gt_event.

  gw_grid_setting-edt_cll_cb = 'X'.
ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  FRM_SHOW
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_show .
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'PRM_SET_STATUS'
      i_callback_user_command  = 'PRM_USER_COMMAND'
      is_layout_lvc            = ls_layout
      it_fieldcat_lvc          = gt_fieldcat[]
      i_grid_settings          = gw_grid_setting
*     I_GRID_TITLE             = LV_TITLE
      i_default                = 'X'
      i_save                   = 'A'
      it_events                = gt_event[]
    TABLES
      t_outtab                 = gt_show
    EXCEPTIONS
      program_error            = 1
      OTHERS                   = 2.
  IF sy-subrc <> 0.
  ENDIF.
ENDFORM.
FORM prm_set_status USING rt_extab TYPE slis_t_extab.

  SET PF-STATUS 'ZSTAT'.


  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lr_grid.


ENDFORM.
FORM prm_user_command USING p_ucomm    LIKE sy-ucomm
                        p_selfield TYPE slis_selfield.

  CHECK p_ucomm = '&IC1'.

  READ TABLE gt_show INTO gw_show INDEX p_selfield-tabindex.
  IF sy-subrc = 0.
  ENDIF.
ENDFORM.
FORM data_changed_method
              USING pa_data_changed TYPE REF TO cl_alv_changed_data_protocol .
  DATA:
    l_it_data TYPE         lvc_t_modi,
    l_wa_data LIKE LINE OF l_it_data.
  FIELD-SYMBOLS:<lv_field> TYPE any.

  l_it_data = pa_data_changed->mt_mod_cells.

  CHECK l_it_data IS NOT INITIAL.

  LOOP AT pa_data_changed->mt_mod_cells INTO l_wa_data.
    READ TABLE gt_show INTO gw_show INDEX l_wa_data-row_id.
    IF sy-subrc = 0.
      ASSIGN COMPONENT l_wa_data-fieldname OF STRUCTURE gw_show TO <lv_field>.
      IF sy-subrc = 0.
        <lv_field> = l_wa_data-value.
        MODIFY gt_show FROM gw_show INDEX l_wa_data-row_id.
      ENDIF.
    ENDIF.
  ENDLOOP.

  CALL METHOD lr_grid->refresh_table_display.
ENDFORM.                    " DATA_CHANGED_METHOD

 

标签:gt,show,fieldcat,data,ty,样例,ABAP,ALV,TYPE
From: https://www.cnblogs.com/sapSB/p/17028347.html

相关文章

  • 滚动条设置样例
    ::-webkit-scrollbar{display:block;/*滚动条整体样式*/width:5px;/*高宽分别对应横竖滚动条的尺寸*/height:0;}::-webkit-scrollbar-th......
  • ABAP 常用BAPI
    记录ABAP开发的日常——常用的BAPI前言:在开发的过程中经常用到一些BAPI函数,为了便于查找,于是在这里记录下。如果有广大朋友使用的BAPI在这里没有记录到,欢迎评论补充。SD模......
  • ABAP开发面向对象---类
    今日学习ABAP面向对象里面的类,关于构造,继承,实现。踩坑点:类有抽象的方法,类本身也需要是抽象的,故需要在类申明里面加上ABSTRACT关键字学习资料为B站翱翔云天老师的1CLA......
  • SAP MM 模块的入门者,想学习 ABAP 编程语言应该如何入手?
    本人自2007年计算机专业研究生毕业加入SAP成都研究院,在这之前也从未听说过ABAP这门编程语言,我算是ABAP零基础开始学习。根据我的过往经验,可以先简单了解一下ABAP......
  • 使用 ABAP 代码给 OData 元数据增添注解的一些例子
    ​​使用ABAP代码给OData元数据增添注解的一些例子​​ 最近有读者向我咨询,关于当系统在SAPNetWeaver740之上运行时,如何向OData服务添加注解(annotation)......
  • 如何通过 ABAP 代码给 SAP OData 元数据增添注解试读版
    ​​如何通过ABAP代码给SAPOData元数据增添注解试读版​​ 笔者的这篇SAPUI5开发教程文章,*​​SAPUI5应用开发教程之一百二十九-如何给SAPUI5SmartF......
  • 如何使用 SAP OData 服务向 ABAP 服务器上传文件试读版
    ​​如何使用SAPOData服务向ABAP服务器上传文件试读版​​ 本教程到目前为止开发的OData图书管理服务,可以在ABAP系统里对图书数据进行增删改查。本步骤我们......
  • ABAP 动态生成内表方法一
    在做动态更新程序的时候,要注意两点:1.使用modify(p_tabname)from<dyn_wa>.的时候,一定要确定要更新的字段是不是系统表的关键字段,如果是关键字段的话,需要先删除这条记录,de......
  • 最近的ALV DEMO整理
    申请时请提供:地区-模块-昵称(如BJ-ABAP-XX)所有群管理严格,严格禁止一切外来链接、招聘、广告等垃圾信息!最近一段时间做的ALVdemo1​​ALVdemo 01:cl_salv_table简单输出......
  • ABAP 动态生成内表方法三
    SAP干货铺QQ群及微信群。纯顾问群,禁止发招聘及一切无关链接 广告等,欢迎顾问加入,申请时请提供模块,加入后请看公告,谢谢配合在做动态更新程序的时候,要注意两点:1.使用modify(p......