SAP ABAP在alv grid中使用subtotal小计数据

小计需要配置的地方:

1、fieldcate中指定需要do_sum合计的字段
2、sort中指定合计依据的需排序字段
3、event事件回调subtotal_text子过程,显示文本

程序代码:

*&---------------------------------------------------------------------*

[plain]  view plain  copy
  1. *& Table declaration  
  2. *&---------------------------------------------------------------------*  
  3. TABLES: ekko.  
  4. *&---------------------------------------------------------------------*  
  5. *& Type pool declaration  
  6. *&---------------------------------------------------------------------*  
  7. TYPE-POOLS: slis. " Type pool for ALV  
  8. *&---------------------------------------------------------------------*  
  9. *& Selection screen  
  10. *&---------------------------------------------------------------------*  
  11. SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.  
  12. *&---------------------------------------------------------------------*  
  13. *& Type declaration  
  14. *&---------------------------------------------------------------------*  
  15. * Type declaration for internal table to store EKPO data  
  16. TYPES: BEGIN OF x_data,  
  17.        ebeln  TYPE char30,  " Document no.  
  18.        ebelp  TYPE ebelp,   " Item no  
  19.        matnr  TYPE matnr,   " Material no  
  20.        matnr1 TYPE matnr,   " Material no  
  21.        werks  TYPE werks_d, " Plant  
  22.        werks1 TYPE werks_d, " Plant  
  23.        ntgew  TYPE entge,   " Net weight  
  24.        gewe   TYPE egewe,   " Unit of weight                               
  25.        END OF x_data.  
  26. *&---------------------------------------------------------------------*  
  27. *& Internal table declaration  
  28. *&---------------------------------------------------------------------*  
  29. DATA:  
  30. * Internal table to store EKPO data  
  31.   i_ekpo TYPE STANDARD TABLE OF x_data INITIAL SIZE 0,  
  32. * Internal table for storing field catalog information  
  33.   i_fieldcat TYPE slis_t_fieldcat_alv,  
  34. * Internal table for Top of Page info. in ALV Display  
  35.   i_alv_top_of_page TYPE slis_t_listheader,  
  36. * Internal table for ALV Display events  
  37.   i_events TYPE slis_t_event,  
  38. * Internal table for storing ALV sort information  
  39.   i_sort TYPE  slis_t_sortinfo_alv,  
  40.   i_event TYPE slis_t_event.  
  41. *&---------------------------------------------------------------------*  
  42. *& Work area declaration  
  43. *&---------------------------------------------------------------------*  
  44. DATA:  
  45.   wa_ekko TYPE x_data,  
  46.   wa_layout     TYPE slis_layout_alv,  
  47.   wa_events         TYPE slis_alv_event,  
  48.   wa_sort TYPE slis_sortinfo_alv.  
  49. *&---------------------------------------------------------------------*  
  50. *& Constant declaration  
  51. *&---------------------------------------------------------------------*  
  52. CONSTANTS:  
  53.    c_header   TYPE char1  
  54.               VALUE 'H',                    "Header in ALV  
  55.    c_item     TYPE char1  
  56.               VALUE 'S'.  
  57. *&---------------------------------------------------------------------*  
  58. *& Start-of-selection event  
  59. *&---------------------------------------------------------------------*  
  60. START-OF-SELECTION.  
  61. * Select data from ekpo  
  62.   SELECT ebeln " Doc no  
  63.          ebelp " Item  
  64.          matnr " Material  
  65.          matnr " Material  
  66.          werks " Plant  
  67.          werks " Plant  
  68.          ntgew " Quantity  
  69.          gewei " Unit  
  70.          FROM ekpo  
  71.          INTO TABLE i_ekpo  
  72.          WHERE ebeln IN s_ebeln  
  73.          AND ntgew NE '0.00'.  
  74.   IF sy-subrc = 0.  
  75.     SORT i_ekpo BY ebeln ebelp matnr .  
  76.   ENDIF.  
  77. * To build the Page header  
  78.   PERFORM sub_build_header.  
  79. * To prepare field catalog  
  80.   PERFORM sub_field_catalog.  
  81. * Perform to populate the layout structure  
  82.   PERFORM sub_populate_layout.  
  83. * Perform to populate the sort table.  
  84.   PERFORM sub_populate_sort.  
  85. * Perform to populate ALV event  
  86.   PERFORM sub_get_event.  
  87. END-OF-SELECTION.  
  88. * Perform to display ALV report  
  89.   PERFORM sub_alv_report_display.  
  90.   
  91. *&---------------------------------------------------------------------*  
  92. *&      Form  sub_build_header  
  93. *&---------------------------------------------------------------------*  
  94. *       To build the header  
  95. *----------------------------------------------------------------------*  
  96. *       No Parameter  
  97. *----------------------------------------------------------------------*  
  98. FORM sub_build_header .  
  99. * Local data declaration  
  100.   DATA: l_system     TYPE char10 ,          "System id  
  101.         l_r_line     TYPE slis_listheader,  "Hold list header  
  102.         l_date       TYPE char10,           "Date  
  103.         l_time       TYPE char10,           "Time  
  104.         l_success_records TYPE i,           "No of success records  
  105.         l_title(300) TYPE c.                " Title  
  106.   
  107. * Title  Display  
  108.   l_r_line-typ = c_header.               " header  
  109.   l_title = 'Test report'(001).  
  110.   l_r_line-info = l_title.  
  111.   APPEND l_r_line TO i_alv_top_of_page.  
  112.   CLEAR l_r_line.  
  113. * Run date Display  
  114.   CLEAR l_date.  
  115.   l_r_line-typ  = c_item.                " Item  
  116.   WRITE: sy-datum  TO l_date MM/DD/YYYY.  
  117.   l_r_line-key = 'Run Date :'(002).  
  118.   l_r_line-info = l_date.  
  119.   APPEND l_r_line TO i_alv_top_of_page.  
  120.   CLEAR: l_r_line,  
  121.          l_date.  
  122. ENDFORM.                    " sub_build_header  
  123.   
  124. *&---------------------------------------------------------------------*  
  125. *&      Form  sub_field_catalog  
  126. *&---------------------------------------------------------------------*  
  127. *       Build Field Catalog  
  128. *----------------------------------------------------------------------*  
  129. *       No Parameter  
  130. *----------------------------------------------------------------------*  
  131. FORM sub_field_catalog .  
  132. *  Build Field Catalog  
  133.   PERFORM sub_fill_alv_field_catalog USING:  
  134.      '01' '01' 'EBELN' 'I_EKPO' 'L'  
  135.      'Doc No'(003) ' ' ' ' ' ' ' ',  
  136.      '01' '02' 'EBELP' 'I_EKPO' 'L'  
  137.      'Item No'(004) 'X' 'X' ' ' ' ',  
  138.      '01' '03' 'MATNR' 'I_EKPO' 'L'  
  139.      'Material No'(005) 'X' 'X' ' ' ' ',  
  140.      '01' '03' 'MATNR1' 'I_EKPO' 'L'  
  141.      'Material No'(005) ' ' ' ' ' ' ' ',  
  142.   
  143.      '01' '04' 'WERKS' 'I_EKPO' 'L'  
  144.      'Plant'(006) 'X' 'X' ' ' ' ',  
  145.      '01' '04' 'WERKS1' 'I_EKPO' 'L'  
  146.      'Plant'(006) ' ' ' ' ' ' ' ',  
  147.      '01' '05' 'NTGEW' 'I_EKPO' 'R'  
  148.      'Net Weight'(007) ' ' ' ' 'GEWE' 'I_EKPO'.  
  149. ENDFORM.                    " sub_field_catalog  
  150. *&---------------------------------------------------------------------*  
  151. *&     Form  sub_fill_alv_field_catalog  
  152. *&---------------------------------------------------------------------*  
  153. *&     For building Field Catalog  
  154. *&---------------------------------------------------------------------*  
  155. *&     p_rowpos   Row position  
  156. *&     p_colpos   Col position  
  157. *&     p_fldnam   Fldname  
  158. *&     p_tabnam   Tabname  
  159. *&     p_justif   Justification  
  160. *&     p_seltext  Seltext  
  161. *&     p_out      no out  
  162. *&     p_tech     Technical field  
  163. *&     p_qfield   Quantity field  
  164. *&     p_qtab     Quantity table  
  165. *&---------------------------------------------------------------------*  
  166. FORM sub_fill_alv_field_catalog  USING  p_rowpos    TYPE sycurow  
  167.                                         p_colpos    TYPE sycucol  
  168.                                         p_fldnam    TYPE fieldname  
  169.                                         p_tabnam    TYPE tabname  
  170.                                         p_justif    TYPE char1  
  171.                                         p_seltext   TYPE dd03p-scrtext_l  
  172.                                         p_out       TYPE char1  
  173.                                         p_tech      TYPE char1  
  174.                                         p_qfield    TYPE slis_fieldname  
  175.                                         p_qtab      TYPE slis_tabname.  
  176. * Local declaration for field catalog  
  177.   DATA: wa_lfl_fcat    TYPE  slis_fieldcat_alv.  
  178.   wa_lfl_fcat-row_pos        =  p_rowpos.     "Row  
  179.   wa_lfl_fcat-col_pos        =  p_colpos.     "Column  
  180.   wa_lfl_fcat-fieldname      =  p_fldnam.     "Field Name  
  181.   wa_lfl_fcat-tabname        =  p_tabnam.     "Internal Table Name  
  182.   wa_lfl_fcat-just           =  p_justif.     "Screen Justified  
  183.   wa_lfl_fcat-seltext_l      =  p_seltext.    "Field Text  
  184.   wa_lfl_fcat-no_out         =  p_out.        "No output  
  185.   wa_lfl_fcat-tech           =  p_tech.       "Technical field  
  186.   wa_lfl_fcat-qfieldname     =  p_qfield.     "Quantity unit  
  187.   wa_lfl_fcat-qtabname       =  p_qtab .      "Quantity table  
  188.   IF p_fldnam = 'NTGEW'.  
  189.     wa_lfl_fcat-do_sum  = 'X'.  
  190.   ENDIF.  
  191.   APPEND wa_lfl_fcat TO i_fieldcat.  
  192.   CLEAR wa_lfl_fcat.  
  193. ENDFORM.                    " sub_fill_alv_field_catalog  
  194. *&---------------------------------------------------------------------*  
  195. *&      Form  sub_populate_layout  
  196. *&---------------------------------------------------------------------*  
  197. *       Populate ALV layout  
  198. *----------------------------------------------------------------------*  
  199. *       No Parameter  
  200. *----------------------------------------------------------------------*  
  201. FORM sub_populate_layout .  
  202.   CLEAR wa_layout.  
  203.   wa_layout-colwidth_optimize = 'X'." Optimization of Col width  
  204. ENDFORM.                    " sub_populate_layout  
  205. *&---------------------------------------------------------------------*  
  206. *&      Form  sub_populate_sort  
  207. *&---------------------------------------------------------------------*  
  208. *       Populate ALV sort table  
  209. *----------------------------------------------------------------------*  
  210. *       No Parameter  
  211. *----------------------------------------------------------------------*  
  212. FORM sub_populate_sort .  
  213. * Sort on material  
  214.   wa_sort-spos = '01' .  
  215.   wa_sort-fieldname = 'MATNR'.  
  216.   wa_sort-tabname = 'I_EKPO'.  
  217.   wa_sort-up = 'X'.  
  218.   wa_sort-subtot = 'X'.  
  219.   APPEND wa_sort TO i_sort .  
  220.   CLEAR wa_sort.  
  221. * Sort on plant  
  222.   wa_sort-spos = '02'.  
  223.   wa_sort-fieldname = 'WERKS'.  
  224.   wa_sort-tabname = 'I_EKPO'.  
  225.   wa_sort-up = 'X'.  
  226.   wa_sort-subtot = 'X'.  
  227.   APPEND wa_sort TO i_sort .  
  228.   CLEAR wa_sort.  
  229.   
  230. ENDFORM.                    " sub_populate_sort  
  231. *&---------------------------------------------------------------------*  
  232. *&      Form  sub_get_event  
  233. *&---------------------------------------------------------------------*  
  234. *       Get ALV grid event and pass the form name to subtotal_text  
  235. *       event  
  236. *----------------------------------------------------------------------*  
  237. *       No Parameter  
  238. *----------------------------------------------------------------------*  
  239. FORM sub_get_event .  
  240.   CONSTANTS : c_formname_subtotal_text TYPE slis_formname VALUE  
  241. 'SUBTOTAL_TEXT'.  
  242.   DATA: l_s_event TYPE slis_alv_event.  
  243.   
  244.   CALL FUNCTION 'REUSE_ALV_EVENTS_GET'  
  245.     EXPORTING  
  246.       i_list_type     = 4  
  247.     IMPORTING  
  248.       et_events       = i_event  
  249.     EXCEPTIONS  
  250.       list_type_wrong = 0  
  251.       OTHERS          = 0.  
  252. * Subtotal  
  253.   READ TABLE i_event  INTO l_s_event  
  254.                     WITH KEY name = slis_ev_subtotal_text."在类型池中默认的值为SUBTOTAL_TEXT  
  255.   IF sy-subrc = 0.  
  256.     MOVE c_formname_subtotal_text TO l_s_event-form.  
  257.     MODIFY i_event FROM l_s_event INDEX sy-tabix.  
  258.   ENDIF.  
  259. ENDFORM.                    " sub_get_event  
  260. *&---------------------------------------------------------------------*  
  261. *&      Form  sub_alv_report_display  
  262. *&---------------------------------------------------------------------*  
  263. *       For ALV Report Display  
  264. *----------------------------------------------------------------------*  
  265. *       No Parameter  
  266. *----------------------------------------------------------------------*  
  267. FORM sub_alv_report_display .  
  268.   DATA: l_repid TYPE syrepid .  
  269.   l_repid = sy-repid .  
  270. * This function module for displaying the ALV report  
  271.   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'  
  272.     EXPORTING  
  273.       i_callback_program       = l_repid  
  274.       i_callback_top_of_page   = 'SUB_ALV_TOP_OF_PAGE'  
  275.       is_layout                = wa_layout  
  276.       it_fieldcat              = i_fieldcat  
  277.       it_sort = i_sort  
  278.       it_events                = i_event  
  279.       i_default                = 'X'  
  280.       i_save                   = 'A'  
  281.     TABLES  
  282.       t_outtab                 = i_ekpo  
  283.     EXCEPTIONS  
  284.       program_error            = 1  
  285.       OTHERS                   = 2.  
  286.   IF sy-subrc <> 0.  
  287. *    MESSAGE i000 WITH 'Error in ALV report display'(055).  
  288.   ENDIF.  
  289. ENDFORM.                    " sub_alv_report_display  
  290. *&---------------------------------------------------------------------*  
  291. *       FORM sub_alv_top_of_page  
  292. *---------------------------------------------------------------------*  
  293. *       Call ALV top of page  
  294. *---------------------------------------------------------------------*  
  295. *       No parameter  
  296. *---------------------------------------------------------------------*  
  297. FORM sub_alv_top_of_page.                                   "#EC CALLED  
  298. * To write header for the ALV  
  299.   CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'  
  300.     EXPORTING  
  301.       it_list_commentary = i_alv_top_of_page.  
  302.   
  303. ENDFORM.                    "alv_top_of_page  
  304. *&---------------------------------------------------------------------*  
  305. *&      Form  subtotal_text  
  306. *&---------------------------------------------------------------------*  
  307. *       Build subtotal text  
  308. *----------------------------------------------------------------------*  
  309. *       P_total  Total  
  310. *       p_subtot_text Subtotal text info  
  311. *----------------------------------------------------------------------*  
  312. FORM subtotal_text CHANGING  
  313.                p_total TYPE any  
  314.                p_subtot_text TYPE slis_subtot_text.  
  315.   
  316. * Material level sub total  
  317.   IF p_subtot_text-criteria = 'MATNR'.  
  318.     p_subtot_text-display_text_for_subtotal  
  319.     = 'Material level total'(009).  
  320.   ENDIF.  
  321. * Plant level sub total  
  322.   IF p_subtot_text-criteria = 'WERKS'.  
  323.     p_subtot_text-display_text_for_subtotal = 'Plant level total'(010).  
  324.   ENDIF.  
  325.   
  326. ENDFORM.                    "subtotal_text  
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ChampaignWolf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值