二维码

[ooalv] 在alv用class方式添加PF-Status, Header and Footer

Twilight发表于 2014-08-26 19:32Twilight 最后回复于 2014-08-26 19:32 [复制链接] 4319 0

以前我们开发ALV是用slis类型池 或者Cl_GUI_ALV_GRID的class方式,在这篇帖子里我们使用CL_SALV 类的方式实现

程序代码:
  1. *----------------------------------------------------------------------*
  2. *       CLASS lcl_report DEFINITION
  3. *----------------------------------------------------------------------*
  4. *
  5. *----------------------------------------------------------------------*
  6. CLASS LCL_REPORT DEFINITION.
  7.   PUBLIC SECTION.
  8. *----------------------------------------------------------------------*
  9. * Final Output Table
  10. *----------------------------------------------------------------------*
  11.     TYPES: BEGIN OF TY_MARA,
  12.             MATNR TYPE MATNR,
  13.             ERSDA TYPE ERSDA,
  14.             MATKL TYPE MATKL,
  15.             MTART TYPE MTART,
  16.             LVORM TYPE LVORM,
  17.            END OF TY_MARA.
  18.     DATA: O_ALV TYPE REF TO CL_SALV_TABLE,              " ALV Reference
  19.           T_MARA TYPE STANDARD TABLE OF TY_MARA.
  20. *----------------------------------------------------------------------*
  21. * Methods to Fetch Data and Generate Output
  22. *----------------------------------------------------------------------*
  23.     METHODS: GET_DATA,                                  "Data Selection
  24.              GENERATE_OUTPUT.                           "Generating Output
  25.   PRIVATE SECTION.
  26. *----------------------------------------------------------------------*
  27. * Methods to Set PF-Status, Header and Footer
  28. *----------------------------------------------------------------------*
  29.     METHODS: SET_PF_STATUS
  30.                   CHANGING
  31.                       CO_ALV TYPE REF TO CL_SALV_TABLE, " Default Pf Status
  32.              SET_TOP_OF_PAGE
  33.                   CHANGING
  34.                       CO_ALV TYPE REF TO CL_SALV_TABLE, " Set Top of page
  35.              SET_END_OF_PAGE
  36.                   CHANGING
  37.                       CO_ALV TYPE REF TO CL_SALV_TABLE. " Set End of page
  38. ENDCLASS.                    "lcl_report DEFINITION
  39. *----------------------------------------------------------------------*
  40. *       CLASS lcl_report IMPLEMENTATION
  41. *----------------------------------------------------------------------*
  42. *
  43. *----------------------------------------------------------------------*
  44. CLASS LCL_REPORT IMPLEMENTATION.
  45. *----------------------------------------------------------------------*
  46. * Data selection
  47. *----------------------------------------------------------------------*
  48.   METHOD GET_DATA.
  49.     SELECT MATNR ERSDA MATKL MTART LVORM INTO TABLE T_MARA
  50.            FROM MARA UP TO 20 ROWS.
  51.   ENDMETHOD.                    "get_data
  52. *----------------------------------------------------------------------*
  53. * Generating Output
  54. *----------------------------------------------------------------------*
  55.   METHOD GENERATE_OUTPUT.
  56. *Exception Class
  57.     DATA: LC_MSG TYPE REF TO CX_SALV_MSG.
  58. *----------------------------------------------------------------------*
  59. * We are calling the static Factory method which will give back
  60. * the ALV object reference.
  61. *----------------------------------------------------------------------*
  62.     TRY.
  63.         CALL METHOD CL_SALV_TABLE=>FACTORY
  64.           IMPORTING
  65.             R_SALV_TABLE = O_ALV
  66.           CHANGING
  67.             T_TABLE      = T_MARA.
  68.       CATCH CX_SALV_MSG INTO LC_MSG .
  69.     ENDTRY.
  70. ************************************************************************
  71. * In this area we will call the methods which will set the
  72. * different properties to the ALV
  73. ************************************************************************
  74. * Calling Set PF status method
  75.     CALL METHOD SET_PF_STATUS
  76.       CHANGING
  77.         CO_ALV = O_ALV.       "set_end_of_page
  78. * Calling the top of page method
  79.     CALL METHOD SET_TOP_OF_PAGE
  80.       CHANGING
  81.         CO_ALV = O_ALV.
  82. * Calling the End of page method
  83.     CALL METHOD SET_END_OF_PAGE
  84.       CHANGING
  85.         CO_ALV = O_ALV.
  86. ************************************************************************
  87. * Displaying the ALV
  88. * Here we will call the DISPLAY method to get the output on the screen
  89. ************************************************************************
  90.     O_ALV->DISPLAY( ).
  91.   ENDMETHOD.                    "generate_output
  92. ************************************************************************
  93. *    In this area we will implement the methods which are defined in
  94. *    the class definition
  95. ************************************************************************
  96. * Setting Default PF-Status
  97.   METHOD SET_PF_STATUS.
  98.     DATA: LO_FUNCTIONS TYPE REF TO CL_SALV_FUNCTIONS_LIST.
  99. * Default functions
  100.     LO_FUNCTIONS = CO_ALV->GET_FUNCTIONS( ).
  101.     LO_FUNCTIONS->SET_DEFAULT( ABAP_TRUE ).
  102.   ENDMETHOD.                    "set_pf_status

  103. * Setting Top_of_page
  104.   METHOD SET_TOP_OF_PAGE.
  105.     DATA: LO_HEADER TYPE REF TO CL_SALV_FORM_LAYOUT_GRID,
  106.           LO_H_LABEL TYPE REF TO CL_SALV_FORM_LABEL,
  107.           LO_H_FLOW  TYPE REF TO CL_SALV_FORM_LAYOUT_FLOW.
  108. * Header object
  109.     CREATE OBJECT LO_HEADER.
  110. *----------------------------------------------------------------------*
  111. * To create a Label or Flow we have to specify the target
  112. * row and column number where we need to set up the output
  113. * text.
  114. *----------------------------------------------------------------------*
  115. * Information in Bold
  116.     LO_H_LABEL = LO_HEADER->CREATE_LABEL( ROW = 1 COLUMN = 1 ).
  117.     LO_H_LABEL->SET_TEXT('Header of the ALV Output in Bold').
  118. * Information in tabular format
  119.     LO_H_FLOW = LO_HEADER->CREATE_FLOW( ROW = 2 COLUMN = 1 ).
  120.     LO_H_FLOW->CREATE_TEXT( TEXT = 'This is text of flow in Header' ).
  121.     LO_H_FLOW = LO_HEADER->CREATE_FLOW( ROW = 3 COLUMN = 1 ).
  122.     LO_H_FLOW->CREATE_TEXT( TEXT = 'Date of List Generation' ).
  123.     LO_H_FLOW = LO_HEADER->CREATE_FLOW( ROW = 3 COLUMN = 2 ).
  124.     LO_H_FLOW->CREATE_TEXT( TEXT = sy-datum ).
  125. * Set the top of list using the header for Online
  126.     CO_ALV->SET_TOP_OF_LIST( LO_HEADER ).
  127. * Set the top of list using the header for Print
  128.     CO_ALV->SET_TOP_OF_LIST_PRINT( LO_HEADER ).
  129.   ENDMETHOD.                    "set_top_of_page

  130. * Setting End_Of_page
  131.   METHOD SET_END_OF_PAGE.
  132.     DATA: LO_FOOTER  TYPE REF TO CL_SALV_FORM_LAYOUT_GRID,
  133.           LO_F_LABEL TYPE REF TO CL_SALV_FORM_LABEL,
  134.           LO_F_FLOW  TYPE REF TO CL_SALV_FORM_LAYOUT_FLOW.
  135. * Footer Object
  136.     CREATE OBJECT LO_FOOTER.
  137. * Information in Bold
  138.     LO_F_LABEL = LO_FOOTER->CREATE_LABEL( ROW = 1 COLUMN = 1 ).
  139.     LO_F_LABEL->SET_TEXT('Footer of the ALV in Bold').
  140. * Tabular Information
  141.     LO_F_FLOW = LO_FOOTER->CREATE_FLOW( ROW = 2 COLUMN = 1 ).
  142.     LO_F_FLOW->CREATE_TEXT( TEXT = 'This is text of flow in footer' ).

  143. * Set the end of list using the header for Online
  144.     CO_ALV->SET_END_OF_LIST( LO_FOOTER ).
  145. * Set the End of list using the header for Print
  146.     CO_ALV->SET_END_OF_LIST_PRINT( LO_FOOTER ).
  147.   ENDMETHOD.                    "set_end_of_page
  148. ENDCLASS.                    "lcl_report IMPLEMENTATION
  149. *----------------------------------------------------------------------*
  150. START-OF-SELECTION.
  151. *----------------------------------------------------------------------*
  152.   DATA: LO_REPORT TYPE REF TO LCL_REPORT.
  153.   CREATE OBJECT LO_REPORT.
  154.   LO_REPORT->GET_DATA( ).
  155.   LO_REPORT->GENERATE_OUTPUT( ).
复制代码

程序执行效果:
header and footer using class.png
回复

使用道具 举报

快速回帖

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

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