二维码

[fmalv] 在alv上下文菜单中增加自定义子菜单

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


程序代码:
  1. REPORT  YDAMON_043 NO STANDARD PAGE HEADING.
  2. *******************************************************************
  3. * Type pool declaration
  4. *******************************************************************
  5. TYPE-POOLS: slis.
  6. ******************************************************************
  7. * Internal table declaration
  8. ******************************************************************
  9. DATA: BEGIN OF gt_outtab OCCURS 0.
  10.         INCLUDE STRUCTURE sflight.
  11. DATA: END OF gt_outtab.
  12. data: gt_events       TYPE slis_t_event.
  13. *****************************************************************
  14. * Structure / Variable declaration
  15. *****************************************************************
  16. DATA: g_repid         LIKE sy-repid,
  17.       event           TYPE slis_ALV_event.

  18. *****************************************************************
  19. * Event: START-OF-SELECTION
  20. ******************************************************************
  21. START-OF-SELECTION.
  22. * Storing the program name
  23.   g_repid = sy-repid.
  24. * Building ALV event table
  25.   CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
  26.     EXPORTING
  27.       i_list_type     = 4
  28.     IMPORTING
  29.       et_events       = gt_events
  30.     EXCEPTIONS
  31.       list_type_wrong = 1
  32.       OTHERS          = 2.
  33.   IF sy-subrc = 0.
  34.     REFRESH gt_events.
  35. *   Adding records for CONTEXT_MENU event
  36.     event-name = 'CONTEXT_MENU'.
  37.     event-form = 'CONTEXT_MENU'.
  38.     APPEND event TO gt_events.
  39.   ENDIF.

  40. * Data Selection
  41.   SELECT * FROM sflight INTO CORRESPONDING FIELDS
  42.                    OF TABLE gt_outtab
  43.                    UP TO 00030 ROWS.
  44. * Display ALV grid
  45.   CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  46.     EXPORTING
  47.       i_callback_html_top_of_page = 'HTML_TOP_OF_PAGE'
  48.       i_callback_program          = g_repid
  49.       i_callback_user_command     = 'USER_COMMAND'
  50.       i_structure_name            = 'SFLIGHT'
  51.       it_events                   = gt_events
  52.     TABLES
  53.       t_outtab                    = gt_outtab
  54.     EXCEPTIONS
  55.       program_error               = 1
  56.       OTHERS                      = 2.
  57.   IF sy-subrc <> 0.
  58. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
  59. *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  60.   ENDIF.

  61. *****************************************************************
  62. * FORM html_top_of_page
  63. *****************************************************************
  64. FORM html_top_of_page USING top TYPE REF TO cl_dd_document.
  65.   CALL METHOD top->add_text
  66.     EXPORTING
  67.       text      = 'Hello world '
  68.       sap_style = 'heading'.
  69.   CALL METHOD top->add_gap
  70.     EXPORTING
  71.       width = 200.
  72.   CALL METHOD top->add_picture
  73.     EXPORTING
  74.       picture_id = 'ENJOYSAP_LOGO'.
  75. ENDFORM.                    "html_top_of_page
  76. ****************************************************************
  77. * Form  context_menu
  78. ****************************************************************
  79. FORM context_menu USING e_object TYPE REF TO cl_ctmenu.
  80.   DATA: l_smenu TYPE REF TO cl_ctmenu.
  81.   IF e_object IS BOUND.
  82. *   Create custom Sub-menu to hide column on which right
  83. *   mouse button will be clicked
  84.     CREATE OBJECT l_smenu.
  85.     CALL METHOD l_smenu->add_function
  86.       EXPORTING
  87.         fcode = 'ZFN1'
  88.         text  = 'Hide Column'(001).
  89.     CALL METHOD e_object->add_submenu
  90.       EXPORTING
  91.         menu = l_smenu
  92.         text = 'Hide'(002).

  93.   ENDIF.
  94. ENDFORM.                    "CONTEXT_MENU
  95. ******************************************************************
  96. * Form  user_command
  97. ******************************************************************
  98. FORM user_command  USING r_ucomm TYPE sy-ucomm
  99.                          ls_selfield TYPE slis_selfield.

  100.   DATA: g_grid TYPE REF TO cl_gui_alv_grid,
  101.         t_catalog TYPE lvc_t_fcat,
  102.         w_catalog TYPE lvc_s_fcat,
  103.         l_repid  TYPE sy-repid.

  104.   CASE r_ucomm.

  105. *   When 'hide column' sub-menu is clicked from the context menu
  106. *   then hide the column from where this is happened
  107.     WHEN 'ZFN1'.
  108. *     Get the global instance of the ALV grid as well as
  109. *     it's field catalog info.
  110.       CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
  111.         IMPORTING
  112.           e_callback_program = l_repid
  113.           e_grid             = g_grid
  114.           et_fieldcat_lvc    = t_catalog.
  115.       CHECK l_repid = g_repid.
  116.       IF g_grid IS BOUND AND t_catalog[] IS NOT INITIAL.
  117. *       Set the 'NO_OUT' attribute of the catalog to 'X'
  118.         w_catalog-no_out = 'X'.
  119. *       Modify the field with this above value
  120. *       on which right click occured
  121.         MODIFY t_catalog FROM w_catalog TRANSPORTING no_out
  122.             WHERE fieldname = ls_selfield-fieldname.
  123.         IF sy-subrc = 0.
  124. *         Set the field catalog with this modified one
  125.           CALL METHOD g_grid->set_frontend_fieldcatalog
  126.             EXPORTING
  127.               it_fieldcatalog = t_catalog.
  128.         ENDIF.
  129.       ENDIF.
  130.     WHEN OTHERS.
  131. * Do nothing
  132.   ENDCASE.
  133.   ls_selfield-refresh = 'X'.
  134. ENDFORM.                    "USER_COMMAND
复制代码


程序执行结果:
Add custom sub-menu.jpg
鼠标右键单击,隐藏该行记录
回复

使用道具 举报

快速回帖

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

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