ALV Grid控制元件拖放功能

sapkitty發表於2007-06-16

程式BCALV_DND_04。

[@more@]

REPORT ZALVGRID_DRAGDROP MESSAGE-ID TREE_CONTROL_MSG.
*-------------------------------------------------------------------
* Purpose
* ~~~~~~~
* This example shows how to define a drag and drop behaviour using
* a drag and drop control.
* The behaviour is defined within one ALV Control.
*--------------------------------------------------------------------
* To check program behaviour:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Try to move or copy lines by dragging them to another position of
* the grid control.
*---------------------------------------------------------------------
* Essential steps: (Search for '§')
* ~~~~~~~~~~~~~~~~
* 1. Define reference variables to CL_DRAGDROP: In this case
* you need only one for a D&D-behaviour within one ALV Control.
* 2. Define a behaviour for drag and drop on alv objects
* and get its handle.
* 3. Link defined behaviour to all rows of the ALV Control.
* 4. Define a class for a data object to exchange data
* within ALV Control when using the drag and drop operation.
* 5. Define methods for two events raised by the drag and drop
* control. (Note that the names for these events depend on
* the classes for which the behaviour was defined!).
* 6. In the 'onDrag' event handler create a data object and fill it with
* appropriate data for your intended operation. This event is used
* to 'fetch' information from the drag source.
* 7. Assign your data object to the refering event parameter.
* This parameter ensures that your data object can be referenced
* in each of the following events.
* 8. Implement the event handler for event 'OnDrop'. This event is used
* to use your dragged information in combination with your drop
* source. What is more, you should make all checks
* if the operation is successful _at this point_.
* 9. Check which operation the user has conducted (copy or move).
*----------------------------------------------------------------------
START-OF-SELECTION.
SET SCREEN 100.

*#####################################################################
* global data
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CLASS LCL_APPLICATION DEFINITION DEFERRED.

DATA:
G_APPLICATION TYPE REF TO LCL_APPLICATION,
G_DOCKING_CONTAINER TYPE REF TO CL_GUI_DOCKING_CONTAINER,
G_ALV TYPE REF TO CL_GUI_ALV_GRID,
* §1. Define reference variables to CL_DRAGDROP: In this case
* you need only one for a D&D-behaviour within one ALV Control.

G_BEHAVIOUR_ALV TYPE REF TO CL_DRAGDROP,
* G_OK_CODE TYPE SY-UCOMM,
GT_OUTTAB TYPE TABLE OF SBOOK,
g_max type i value 50.
* You need the layout structure of alv to transfer the handle
* of your defined behaviour (see step 2).
DATA: GS_LAYOUT TYPE LVC_S_LAYO.


*#####################################################################
* Class definitions and method implementation
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* §4. Define a class for a data object to exchange data
* within ALV Control when using the drag and drop operation.
CLASS LCL_DRAGDROPOBJ DEFINITION.
PUBLIC SECTION.
DATA: WA_SBOOK TYPE SBOOK,
INDEX TYPE I. "Index of Line to be moved or copied.

ENDCLASS.

*---------------------------------------------------------------------*
* CLASS LCL_APPLICATION DEFINITION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
* §5. Define methods for two events raised by the drag and drop
* control. (Note that the names for these events depend on
* the classes for which the behaviour was defined!).
CLASS LCL_APPLICATION DEFINITION.

PUBLIC SECTION.
METHODS:
HANDLE_ALV_DRAG
FOR EVENT ONDRAG
OF CL_GUI_ALV_GRID
IMPORTING E_ROW E_COLUMN E_DRAGDROPOBJ,
HANDLE_ALV_DROP
FOR EVENT ONDROP
OF CL_GUI_ALV_GRID
IMPORTING E_ROW E_COLUMN E_DRAGDROPOBJ.
ENDCLASS.

*---------------------------------------------------------------------*
* CLASS LCL_APPLICATION IMPLEMENTATION
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
CLASS LCL_APPLICATION IMPLEMENTATION.
*-------------------------------------------------------------------
* §6.In the 'onDrag' event handler create a data object and fill it with
* appropriate data for your intended operation. This event is used
* to 'fetch' information from the drag source.
METHOD HANDLE_ALV_DRAG.
DATA: DATAOBJ TYPE REF TO LCL_DRAGDROPOBJ,
LINE TYPE SBOOK.

* Read dragged row READ TABLE GT_OUTTAB INDEX E_ROW-INDEX INTO LINE.

* create and fill dataobject for events ONDROP and ONDROPCOMPLETE CREATE OBJECT DATAOBJ.
* remember the row index to copy or move a line MOVE E_ROW-INDEX TO DATAOBJ->INDEX.
* store the dragged line, too.
READ TABLE GT_OUTTAB INTO DATAOBJ->WA_SBOOK INDEX E_ROW-INDEX.

* §7. Assign your data object to the refering event parameter.
* This parameter ensures that your data object can be referenced
* in each of the following events.

E_DRAGDROPOBJ->OBJECT = DATAOBJ.
ENDMETHOD.

*--------------------------------------------------------------------
* §8.Implement the event handler for event 'OnDrop'. This event is used
* to use your dragged information in combination with your drop
* source. What is more, you should make all checks
* if the operation is successful _at this point_.
METHOD HANDLE_ALV_DROP.
DATA: DATAOBJ TYPE REF TO LCL_DRAGDROPOBJ,
DROP_INDEX TYPE I,
LS_SBOOK TYPE SBOOK,
STABLE TYPE LVC_S_STBL.

* Refresh Alv Grid Control without scrolling STABLE-ROW = 'X'.
STABLE-COL = 'X'.

*!!!
* Very importent: 'e_dragDropObj->object' can have any instance type
* The needed cast type may lead to a system-exception if the
* cast can not be performed.
* For this reason: use ALWAYS the Catch-Statement to make sure
* that the drag&drop-Operation is aborted properly.
*!!!
CATCH SYSTEM-EXCEPTIONS MOVE_CAST_ERROR = 1.

DATAOBJ ?= E_DRAGDROPOBJ->OBJECT.

* 9. Check which operation the user has conducted (copy or move).

IF E_DRAGDROPOBJ->EFFECT EQ CL_DRAGDROP=>COPY.
INSERT DATAOBJ->WA_SBOOK INTO GT_OUTTAB INDEX E_ROW-INDEX.
ELSE.
DELETE GT_OUTTAB INDEX DATAOBJ->INDEX.
INSERT DATAOBJ->WA_SBOOK INTO GT_OUTTAB INDEX E_ROW-INDEX.
ENDIF.

CALL METHOD G_ALV->REFRESH_TABLE_DISPLAY
EXPORTING I_SOFT_REFRESH = 'X'
IS_STABLE = STABLE.

ENDCATCH.
IF SY-SUBRC <> 0.
* If anything went wrong this is the clean way of aborting the
* drag and drop operation:
CALL METHOD E_DRAGDROPOBJ->ABORT.
ENDIF.
ENDMETHOD.

ENDCLASS.
*#####################################################################
*&---------------------------------------------------------------------*
*& Module PBO_0400 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE PBO_100 OUTPUT.

SET PF-STATUS 'MAIN'.
set titlebar 'MAIN'.
IF G_ALV IS INITIAL.
PERFORM CREATE_AND_INIT_CONTROLS.
ENDIF.

ENDMODULE. " PBO_0100 OUTPUT
*#####################################################################
*&---------------------------------------------------------------------*
*& Module PAI_0100 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE PAI_100 INPUT.

CASE G_OK_CODE.
WHEN 'BACK'. " Finish program
IF NOT G_DOCKING_CONTAINER IS INITIAL.
CALL METHOD G_DOCKING_CONTAINER->FREE
EXCEPTIONS
CNTL_SYSTEM_ERROR = 1
CNTL_ERROR = 2.
IF SY-SUBRC <> 0.
MESSAGE A000.
ENDIF.
CALL METHOD CL_GUI_CFW=>FLUSH
EXCEPTIONS
CNTL_SYSTEM_ERROR = 1
CNTL_ERROR = 2.
IF SY-SUBRC <> 0.
MESSAGE A000.
ENDIF.
CLEAR G_DOCKING_CONTAINER.
CLEAR G_ALV.
ENDIF.
LEAVE PROGRAM.
ENDCASE.

* CAUTION: clear ok code! CLEAR G_OK_CODE.
ENDMODULE. " PAI_0100 INPUT
*#####################################################################
* Forms
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*&---------------------------------------------------------------------*
*& Form CREATE_AND_INIT_CONTROLS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --&gt p1 text
* *----------------------------------------------------------------------*
FORM CREATE_AND_INIT_CONTROLS.
* create docking container for alv control CREATE OBJECT G_DOCKING_CONTAINER
EXPORTING
DYNNR = '100'
EXTENSION = 260
SIDE = CL_GUI_DOCKING_CONTAINER=>DOCK_AT_TOP.


* create alv control CREATE OBJECT G_ALV
EXPORTING I_PARENT = G_DOCKING_CONTAINER.

* create the application object
* this object is needed to handle the ABAP Objects Events of
* Controls
CREATE OBJECT G_APPLICATION.

* Events alv control SET HANDLER G_APPLICATION->HANDLE_ALV_DRAG FOR G_ALV.
SET HANDLER G_APPLICATION->HANDLE_ALV_DROP FOR G_ALV.

* build tree nodes and describe behaviour of drag&drop PERFORM BUILD_AND_ASSIGN_HANDLE.

* select and display data from SBOOK SELECT * FROM SBOOK INTO TABLE GT_OUTTAB up to g_max rows. "#EC CI_NOWHERE

GS_LAYOUT-GRID_TITLE = TEXT-101.

CALL METHOD G_ALV->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING I_STRUCTURE_NAME = 'SBOOK'
IS_LAYOUT = GS_LAYOUT
CHANGING IT_OUTTAB = GT_OUTTAB.

ENDFORM. " CREATE_AND_INIT_CONTROLS
*&---------------------------------------------------------------------*
*& Form build_nodes_and_handles
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --&gt p1 text
* *----------------------------------------------------------------------*

FORM BUILD_AND_ASSIGN_HANDLE.

DATA: EFFECT TYPE I,
HANDLE_ALV TYPE I.

* §2. Define a behaviour for drag and drop on alv objects
* and get its handle.

* define a drag & Drop behaviour for the whole grid CREATE OBJECT G_BEHAVIOUR_ALV.
EFFECT = CL_DRAGDROP=>MOVE + CL_DRAGDROP=>COPY.
CALL METHOD G_BEHAVIOUR_ALV->ADD
EXPORTING
FLAVOR = 'Line' "#EC NOTEXT
DRAGSRC = 'X'
DROPTARGET = 'X'
EFFECT = EFFECT.
CALL METHOD G_BEHAVIOUR_ALV->GET_HANDLE
IMPORTING HANDLE = HANDLE_ALV.

*..........
* §3. Link defined behaviour to all rows of the ALV Control.
*
* Remark: The place at which you transfer your handle is control
* dependend!


* provide handle to alv control using the layout-structure
* In this example all rows obtain the same drag and drop behaviour:
GS_LAYOUT-S_DRAGDROP-ROW_DDID = HANDLE_ALV.

ENDFORM. " build_nodes_and_handles
*#####################################################################

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8214011/viewspace-910676/,如需轉載,請註明出處,否則將追究法律責任。

相關文章