SAP ALV 拖拽放置 drag&drop 实现方法

程天佑
2023-12-01

SAP ALV 拖拽放置 drag&drop 实现方法

1.ALV Display 内表中定义技术字段(as cellstyle color等)

 Data: begin of gt_dis OCCURS 0,
             field1 type c lenth 10,
             field2 type c lenth 10,
             dnd type lvc_t_drdr,
           end of gt_dis .
 go_dragdrop TYPE REF TO cl_dragdrop 

2.内表赋值

  CREATE OBJECT go_dragdrop.
  lv_effect = cl_dragdrop=>move + cl_dragdrop=>copy.
  CALL METHOD c_dragdropalv->add
    EXPORTING
      flavor     = 'MOVE'
      dragsrc    = 'X'
      droptarget = 'X'
      effect     =  lv_effect.
      
*Get the handle for drag and drop
  CALL METHOD c_dragdropalv->get_handle
    IMPORTING
      handle = lv_handle_alv.  
      
Data: ls_drdr type lvc_s_drdr ,
         lt_drdr type lvc_t_drdr .
 
 "赋值后field1字段可以将进行拖放
 clear ls_drdr.
 ls_drdr-fieldname  = 'FIELD1'. 
 ls_drdr-handle     =  lv_handle_alv.  
 append ls_drdr TO lt_drdr .
 
 "赋值后field2字段可以将进行拖放
 clear ls_drdr.
 ls_drdr-fieldname  = 'FIELD2'. 
 ls_drdr-handle     =  lv_handle_alv.  
 append ls_drdr TO lt_drdr .

 gt_dis-field1      = 'Test1'.
 gt_dis-field2      = 'Test2'.
 gt_dis-dnd        = lt_drdr.
 APPEND gt_dis.

2.Layout 参数事件注册

     “ 参数赋值
      is_layout-s_dragdrop-row_ddid   = handle_alv.
      is_layout-s_dragdrop-fieldname  ='DND'
      
      ”注册事件 as hotspot or dada change
      DATA go_event TYPE REF TO lcl_objdragdropapp .
      CREATE OBJECT go_event.
      SET HANDLER go_event->handle_alv_drag FOR go_alv.
      SET HANDLER go_event->handle_alv_drop FOR go_alv.

3.方法实现

CLASS lcl_objdragdropapp DEFINITION.
  PUBLIC SECTION.
    METHODS:
      handle_alv_drag  FOR EVENT ondrag  OF cl_gui_alv_grid
                                  IMPORTING e_row e_column e_dragdropobj,
*Handling event DROP
      handle_alv_drop  FOR EVENT ondrop  OF cl_gui_alv_grid
                                  IMPORTING e_row e_column e_dragdropobj.
ENDCLASS.                    "LCL_objdragdropapp DEFINITION
CLASS lcl_objdragdropapp IMPLEMENTATION.
  METHOD handle_alv_drag.
       e_column-fieldname : 拖拽时选择的源数据的列名
       e_row-index : 拖拽时选择的源数据行
       或者用get_selected_cells 方法得到当前的行和列
                 call method go_alv->get_selected_cells 
                       IMPORTING ~
      
       记录下source data 的行和列或内表数据:
         Example:
         DATA:  lo_dataobj TYPE REF TO lcl_dragdrop,
         CREATE OBJECT  lo_dataobj.
         MOVE e_row-index TO  lo_dataobj->index. “保存当前行
         READ TABLE gt_dis INTO  lo_dataobj->wa INDEX e_row-index."保存当前数据
         e_dragdropobj->object = dataobj.
  ENDMETHOD.                    "HANDLE_ALV_DRAG

  METHOD handle_alv_drop.
* Catch-Statement to ensure the drag&drop-Operation is aborted properly.
    CATCH SYSTEM-EXCEPTIONS move_cast_error = 1.
      DATA: lo_dataobj TYPE REF TO lcl_dragdrop,
       lo_dataobj?= e_dragdropobj->object.
       ”Main Logic
       e_column-fieldname : 放置时选择的目的cell列名
       e_row-index : 放置时选择的目的cell行
       与在drag中记录的lo_dataobj行列进行对比
       两个ALV之间的拖放可通过:e_dragdropobj->dragsourcectrl属性进行区分
       根据业务需要进行内表的操作后刷新ALV显示
    ENDCATCH.
    IF sy-subrc <> 0.
      CALL METHOD e_dragdropobj->abort.
    ENDIF.
  ENDMETHOD.                    "HANDLE_ALV_DROP
  
ENDCLASS.                    "LCL_objdragdropapp IMPLEMENTATION
 类似资料: