二维码

[ooalv] 在alv工具栏上自定义button按钮

Twilight发表于 2014-08-19 19:48Twilight 最后回复于 2014-08-19 19:48 [复制链接] 5323 0

程序代码:
  1. *type pools declaratins for icon and ALV
  2. TYPE-POOLS : slis,icon.
  3. *Structure declaration for tcodes
  4. TYPES : BEGIN OF ty_table,
  5.         tcode TYPE tcode,
  6.         pgmna TYPE progname,
  7.         END OF ty_table.
  8. *Structure for tocde text
  9. TYPES : BEGIN OF ty_itext,
  10.         tcode TYPE tcode,
  11.         ttext TYPE ttext_stct,
  12.         sprsl TYPE sprsl,
  13.         END OF ty_itext.
  14. *Structure for output display
  15. TYPES : BEGIN OF ty_output,
  16.         tcode TYPE tcode,
  17.         pgmna TYPE progname,
  18.         ttext TYPE ttext_stct,
  19.        END OF ty_output.
  20. *internal table and work area declarations
  21. DATA : it_table TYPE STANDARD TABLE OF ty_table INITIAL SIZE 0,
  22.        it_output TYPE STANDARD TABLE OF ty_output INITIAL SIZE 0,
  23.        it_ittext TYPE STANDARD TABLE OF ty_itext INITIAL SIZE 0,
  24.        wa_table TYPE ty_table,
  25.        wa_output TYPE ty_output,
  26.        wa_ittext TYPE ty_itext.
  27. *Class definition for ALV toolbar
  28. CLASS:      lcl_alv_toolbar   DEFINITION DEFERRED.
  29. *Declaration for toolbar buttons
  30. DATA : ty_toolbar TYPE stb_button.
  31. * Data declarations for ALV
  32. DATA: c_ccont TYPE REF TO cl_gui_custom_container,   "Custom container object
  33.       c_alvgd         TYPE REF TO cl_gui_alv_grid,   "ALV grid object
  34.       it_fcat            TYPE lvc_t_fcat,            "Field catalogue
  35.       it_layout          TYPE lvc_s_layo,            "Layout
  36.       c_alv_toolbar    TYPE REF TO lcl_alv_toolbar,           "Alv toolbar
  37.       c_alv_toolbarmanager TYPE REF TO cl_alv_grid_toolbar_manager.  "Toolbar manager
  38. *Initialization event
  39. INITIALIZATION.
  40. *Start of selection event
  41. START-OF-SELECTION.
  42. *Subroutine to get values from tstc table
  43.   PERFORM fetch_data.
  44. *subroutine for alv display
  45.   PERFORM alv_output.
  46. *---------------------------------------------------------------------*
  47. *       CLASS lcl_alv_toolbar DEFINITION
  48. *---------------------------------------------------------------------*
  49. *       ALV event handler
  50. *---------------------------------------------------------------------*
  51. CLASS lcl_alv_toolbar DEFINITION.
  52.   PUBLIC SECTION.
  53. *Constructor
  54.     METHODS: constructor
  55.                IMPORTING
  56.                  io_alv_grid TYPE REF TO cl_gui_alv_grid,
  57. *Event for toolbar
  58.     on_toolbar
  59.        FOR EVENT toolbar
  60.        OF  cl_gui_alv_grid
  61.        IMPORTING
  62.          e_object.
  63. ENDCLASS.                    "lcl_alv_toolbar DEFINITION
  64. *---------------------------------------------------------------------*
  65. *       CLASS lcl_alv_toolbar IMPLEMENTATION
  66. *---------------------------------------------------------------------*
  67. *       ALV event handler
  68. *---------------------------------------------------------------------*
  69. CLASS lcl_alv_toolbar IMPLEMENTATION.
  70.   METHOD constructor.
  71. *   Create ALV toolbar manager instance
  72.     CREATE OBJECT c_alv_toolbarmanager
  73.       EXPORTING
  74.         io_alv_grid      = io_alv_grid.
  75.   ENDMETHOD.                    "constructor
  76.   METHOD on_toolbar.
  77. *   Add customized toolbar buttons.
  78. *   variable for Toolbar Button
  79.     ty_toolbar-icon      =  icon_generate.
  80.     ty_toolbar-butn_type = 0.
  81.     ty_toolbar-text = 'Button1'.
  82.     APPEND ty_toolbar TO e_object->mt_toolbar.
  83.     ty_toolbar-icon      =  icon_voice_output.
  84.     ty_toolbar-butn_type = 0.
  85.     ty_toolbar-text = 'Button2'.


  86.     APPEND ty_toolbar TO e_object->mt_toolbar.
  87.     ty_toolbar-icon      =  icon_phone.
  88.     ty_toolbar-butn_type = 0.
  89.     ty_toolbar-text = 'Button3'.

  90.     APPEND ty_toolbar TO e_object->mt_toolbar.
  91.     ty_toolbar-icon      =  icon_mail.
  92.     ty_toolbar-butn_type = 0.
  93.     ty_toolbar-text = 'Button4'.
  94.     APPEND ty_toolbar TO e_object->mt_toolbar.
  95.     ty_toolbar-icon      =  icon_voice_input.
  96.     ty_toolbar-butn_type = 0.
  97.     ty_toolbar-text = 'Button5'.
  98.     APPEND ty_toolbar TO e_object->mt_toolbar.
  99. **   Call reorganize method of toolbar manager to
  100. **   display the toolbar
  101.     CALL METHOD c_alv_toolbarmanager->reorganize
  102.       EXPORTING
  103.         io_alv_toolbar = e_object.
  104.   ENDMETHOD.                    "on_toolbar
  105. ENDCLASS.                    "lcl_alv_toolbar IMPLEMENTATION
  106. *&---------------------------------------------------------------------*
  107. *&      Form  fetch_data
  108. *&---------------------------------------------------------------------*
  109. *       text
  110. *----------------------------------------------------------------------*
  111. *  -->  p1        text
  112. *  <--  p2        text
  113. *----------------------------------------------------------------------*
  114. FORM fetch_data .
  115. * Select the tcodes upto 200 rows from TSTC
  116.   SELECT   tcode
  117.            pgmna
  118.            FROM tstc
  119.            INTO CORRESPONDING FIELDS OF TABLE it_table
  120.            UP TO 200 ROWS
  121.            WHERE dypno NE '0000'.
  122. *Select the tcode textx
  123.   IF it_table[] IS NOT INITIAL.
  124.     SELECT ttext
  125.            tcode
  126.            sprsl
  127.            FROM tstct
  128.            INTO CORRESPONDING FIELDS OF TABLE it_ittext
  129.            FOR ALL ENTRIES IN it_table
  130.            WHERE tcode = it_table-tcode
  131.            AND sprsl = 'E'.
  132.   ENDIF.
  133. * Apppending the data to the internal table of ALV output
  134.   LOOP AT it_table INTO wa_table.
  135.     wa_output-tcode = wa_table-tcode.
  136.     wa_output-pgmna = wa_table-pgmna.
  137. *    For texts
  138.     READ TABLE it_ittext INTO wa_ittext WITH KEY tcode = wa_table-tcode.
  139.     wa_output-ttext = wa_ittext-ttext.
  140.     APPEND wa_output TO it_output.
  141.     CLEAR wa_output.
  142.   ENDLOOP.
  143. ENDFORM.                    " fetch_data
  144. *&---------------------------------------------------------------------*
  145. *&      Form  alv_output
  146. *&---------------------------------------------------------------------*
  147. *       text
  148. *----------------------------------------------------------------------*
  149. *  -->  p1        text
  150. *  <--  p2        text
  151. *----------------------------------------------------------------------*
  152. FORM alv_output .
  153. *Calling the ALV
  154.   CALL SCREEN 0600.
  155. ENDFORM.                    " alv_output
  156. ** Calling the ALV screen with custom container
  157. *On this statement double click  it takes you to the screen painter SE51.*Enter the attributes
  158. *Create a Custom container and name it CC_CONT and OK code as OK_CODE.
  159. *Save check and Activate the screen painter.
  160. *Now a normal screen with number 600 is created which holds the ALV grid.
  161. * PBO of the actual screen , Here we can give a title and customized menus
  162. *&---------------------------------------------------------------------*
  163. *&      Module  STATUS_0600  OUTPUT
  164. *&---------------------------------------------------------------------*
  165. *       text
  166. *----------------------------------------------------------------------*
  167. MODULE status_0600 OUTPUT.
  168. *  SET PF-STATUS 'xxxxxxxx'.
  169. *  SET TITLEBAR 'xxx'.
  170. ENDMODULE.                 " STATUS_0600  OUTPUT
  171. * calling the PBO module ALV_GRID.
  172. *&---------------------------------------------------------------------*
  173. *&      Module  ALV_GRID  OUTPUT
  174. *&---------------------------------------------------------------------*
  175. *       text
  176. *----------------------------------------------------------------------*
  177. MODULE alv_grid OUTPUT.
  178. *create object for custom container
  179.   CREATE OBJECT c_ccont
  180.        EXPORTING
  181.           container_name = 'CC_CONT'.
  182. *create object of alv grid
  183.   CREATE OBJECT c_alvgd
  184.       EXPORTING
  185.           i_parent = c_ccont.
  186. * create ALV event handler
  187.   CREATE OBJECT c_alv_toolbar
  188.     EXPORTING
  189.       io_alv_grid = c_alvgd.
  190. * Register event handler
  191.   SET HANDLER c_alv_toolbar->on_toolbar FOR c_alvgd.
  192. * Fieldcatalogue for ALV
  193.   PERFORM alv_build_fieldcat.
  194. * ALV attributes FOR LAYOUT
  195.   PERFORM alv_report_layout.
  196.   CHECK NOT c_alvgd IS INITIAL.
  197. * Call ALV GRID
  198.   CALL METHOD c_alvgd->set_table_for_first_display
  199.     EXPORTING
  200.       is_layout                     = it_layout
  201.     CHANGING
  202.       it_outtab                     = it_output
  203.       it_fieldcatalog               = it_fcat
  204.     EXCEPTIONS
  205.       invalid_parameter_combination = 1
  206.       program_error                 = 2
  207.       too_many_lines                = 3
  208.       OTHERS                        = 4.
  209.   IF sy-subrc <> 0.
  210.     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  211.                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  212.   ENDIF.
  213. ENDMODULE.                 " ALV_GRID  OUTPUT
  214. *&---------------------------------------------------------------------*
  215. *&      Form  alv_build_fieldcat
  216. *&---------------------------------------------------------------------*
  217. *       text
  218. *----------------------------------------------------------------------*
  219. *      <--P_IT_FCAT  text
  220. *----------------------------------------------------------------------*
  221. FORM alv_build_fieldcat.
  222.   DATA lv_fldcat TYPE lvc_s_fcat.
  223.   CLEAR lv_fldcat.
  224.   lv_fldcat-row_pos   = '1'.
  225.   lv_fldcat-col_pos   = '1'.
  226.   lv_fldcat-fieldname = 'TCODE'.
  227.   lv_fldcat-tabname   = 'IT_OUTPUT'.
  228.   lv_fldcat-outputlen = 8.
  229.   lv_fldcat-scrtext_m = 'TCODE'.
  230.   APPEND lv_fldcat TO it_fcat.
  231.   CLEAR lv_fldcat.
  232.   lv_fldcat-row_pos   = '1'.
  233.   lv_fldcat-col_pos   = '2'.
  234.   lv_fldcat-fieldname = 'PGMNA'.
  235.   lv_fldcat-tabname   = 'IT_OUTPUT'.
  236.   lv_fldcat-outputlen = 15.
  237.   lv_fldcat-scrtext_m = 'PROGNAME'.
  238.   APPEND lv_fldcat TO it_fcat.
  239.   CLEAR lv_fldcat.
  240.   lv_fldcat-row_pos   = '1'.
  241.   lv_fldcat-col_pos   = '3'.
  242.   lv_fldcat-fieldname = 'TTEXT'.
  243.   lv_fldcat-tabname   = 'IT_OUTPUT'.
  244.   lv_fldcat-outputlen = 60.
  245.   lv_fldcat-scrtext_m = 'Description'.
  246.   APPEND lv_fldcat TO it_fcat.
  247.   CLEAR lv_fldcat.
  248. ENDFORM.                    " alv_build_fieldcat
  249. *&---------------------------------------------------------------------*
  250. *&      Form  alv_report_layout
  251. *&---------------------------------------------------------------------*
  252. *       text
  253. *----------------------------------------------------------------------*
  254. *      <--P_IT_LAYOUT  text
  255. *----------------------------------------------------------------------*
  256. FORM alv_report_layout.
  257.   it_layout-cwidth_opt = 'X'.
  258.   it_layout-zebra = 'X'.
  259. ENDFORM.                    " alv_report_layout
  260. * PAI module of the screen created. In case we use an interactive ALV or
  261. *for additional functionalities we can create OK codes
  262. *and based on the user command we can do the coding.
  263. *&---------------------------------------------------------------------*
  264. *&      Module  USER_COMMAND_0600  INPUT
  265. *&---------------------------------------------------------------------*
  266. *       text
  267. *----------------------------------------------------------------------*
  268. MODULE user_command_0600 INPUT.

  269. ENDMODULE.                 " USER_COMMAND_0600  INPUT
复制代码

程序执行效果:
ALV with user defined buttons.png
单击箭头按钮可以显示标准工具栏
回复

使用道具 举报

快速回帖

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

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