程序代码:
  1. *Structure declaration for T016T
  2. TYPES : BEGIN OF ty_t016t,
  3.          brsch TYPE brsch,
  4.          brtxt TYPE text1_016t,
  5.          spras TYPE spras,
  6.          END OF ty_t016t.
  7. *Work area and internal table for  T016T
  8. DATA : it_t016t TYPE STANDARD TABLE OF ty_t016t,
  9.        wa_t016t TYPE ty_t016t.
  10. *class declaration
  11. CLASS lcl_objdragdropapp DEFINITION DEFERRED.
  12. *data declarations for ALV
  13. DATA:   c_dragdropapp TYPE REF TO lcl_objdragdropapp,
  14.         c_dockingcont TYPE REF TO cl_gui_docking_container,
  15.         c_alv TYPE REF TO cl_gui_alv_grid,
  16. * reference variable to CL_DRAGDROP:
  17.         c_dragdropalv TYPE REF TO cl_dragdrop,
  18.         it_layout TYPE lvc_s_layo,
  19.         it_fcat  TYPE lvc_t_fcat.            "Field catalogue
  20. *declarations for handle event
  21. DATA:   effect TYPE i,
  22.         handle_alv TYPE i.
  23. *initialization event
  24. INITIALIZATION.
  25. *start of selection event
  26. START-OF-SELECTION.
  27. *select data
  28.   PERFORM fetch_data.
  29. *ALV output
  30.   PERFORM alv_output.
  31. * Class definitions and method implementation for drag and drop
  32. CLASS lcl_dragdrop DEFINITION.
  33.   PUBLIC SECTION.
  34.     DATA: wa TYPE ty_t016t,
  35.           index TYPE i.   "Index of Line to be moved
  36. ENDCLASS.                    "LCL_DRAGDROP DEFINITION
  37. *Application class definition
  38. CLASS lcl_objdragdropapp DEFINITION.
  39.   PUBLIC SECTION.
  40.     METHODS:
  41. *Handling Event Drag
  42.       handle_alv_drag
  43.         FOR EVENT ondrag
  44.         OF cl_gui_alv_grid
  45.         IMPORTING e_row e_column e_dragdropobj,
  46. *Handling event DROP
  47.       handle_alv_drop
  48.         FOR EVENT ondrop
  49.         OF cl_gui_alv_grid
  50.         IMPORTING e_row e_column e_dragdropobj.
  51. ENDCLASS.                    "LCL_objdragdropapp DEFINITION
  52. *Application class implementation
  53. CLASS lcl_objdragdropapp IMPLEMENTATION.
  54. *  OnDrag event is used to 'fetch' information from the drag source.
  55.   METHOD handle_alv_drag.
  56.     DATA: dataobj TYPE REF TO lcl_dragdrop,
  57.           line TYPE ty_t016t.
  58. * Read dragged row
  59.     READ TABLE it_t016t INDEX e_row-index INTO line.
  60. * create and fill dataobject for events ONDROP
  61.     CREATE OBJECT dataobj.
  62. * Remembering row index to move a line
  63.     MOVE e_row-index TO dataobj->index.
  64. * store the dragged line.
  65.     READ TABLE it_t016t INTO dataobj->wa INDEX e_row-index.
  66. * Assigning data object to the refering event parameter
  67.     e_dragdropobj->object = dataobj.
  68.   ENDMETHOD.                    "HANDLE_ALV_DRAG
  69. *Event handler for event 'OnDrop'. This event is used
  70. *to use your dragged information in combination with your drop source.
  71.   METHOD handle_alv_drop.
  72.     DATA: dataobj TYPE REF TO lcl_dragdrop,
  73.           drop_index TYPE i,
  74.           stable TYPE lvc_s_stbl.
  75. * Refresh Alv Grid Control without scrolling
  76.     stable-row = 'X'.
  77.     stable-col = 'X'.

  78. * Catch-Statement to ensure the drag&drop-Operation is aborted properly.
  79.     CATCH SYSTEM-EXCEPTIONS move_cast_error = 1.
  80.       dataobj ?= e_dragdropobj->object.
  81.       DELETE it_t016t INDEX dataobj->index.
  82.       INSERT dataobj->wa INTO it_t016t INDEX e_row-index.
  83. *Refreshing the ALV
  84.       CALL METHOD c_alv->refresh_table_display
  85.         EXPORTING
  86.           i_soft_refresh = 'X'
  87.           is_stable      = stable.
  88.     ENDCATCH.
  89.     IF sy-subrc <> 0.
  90. * If anything went wrong aborting the drag and drop operation:
  91.       CALL METHOD e_dragdropobj->abort.
  92.     ENDIF.
  93.   ENDMETHOD.                    "HANDLE_ALV_DROP
  94. ENDCLASS.                    "LCL_objdragdropapp IMPLEMENTATION
  95. *&---------------------------------------------------------------------*
  96. *&      Form  alv_output
  97. *&---------------------------------------------------------------------*
  98. *       text
  99. *----------------------------------------------------------------------*
  100. *  -->  p1        text
  101. *  <--  p2        text
  102. *----------------------------------------------------------------------*
  103. FORM alv_output .
  104.   CALL SCREEN 600.

  105. ENDFORM.                    " alv_output
  106. ** Calling the ALV screen with custom container
  107. *On this statement double click  it takes you to the screen painter SE51.
  108. *Enter the attributes
  109. *Create a Custom container and name it CC_CONT and OK code as OK_CODE.
  110. *Save check and Activate the screen painter.
  111. *Now a normal screen with number 600 is created which holds the ALV grid.
  112. * PBO of the actual screen ,Here we can give a title and customized menus
  113. *&---------------------------------------------------------------------*
  114. *&      Module  STATUS_0600  OUTPUT
  115. *&---------------------------------------------------------------------*
  116. *       text
  117. *----------------------------------------------------------------------*
  118. MODULE status_0600 OUTPUT.
  119. *  SET PF-STATUS 'xxxxxxxx'.
  120. *  SET TITLEBAR 'xxx'.
  121.   IF c_alv IS INITIAL.
  122.     PERFORM alv_controls.
  123.   ENDIF.
  124. ENDMODULE.                 " STATUS_0600  OUTPUT
  125. *&---------------------------------------------------------------------*
  126. *&      Form  alv_CONTROLS
  127. *&---------------------------------------------------------------------*
  128. *       text
  129. *----------------------------------------------------------------------*
  130. *  -->  p1        text
  131. *  <--  p2        text
  132. *----------------------------------------------------------------------*
  133. FORM alv_controls.
  134. * create docking container for alv control
  135.   CREATE OBJECT c_dockingcont
  136.     EXPORTING
  137.         dynnr = '600'
  138.         extension = 300
  139.         side = cl_gui_docking_container=>dock_at_top.

  140. * create alv control
  141.   CREATE OBJECT c_alv
  142.      EXPORTING i_parent = c_dockingcont.
  143. * create the application object to handle the ABAP Objects Events
  144.   CREATE OBJECT c_dragdropapp.
  145. * Events alv control
  146. *For Dragging
  147.   SET HANDLER c_dragdropapp->handle_alv_drag FOR c_alv.
  148. *For Dropping
  149.   SET HANDLER c_dragdropapp->handle_alv_drop FOR c_alv.
  150. * build tree nodes for drag&drop
  151.   PERFORM build_handle.
  152. * Fieldcatalogue for ALV
  153.   PERFORM alv_build_fieldcat.
  154. * ALV attributes FOR LAYOUT
  155.   PERFORM alv_report_layout.
  156. * Call ALV GRID
  157.   CALL METHOD c_alv->set_table_for_first_display
  158.     EXPORTING
  159.       is_layout                     = it_layout
  160.     CHANGING
  161.       it_outtab                     = it_t016t
  162.       it_fieldcatalog               = it_fcat
  163.     EXCEPTIONS
  164.       invalid_parameter_combination = 1
  165.       program_error                 = 2
  166.       too_many_lines                = 3
  167.       OTHERS                        = 4.
  168.   IF sy-subrc <> 0.
  169.     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  170.                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  171.   ENDIF.
  172. ENDFORM.                               "ALV_CONTROLS
  173. *&---------------------------------------------------------------------*
  174. *&      Form  build_handle
  175. *&---------------------------------------------------------------------*
  176. *       text
  177. *----------------------------------------------------------------------*
  178. *  -->  p1        text
  179. *  <--  p2        text
  180. *----------------------------------------------------------------------*
  181. FORM build_handle.

  182. * define a drag & Drop behaviour for the whole grid
  183.   CREATE OBJECT c_dragdropalv.
  184.   effect = cl_dragdrop=>move + cl_dragdrop=>copy.
  185.   CALL METHOD c_dragdropalv->add
  186.     EXPORTING
  187.       flavor     = 'Line'
  188.       dragsrc    = 'X'
  189.       droptarget = 'X'
  190.       effect     = effect.
  191. *getting the handle for drag and drop
  192.   CALL METHOD c_dragdropalv->get_handle
  193.     IMPORTING
  194.       handle = handle_alv.

  195. ENDFORM.                               " build_handle
  196. *&---------------------------------------------------------------------*
  197. *&      Form  fetch_data
  198. *&---------------------------------------------------------------------*
  199. *       text
  200. *----------------------------------------------------------------------*
  201. *  -->  p1        text
  202. *  <--  p2        text
  203. *----------------------------------------------------------------------*
  204. FORM fetch_data .
  205. * select and display data from t016
  206.   SELECT brtxt brsch spras FROM t016t INTO CORRESPONDING FIELDS OF TABLE it_t016t
  207.   WHERE spras = 'EN'.

  208. ENDFORM.                    " fetch_data
  209. *&---------------------------------------------------------------------*
  210. *&      Form  alv_report_layout
  211. *&---------------------------------------------------------------------*
  212. *       text
  213. *----------------------------------------------------------------------*
  214. *  -->  p1        text
  215. *  <--  p2        text
  216. *----------------------------------------------------------------------*
  217. FORM alv_report_layout .
  218.   it_layout-grid_title = 'ALV Drag Drop'.
  219. * provide handle to alv control to all rows for same drag & drop behaviour
  220.   it_layout-s_dragdrop-row_ddid = handle_alv.

  221. ENDFORM.                    " alv_report_layout
  222. *&---------------------------------------------------------------------*
  223. *&      Form  alv_build_fieldcat
  224. *&---------------------------------------------------------------------*
  225. *       text
  226. *----------------------------------------------------------------------*
  227. *  -->  p1        text
  228. *  <--  p2        text
  229. *----------------------------------------------------------------------*
  230. FORM alv_build_fieldcat .
  231.   DATA lv_fldcat TYPE lvc_s_fcat.
  232.   CLEAR lv_fldcat.
  233.   lv_fldcat-row_pos   = '1'.
  234.   lv_fldcat-col_pos   = '1'.
  235.   lv_fldcat-fieldname = 'BRSCH'.
  236.   lv_fldcat-tabname   = 'IT_T016T'.
  237.   lv_fldcat-outputlen = 8.
  238.   lv_fldcat-scrtext_m = 'Industry'.
  239.   APPEND lv_fldcat TO it_fcat.
  240.   CLEAR lv_fldcat.
  241.   lv_fldcat-row_pos   = '1'.
  242.   lv_fldcat-col_pos   = '2'.
  243.   lv_fldcat-fieldname = 'BRTXT'.
  244.   lv_fldcat-tabname   = 'IT_T016T'.
  245.   lv_fldcat-outputlen = 15.
  246.   lv_fldcat-scrtext_m = 'Description'.
  247.   APPEND lv_fldcat TO it_fcat.
  248.   CLEAR lv_fldcat.

  249. ENDFORM.                    " alv_build_fieldcat
  250. * PAI module of the screen created. In case we use an interactive ALV or
  251. *for additional functionalities we can create OK codes
  252. *and based on the user command we can do the coding.
  253. *&---------------------------------------------------------------------*
  254. *&      Module  USER_COMMAND_0600  INPUT
  255. *&---------------------------------------------------------------------*
  256. *       text
  257. *----------------------------------------------------------------------*
  258. MODULE user_command_0600 INPUT.
  259. ENDMODULE.                 " USER_COMMAND_0600  INPUT
复制代码

程序执行效果:
可手工拖拽alv行记录的实例 
alv行记录被拖拽之后的效果
可手工拖拽alv行记录的实例 

相关文章: