二维码

[fmalv] 在alv list底部打印Subtotals

Twilight发表于 2014-07-25 09:56Twilight 最后回复于 2014-07-25 09:56 [复制链接] 4192 0

介绍:
正常的ALV list function,subtotal在每类item后面小计然后在报表的结尾处合计,但是如果我们希望将小计和合计都放在alv报表的结尾,但是标准的alv function不提供这样的功能。我们可以使用自定义event中的form来解决此问题。

Normal Functionality (provided for in the standard ALV)  
Item Heading
Item 1.1              
Item 1.2              
Item 1.3              
Subtotal 1              
Item 2.1              
Item 2.2              
Subtotal 2              
Total

Special Requirement (can be achieved using this tip)
Item Heading              
Item 1.1              
Item 1.2        
Item 1.3              
Item 2.1              
Item 2.2              
Subtotal 1 (Success)              
Subtotal 2 (Error)


Detail Steps:   

Step 1
定义类型为SLIS_T_EVENT的内表和工作区。
  1. DATA:        t_event TYPE slis_t_event,
  2.         w_event TYPE slis_alv_event.
复制代码

Step 2
把 AFTER-LINE-OUTPUT 事件Append 到T_EVENT内表中
  1. CLEAR w_event.
  2. w_event-form = slis_ev_after_line_output.
  3. w_event-name = slis_ev_after_line_output."AFTER_LINE_OUTPUT event
  4. APPEND w_event TO t_event.
复制代码

Step 3
这个subtatal中AFTER_LINE_OUTPUT子过程中计算和用WRITE 、 FORMAT 格式展示
  1. *&---------------------------------------------------------------------*
  2. *&      Form  AFTER_LINE_OUTPUT
  3. *&---------------------------------------------------------------------*
  4. *       text
  5. *----------------------------------------------------------------------*
  6. *      -->P_RS_LINEINFO  text
  7. *----------------------------------------------------------------------*
  8. FORM after_line_output
  9.   USING p_rs_lineinfo TYPE slis_lineinfo.
  10. * Declaration of local variables
  11.   DATA: l_success TYPE wrbtr,     "Total For Successful Entries
  12.         l_error   TYPE wrbtr,     "Total For Unsuccessful Entries
  13.         l_count   TYPE i.         "No. Of lines in table T_OUTPUT
  14. * Getting No. of Lines in the table T_OUTPUT
  15.   DESCRIBE TABLE t_output LINES l_count.
  16. * Displaying the totals after the last record of the internal
  17. * table T_OUTPUT
  18.   IF p_rs_lineinfo-tabindex = l_count.
  19. *   Loop At the internal table T_OUTPUT
  20.     LOOP AT t_output INTO w_output.
  21.       IF w_output-sflag = c_checked.
  22.         "Flag: Indicates error record
  23. *       Calculate total for unsuccessful entries
  24.         l_error = l_error + w_output-wrbtr.
  25.       ELSE.
  26. *       Calculate total for successful entries
  27.         l_success = l_success + w_output-wrbtr.
  28.       ENDIF.
  29. *     Clear workarea W_OUTPUT
  30.       CLEAR w_output.
  31.     ENDLOOP.
  32. *   Set format for the total line display
  33.     ULINE AT (p_rs_lineinfo-linsz).        "Dynamic Line Size
  34.     FORMAT INTENSIFIED COLOR COL_TOTAL ON. "Setting the color
  35.                                            "For the total row
  36.                                            "As Yellow
  37.     WRITE : /    sy-vline,                 "Vertical Line
  38.                  text-017,                 "Caption For Total
  39.                                            "Sum of Successful
  40.                                            "Entries               
  41.               33 l_success,                "Total Of Successful
  42.                                            "Entries
  43.                  c_usd.                    "Currency Type USD
  44.     POSITION     p_rs_lineinfo-linsz.      "Dynamic Line Size
  45.     WRITE :      sy-vline.                 "Vertical Line
  46.     ULINE AT     (p_rs_lineinfo-linsz).    "Dynamic Line Size
  47.     WRITE : /    sy-vline ,                "Vertical Line
  48.                  text-018,                 "Caption For Total
  49.                                            "Sum of Successful
  50.                                            "Entries
  51.               33 l_error,                  "Total Of Unsuccessful
  52.                                            "Entries
  53.                  c_usd.                    "Currency Type USD
  54.     POSITION     p_rs_lineinfo-linsz.      "Dynamic Line Size
  55.     WRITE :      sy-vline.                 "Vertical Line
  56.     FORMAT COLOR OFF.                      "Color Setting Off
  57.   ENDIF.
  58. ENDFORM.                                   "AFTER_LINE_OUTPUT
复制代码

Step 4
将事件T_EVENT内表关联到 function 'REUSE_ALV_LIST_DISPLAY' 中,然后display report
  1. CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
  2.   EXPORTING
  3.     i_callback_program = l_repid    "Program Name
  4.     is_layout          = w_layout   "Layout of the Report
  5.     it_fieldcat        = t_fieldcat "Field Catalog for Report
  6.     it_events          = t_event    "For setting the events
  7.   TABLES
  8.     t_outtab           = t_output   "Report data Internal Table
  9.   EXCEPTIONS
  10.     program_error      = 1
  11.     OTHERS             = 2.
复制代码

Result:
‘Sum of Successful Entries’ 行用绿色表示,
‘Sum of Unsuccessful Entries’行用红色表示。
subtotal.jpg
局限性:
1、subtotals不能用normal alv function计算展示,不得不手工计算
2、如果我们下载这个alv list,那么 subtotals不能被下载
回复

使用道具 举报

快速回帖

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

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