首先要为所有拖放的操作对象做初始化拖放的说明
DATA: DRAGDROP_TREE TYPE REF TO CL_DRAGDROP,
DRAGDROP_ALV TYPE REF TO CL_DRAGDROP,
* 允许放下的效果
G_DROPEFFECT = CL_DRAGDROP=>MOVE.
* 初始化拖放
* -> tree
CREATE OBJECT DRAGDROP_TREE.
CALL METHOD DRAGDROP_TREE->ADD EXPORTING
FLAVOR = 'LINE'
DRAGSRC = SELECTED
DROPTARGET = ''
EFFECT = G_DROPEFFECT.
CALL METHOD DRAGDROP_TREE->GET_HANDLE IMPORTING
HANDLE = G_HANDLE_TREE.
* -> ALV grid
CREATE OBJECT DRAGDROP_ALV.
CALL METHOD DRAGDROP_ALV->ADD EXPORTING
FLAVOR = 'LINE'
DRAGSRC = ''
DROPTARGET = SELECTED
EFFECT = G_DROPEFFECT.
CALL METHOD DRAGDROP_ALV->GET_HANDLE IMPORTING
HANDLE = G_HANDLE_ALV.
之后需要分别为ALV/TREE的LAYOUT赋拖放参数
'UMZIN' '转换因子',
'UMZIZ' '转换因子',
'WAERK' '凭证货币'.
CREATE OBJECT LO_EVENT.
SET HANDLER LO_EVENT->HANDLE_TOOLBAR FOR GO_ALV_GRID_SUB.
SET HANDLER LO_EVENT->HANDLE_COMMAND FOR GO_ALV_GRID_SUB.
SET HANDLER LO_EVENT->HANDLE_ONDROP FOR GO_ALV_GRID_SUB.
WA_LAYOUT_CACHE-GRID_TITLE = '发出商品明细表(双击/拖拽汇总树查看)'."标题栏文本
WA_LAYOUT_CACHE-SEL_MODE = 'D'. "选择方式
WA_LAYOUT_CACHE-CWIDTH_OPT = 'X'. "优化列宽
* handle for D'n'D
WA_LAYOUT_CACHE-S_DRAGDROP-ROW_DDID = G_HANDLE_ALV.“为ALV/TREE的LAYOUT赋参数
CALL METHOD GO_ALV_GRID_SUB->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
IS_LAYOUT = WA_LAYOUT_CACHE
CHANGING
IT_FIELDCATALOG = L_FCAT_CACHE
IT_OUTTAB = GT_DATA_D[].
* CLEAR GT_DATA_D[].
GO_ALV_GRID_SUB->REFRESH_TABLE_DISPLAY( ).
* add node for CARRID
LS_NODE_LAYOUT-DRAGDROPID = G_HANDLE_TREE.
FORM ADD_CARRID_LINE1 USING PS_GT_DATA_C TYPE GTS_DATA
P_RELAT_KEY TYPE LVC_NKEY
CHANGING P_NODE_KEY TYPE LVC_NKEY.
DATA: L_NODE_TEXT TYPE LVC_VALUE.
DATA: GS_DATA_C TYPE GTS_DATA.
DATA: LT_ITEM_LAYOUT TYPE LVC_T_LAYI,
LS_ITEM_LAYOUT TYPE LVC_S_LAYI,
LS_NODE_LAYOUT TYPE LVC_S_LAYN.
* set item-layout
* LS_ITEM_LAYOUT-T_IMAGE = '@FN@'.
LS_ITEM_LAYOUT-FIELDNAME = TREE1->C_HIERARCHY_COLUMN_NAME.
APPEND LS_ITEM_LAYOUT TO LT_ITEM_LAYOUT.
* longtext for CARRID
L_NODE_TEXT = '大类:'&& PS_GT_DATA_C-VTEXT1.
CLEAR GS_DATA_C .
GS_DATA_C-VTEXT1 = PS_GT_DATA_C-VTEXT1 .
GS_DATA_C-P_NODE = 'L' .
* add node for CARRID
LS_NODE_LAYOUT-DRAGDROPID = G_HANDLE_TREE. “为ALV/TREE的LAYOUT赋参数
CALL METHOD TREE1->ADD_NODE
EXPORTING
I_RELAT_NODE_KEY = P_RELAT_KEY
I_RELATIONSHIP = CL_GUI_COLUMN_TREE=>RELAT_LAST_CHILD
I_NODE_TEXT = L_NODE_TEXT
IS_NODE_LAYOUT = LS_NODE_LAYOUT
IS_OUTTAB_LINE = GS_DATA_C
IT_ITEM_LAYOUT = LT_ITEM_LAYOUT
IMPORTING
E_NEW_NODE_KEY = P_NODE_KEY.
ENDFORM.
METHOD没什么好说的,需要什么写什么就好
* Drag----------------------------------------------
SET HANDLER TREE_EVENT_RECEIVER->HANDLE_ON_DRAG FOR TREE1.
METHOD HANDLE_ON_DRAG.
CHECK NOT NODE_KEY IS INITIAL.
PERFORM DISPLAY_FLIGHTS USING NODE_KEY.
ENDMETHOD.
FORM DISPLAY_FLIGHTS USING P_NODE_KEY TYPE LVC_NKEY.
DATA: GRID_TITLE TYPE LVC_TITLE.
DATA: LS_DATA TYPE GTS_DATA,
E_PARENT_NODE_KEY TYPE LVC_NKEY,
LT_CHILDREN TYPE LVC_T_NKEY WITH HEADER LINE,
LT_CHILDREN1 TYPE LVC_T_NKEY WITH HEADER LINE.
REFRESH GT_DATA_D .
CALL METHOD TREE1->GET_OUTTAB_LINE
EXPORTING I_NODE_KEY = P_NODE_KEY
IMPORTING E_OUTTAB_LINE = LS_DATA.
***************************************************************
ENDIF.
ENDFORM.
* Drag-----------------------------------------------
* Drop-----------------------------------------------
SET HANDLER LO_EVENT->HANDLE_ONDROP FOR GO_ALV_GRID_SUB.
* Drop
METHOD HANDLE_ONDROP.
CHECK NOT E_ROW IS INITIAL.
*
ENDMETHOD.
* Drop-----------------------------------------------、
值得注意的是,添加的Drag事件的效果会在Drop事件结束后触发,所以如果我想要的效果不需要Drop事件的参数,我就会统一将效果写在Drag事件中(例如将数据拖拽至树的节点上这种操作是需要Drop事件的参数的)。