二维码

[ooalv] 编辑alv需要输出的fieldcatalog然后alv输出这些字段

Twilight发表于 2014-08-18 16:13Twilight 最后回复于 2014-08-18 16:13 [复制链接] 3330 0

需要创建screen 600、定义工具栏、逻辑流和container容器等,程序代码里有英文说明

程序代码:
  1. *&---------------------------------------------------------------------*
  2. *& AS : ALV report which displays the contents of the table T006
  3. *& (as a docking container in the bottom) along with the
  4. *& editable ALV which contains the ALV fieldcatalogue table structure.
  5. *& With the available toolbar options of the editable ALV in the output,
  6. *& user can change the fieldcatalogue as per his requirement.
  7. *& When the user clicks 'SUBMIT',the display of the ALV with table T006
  8. *& gets modified and customised accordingly to the user's requirement.
  9. *&---------------------------------------------------------------------*
  10. REPORT  ydamon_042.
  11. * Output table T006 structure declaration
  12. TYPES : BEGIN OF ty_t006.
  13.         INCLUDE STRUCTURE t006.
  14. TYPES : END OF ty_t006.
  15. *Internal table and wa declaration for T006
  16. DATA : it_t006 TYPE STANDARD TABLE OF ty_t006,
  17.        wa_t006 TYPE ty_t006.
  18. *declarations for ALV
  19. DATA: ok_code               TYPE sy-ucomm,
  20. * fieldcatalog for T006
  21.       it_fielcat           TYPE lvc_t_fcat,
  22. * fieldcatalog for fieldcatalog itself:
  23.       it_fielcatalogue           TYPE lvc_t_fcat,
  24.       it_layout           TYPE lvc_s_layo.
  25. *declaration for toolbar function
  26. DATA:   it_excl_func        TYPE ui_functions.

  27. * Controls to display it_t006 and corresponding fieldcatalog
  28. DATA: cont_dock TYPE REF TO cl_gui_docking_container,
  29.       cont_alvgd     TYPE REF TO cl_gui_alv_grid.
  30. *controls to display the fieldcatalog as editable alv grid and container
  31. DATA: cont_cust TYPE REF TO cl_gui_custom_container,
  32.       cont_editalvgd     TYPE REF TO cl_gui_alv_grid.
  33. *intialization event
  34. INITIALIZATION.
  35. *start of selection event
  36. START-OF-SELECTION.

  37. **************************************************************
  38. * LOCAL CLASS Definition for data changed in fieldcatalog ALV
  39. **************************************************************
  40. CLASS lcl_event_receiver DEFINITION.
  41.   PUBLIC SECTION.
  42.     METHODS handle_data_changed
  43.       FOR EVENT data_changed OF cl_gui_alv_grid
  44.       IMPORTING er_data_changed.
  45. ENDCLASS.                    "lcl_event_receiver DEFINITION
  46. **************************************************************
  47. * LOCAL CLASS implementation for data changed in fieldcatalog ALV
  48. **************************************************************
  49. CLASS lcl_event_receiver IMPLEMENTATION.
  50.   METHOD handle_data_changed.
  51.   ENDMETHOD.                    "handle_data_changed
  52. ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION
  53. *data declaration for event receiver
  54. DATA: event_receiver TYPE REF TO lcl_event_receiver.
  55. *end of selection event
  56. END-OF-SELECTION.
  57. *setting the screen for alv output for table display and
  58. *changed fieldcatalalogue display
  59.   SET SCREEN 600.

  60. *On this statement double click  it takes you to the screen painter SE51.
  61. * Enter the attributes
  62. *Create a Custom container and name it CCONT and OK code as OK_CODE.
  63. *Save check and Activate the screen painter.
  64. *Now a normal screen with number 600 is created which holds the ALV grid.
  65. * PBO of the actual screen , Here we can give a title and customized menus
  66. *Go to SE41 and create status 'STATUS600' and create THE function code 'SUBMIT'
  67. *and 'EXIT' with icons and icon texts
  68. *Also create a TitleBar 'TITLE600' and give the relevant title.
  69. *&---------------------------------------------------------------------*
  70. *&      Module  STATUS_0600  OUTPUT
  71. *&---------------------------------------------------------------------*
  72. MODULE status_0600 OUTPUT.
  73.   SET PF-STATUS 'STATUS600'.
  74.   SET TITLEBAR 'TITLE600'.
  75. * CREATE ALV GRID CONTROL IF DOES NOT EXISTS INITIALLY
  76.   IF cont_dock IS INITIAL.
  77.     PERFORM create_alv.
  78.   ENDIF.
  79. ENDMODULE.                             " STATUS_0600  OUTPUT
  80. * PAI module of the screen created. In case we use an interactive ALV or
  81. *for additional functionalities we can create OK codes and based on the
  82. *user command we can do the coding as shown below
  83. *&---------------------------------------------------------------------*
  84. *&      Module  USER_COMMAND_0600  INPUT
  85. *&---------------------------------------------------------------------*
  86. MODULE user_command_0600 INPUT.
  87.   CASE ok_code.
  88.     WHEN 'SUBMIT'.
  89. *TO GET THE CURRENT FIELDCATALOGUE FROM THE FRONTEND
  90.       CALL METHOD cont_alvgd->set_frontend_fieldcatalog
  91.         EXPORTING
  92.           it_fieldcatalog = it_fielcat.
  93. *refresh the alv
  94.       CALL METHOD cont_alvgd->refresh_table_display.
  95. *to Send Buffered Automation Queue to Frontend
  96.       CALL METHOD cl_gui_cfw=>flush.
  97. *Exit button clicked to leave the program
  98.     WHEN 'EXIT'.
  99.       LEAVE PROGRAM.
  100.   ENDCASE.
  101. ENDMODULE.                             " USER_COMMAND_0600  INPUT
  102. *&---------------------------------------------------------------------*
  103. *&      Form  CREATE_ALV
  104. *&---------------------------------------------------------------------*
  105. FORM create_alv.
  106. *create a docking container and dock the control at the botton
  107.   CREATE OBJECT cont_dock
  108.       EXPORTING
  109.            dynnr = '600'
  110.            extension = 100
  111.            side = cl_gui_docking_container=>dock_at_bottom.
  112. *create the alv grid for display the table
  113.   CREATE OBJECT cont_alvgd
  114.       EXPORTING
  115.            i_parent = cont_dock.
  116. *create custome container for alv
  117.   CREATE OBJECT cont_cust
  118.       EXPORTING
  119.            container_name = 'CCONT'.
  120. *create alv editable grid
  121.   CREATE OBJECT cont_editalvgd
  122.       EXPORTING
  123.            i_parent = cont_cust.
  124. * register events for the editable alv
  125.   CREATE OBJECT event_receiver.
  126.   SET HANDLER event_receiver->handle_data_changed FOR cont_editalvgd.
  127.   CALL METHOD cont_editalvgd->register_edit_event
  128.     EXPORTING
  129.       i_event_id = cl_gui_alv_grid=>mc_evt_modified.
  130. *building the fieldcatalogue for the initial display
  131.   PERFORM build_fieldcat CHANGING it_fielcat it_fielcatalogue.
  132. *building the fieldcatalogue after the user has changed it
  133.   PERFORM change_fieldcat CHANGING it_fielcatalogue.
  134. *fetch data from the table
  135.   PERFORM fetch_data.
  136. *    Get excluding functions for the alv editable tool bar
  137.   APPEND cl_gui_alv_grid=>mc_fc_loc_append_row TO it_excl_func.
  138.   APPEND cl_gui_alv_grid=>mc_fc_loc_insert_row TO it_excl_func.
  139.   APPEND cl_gui_alv_grid=>mc_fc_loc_cut TO it_excl_func.
  140.   APPEND cl_gui_alv_grid=>mc_fc_sort TO it_excl_func.
  141.   APPEND cl_gui_alv_grid=>mc_fc_sort_asc TO it_excl_func.
  142.   APPEND cl_gui_alv_grid=>mc_fc_sort_dsc TO it_excl_func.
  143.   APPEND cl_gui_alv_grid=>mc_fc_subtot TO it_excl_func.
  144.   APPEND cl_gui_alv_grid=>mc_fc_sum TO it_excl_func.
  145.   APPEND cl_gui_alv_grid=>mc_fc_graph TO it_excl_func.
  146.   APPEND cl_gui_alv_grid=>mc_fc_info TO it_excl_func.
  147.   APPEND cl_gui_alv_grid=>mc_fc_print TO it_excl_func.
  148.   APPEND cl_gui_alv_grid=>mc_fc_filter TO it_excl_func.
  149.   APPEND cl_gui_alv_grid=>mc_fc_views TO it_excl_func.
  150.   APPEND cl_gui_alv_grid=>mc_mb_export TO it_excl_func.
  151.   APPEND cl_gui_alv_grid=>mc_mb_sum TO it_excl_func.
  152.   APPEND cl_gui_alv_grid=>mc_mb_sum TO it_excl_func.
  153.   APPEND cl_gui_alv_grid=>mc_mb_paste TO it_excl_func.
  154.   APPEND cl_gui_alv_grid=>mc_fc_find TO it_excl_func.
  155.   APPEND cl_gui_alv_grid=>mc_fc_loc_copy  TO it_excl_func.

  156. *Alv display for the T006 table at the bottom
  157.   CALL METHOD cont_alvgd->set_table_for_first_display
  158.     CHANGING
  159.       it_outtab       = it_t006[]
  160.       it_fieldcatalog = it_fielcat[].

  161. * optimize column width of grid displaying fieldcatalog
  162.   it_layout-cwidth_opt = 'X'.
  163. * Get fieldcatalog of table T006 - alv might have
  164. * modified it after passing.
  165.   CALL METHOD cont_alvgd->get_frontend_fieldcatalog
  166.     IMPORTING
  167.       et_fieldcatalog = it_fielcat[].
  168. *to Send Buffered Automation Queue to Frontend
  169.   CALL METHOD cl_gui_cfw=>flush.
  170. * Display fieldcatalog of table T006 in editable alv grid
  171.   CALL METHOD cont_editalvgd->set_table_for_first_display
  172.     EXPORTING
  173.       is_layout            = it_layout
  174.       it_toolbar_excluding = it_excl_func
  175.     CHANGING
  176.       it_outtab            = it_fielcat[]
  177.       it_fieldcatalog      = it_fielcatalogue[].

  178. ENDFORM.                               " CREATE_alv
  179. *&---------------------------------------------------------------------*
  180. *&      Form  fetch_data
  181. *&---------------------------------------------------------------------*
  182. FORM fetch_data.
  183. * select data of T006
  184.   SELECT * FROM t006 INTO TABLE it_t006 UP TO 50 ROWS.

  185. ENDFORM.                               " fetch_data
  186. *&---------------------------------------------------------------------*
  187. *&      Form  BUILD_FIELDCAT
  188. *&---------------------------------------------------------------------*
  189. FORM build_fieldcat CHANGING it_fldcat TYPE lvc_t_fcat
  190.                                    it_fcat TYPE lvc_t_fcat.

  191. * Fieldcatalog for table T006: it_fldcat
  192. * to generate the fields automatically
  193.   CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
  194.     EXPORTING
  195.       i_structure_name       = 'T006'
  196.     CHANGING
  197.       ct_fieldcat            = it_fldcat[]
  198.     EXCEPTIONS
  199.       inconsistent_interface = 1
  200.       program_error          = 2
  201.       OTHERS                 = 3.
  202.   IF sy-subrc <> 0.
  203. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  204. *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  205.   ENDIF.
  206. *----------------------------------------------------
  207. * Fieldcatalog for table LVC_T_FCAT:it_fcat
  208. *----------------------------------------------------
  209. * Generate fieldcatalog of fieldcatalog structure.
  210. * This fieldcatalog is used to display fieldcatalog 'it_fldcat'
  211. * on the top of the screen.
  212.   CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
  213.     EXPORTING
  214.       i_structure_name       = 'LVC_S_FCAT'
  215.     CHANGING
  216.       ct_fieldcat            = it_fcat[]
  217.     EXCEPTIONS
  218.       inconsistent_interface = 1
  219.       program_error          = 2
  220.       OTHERS                 = 3.
  221.   IF sy-subrc <> 0.
  222. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  223. *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  224.   ENDIF.

  225. ENDFORM.                               " BUILD_FIELDCAT

  226. *&---------------------------------------------------------------------*
  227. *&      Form  CHANGE_FIELDCAT
  228. *&---------------------------------------------------------------------*
  229. *after the user has modified the fieldcatalogue we build another fieldcat
  230. *for the modified alv display
  231. FORM change_fieldcat CHANGING it_fcat TYPE lvc_t_fcat.
  232.   DATA ls_fcat TYPE lvc_s_fcat.
  233.   LOOP AT it_fcat INTO ls_fcat.
  234.     ls_fcat-coltext = ls_fcat-fieldname.
  235.     ls_fcat-edit = 'X'.
  236.     IF ls_fcat-fieldname = 'COL_POS' OR ls_fcat-fieldname = 'FIELDNAME'.
  237.       ls_fcat-key = 'X'.
  238.     ENDIF.
  239.     MODIFY it_fcat FROM ls_fcat.
  240.   ENDLOOP.
  241. ENDFORM.                               " CHANGE_FIELDCAT
复制代码


程序执行效果:
Editable 1.png
输出报表时我们不用考虑“MANDT”,现在我们编辑alv的一些字段(例如 直接删除一些字段),然后单击submit按钮,输出如下
Editable 2.png
回复

使用道具 举报

快速回帖

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

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