二维码

[ooalv] 使用oo alv 方式显示‘Total’文字

Twilight发表于 2014-08-26 22:28kenny 最后回复于 2018-04-02 14:20 [复制链接] 1584 2

我们需要用oops方式在ALV底部显示Total text文字,参考如下步骤可实现。
1、在alv输出表中扩展一个字段,为所有记录提供一个计算参考的固定值,他同样作为Total text在alv底部输出
2、在alv field catalog层级上隐藏该字段
3、依据这个字段计算alv排序内表,然后小计输出
4、通过在 Layout structure中传输一个恰当的值,隐藏total line(水平线)

举例:
需要输出的表
Extra Field
Field1
Field2
Field3
Field4
Total:
A
B
10
10
Total:
A
A
10
10
Total:
B
C
10
10
Total:
C
D
10
10

通过扩展字段计算之后,最终在alv中显示的效果
Field1
Field2
Field3
Field4
A
B
10
10
A
A
10
10
B
C
10
10
C
D
10
10
Total:
40
40

程序代码:
  1. * Type declaration for final table to display the output
  2. TYPES: BEGIN OF ty_mara,
  3.         srno TYPE char40, " Storing the total text
  4.         matnr TYPE matnr, " Material
  5.         ersda TYPE ersda, " Creation date
  6.         ernam TYPE ernam, " Created by
  7.         laeda TYPE laeda, " Last change date
  8.         aenam TYPE aenam, " Last change by
  9.         vpsta TYPE vpsta, " Maintenance status
  10.         brgew TYPE brgew, " Gross weight
  11.         ntgew TYPE ntgew, " Net weight
  12.         gewei TYPE gewei, " Weight Unit
  13.        END OF ty_mara.
  14. * Type declaration for table storing temp. data
  15. TYPES: BEGIN OF ty_mara_tmp,
  16.         matnr TYPE matnr, " Material
  17.         ersda TYPE ersda, " Creation date
  18.         ernam TYPE ernam, " Created by
  19.         laeda TYPE laeda, " Last change date
  20.         aenam TYPE aenam, " Last change by
  21.         vpsta TYPE vpsta, " Maintenance status
  22.         brgew TYPE brgew, " Gross weight
  23.         ntgew TYPE ntgew, " Net weight
  24.         gewei TYPE gewei, " Weight Unit
  25.       END OF ty_mara_tmp.
  26. *  Internal table for storing final data
  27. DATA: i_mara TYPE STANDARD TABLE OF ty_mara INITIAL SIZE 0.
  28. * Work area for final table
  29. DATA: w_mara TYPE ty_mara.
  30. *  Internal table for storing temp. data
  31. DATA: i_mara_tmp TYPE STANDARD TABLE OF ty_mara_tmp INITIAL SIZE 0.
  32. * Work area for temp. table
  33. DATA: w_mara_tmp TYPE ty_mara_tmp.
  34. * Object variable for ALV grid
  35. DATA: oref1 TYPE REF TO cl_gui_alv_grid.
  36. * Field catalog table for ALV grid
  37. DATA: fieldcat TYPE  lvc_t_fcat.
  38. * Workarea for field catalog table
  39. DATA: w_field LIKE lvc_s_fcat.
  40. *  Internal table for storing info. for ALV grid
  41. data: i_sort2 TYPE STANDARD TABLE OF lvc_s_sort INITIAL SIZE 0.
  42. * Workarea for sort table
  43. DATA: wa_sort2      TYPE  lvc_s_sort.
  44. * Workarea for ALV layout
  45. data: wa_layout     TYPE  lvc_s_layo.
  46. START-OF-SELECTION.
  47. * Fetch data
  48. SELECT  matnr   " Material
  49.         ersda   " Creation date
  50.         ernam   " Created by
  51.         laeda   " Last change date
  52.         aenam   " Last change by
  53.         vpsta   " Maintenance status
  54.         brgew   " Gross weight
  55.         ntgew   " Net weight
  56.         gewei   " Weight Unit
  57.   FROM mara
  58.   INTO TABLE i_mara_tmp
  59.   UP TO 100 ROWS.
  60.   CHECK sy-subrc = 0.
  61. * Populate final table
  62.   LOOP AT i_mara_tmp INTO w_mara_tmp.
  63. *   Storing the Total text need to be displayed in
  64. *   ALV
  65.     w_mara-srno = 'Total weight (Gross & Net)'.
  66.     w_mara-matnr = w_mara_tmp-matnr.
  67.     w_mara-ersda = w_mara_tmp-ersda.
  68.     w_mara-ernam  = w_mara_tmp-ernam .
  69.     w_mara-laeda = w_mara_tmp-laeda.
  70.     w_mara-aenam = w_mara_tmp-aenam.
  71.     w_mara-vpsta = w_mara_tmp-vpsta.
  72.     w_mara-brgew = w_mara_tmp-brgew.
  73.     w_mara-ntgew = w_mara_tmp-ntgew.
  74.     w_mara-gewei = w_mara_tmp-gewei.
  75.     APPEND w_mara TO i_mara.
  76.   ENDLOOP.
  77. * Calling the screen to display ALV
  78.   CALL SCREEN 100.
  79. *&----------------------------------------------------------------*
  80. *&      Module  STATUS_0100  OUTPUT
  81. *&----------------------------------------------------------------*
  82. *       Display ALV report
  83. *-----------------------------------------------------------------*
  84. MODULE status_0100 OUTPUT.
  85.   IF oref1 IS INITIAL.
  86. *   Create ALV grid object
  87. *   In this case we have not created any custom container in the screen,
  88. *   Instead of that dummy container name is passed
  89. *   ADVANTAGE: we can run this report in background without any problem
  90.     CREATE OBJECT oref1
  91.       EXPORTING
  92.         i_parent          = cl_gui_custom_container=>screen0
  93.       EXCEPTIONS
  94.         error_cntl_create = 1
  95.         error_cntl_init   = 2
  96.         error_cntl_link   = 3
  97.         error_dp_create   = 4
  98.         OTHERS            = 5
  99.         .
  100.     CHECK sy-subrc = 0.
  101. *   Preparing the field catalog
  102. *   ZDEMO: Defined in DDIC, it's structure is same as TYPE ty_mara
  103. *   defined in the program
  104.     CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
  105.       EXPORTING
  106.         i_structure_name       = 'ZDEMO'
  107.       CHANGING
  108.         ct_fieldcat            = fieldcat
  109.       EXCEPTIONS
  110.         inconsistent_interface = 1
  111.         program_error          = 2
  112.         OTHERS                 = 3.
  113.     IF sy-subrc = 0.
  114.       LOOP AT fieldcat INTO w_field.
  115.         IF w_field-fieldname = 'BRGEW' OR
  116.           w_field-fieldname = 'NTGEW'.
  117. *         Summation for Gross & Net weight
  118.           w_field-do_sum = 'X'.
  119.           MODIFY fieldcat FROM w_field TRANSPORTING do_sum.
  120.         ENDIF.
  121.         IF w_field-fieldname = 'SRNO'.
  122. *         Hide this field so that it can display it's content i.e.
  123. *         Total text in Subtotal level
  124.           w_field-tech = 'X'.
  125.           w_field-no_out = 'X'.
  126.           MODIFY fieldcat FROM w_field TRANSPORTING tech no_out.
  127.         ENDIF.
  128.         CLEAR w_field.
  129.       ENDLOOP.
  130.     ENDIF.
  131. *   Populate Sort table with SRNO field so that we can display the total
  132. *   text in it's subtotal level
  133.     wa_sort2-spos = 1.
  134.     wa_sort2-fieldname = 'SRNO'.
  135.     wa_sort2-up = 'X'.
  136.     wa_sort2-subtot = 'X'.
  137.     APPEND wa_sort2 TO i_sort2.
  138. *   Hide the total line
  139.     wa_layout-no_totline = 'X'.
  140. *   Display the ALV grid
  141.     CALL METHOD oref1->set_table_for_first_display
  142.       EXPORTING
  143.         is_layout                     = wa_layout
  144.       CHANGING
  145.         it_outtab                     = i_mara[]
  146.         it_fieldcatalog               = fieldcat
  147.         it_sort                       = i_sort2
  148.       EXCEPTIONS
  149.         invalid_parameter_combination = 1
  150.         program_error                 = 2
  151.         too_many_lines                = 3
  152.         OTHERS                        = 4.
  153.     IF sy-subrc <> 0.
  154.     ENDIF.
  155. *   Set the focus on the grid
  156.     CALL METHOD cl_gui_alv_grid=>set_focus
  157.       EXPORTING
  158.         control           = oref1
  159.       EXCEPTIONS
  160.         cntl_error        = 1
  161.         cntl_system_error = 2
  162.         OTHERS            = 3.
  163.     IF sy-subrc <> 0.
  164.     ENDIF.
  165.   ENDIF.
  166. ENDMODULE.                 " STATUS_0100  OUTPUT
复制代码

PS:创建screen 100,不需要画customer container
        将注释的逻辑流语句释放
        创建一个结构,效果如下:
ZDEMO.jpg
程序执行效果:
Display text 'Total' using OO-ALV.jpg
回复

使用道具 举报

zhongguomao
这个有点意思
回复 支持 反对

使用道具 举报

kenny
我的系统是中文的话,新增的一行为什么会出现额外的中文呢?
回复 支持 反对

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表