Code: Alles auswählen.
REPORT zkar_dragdrop_5
MESSAGE-ID ztass_util.
* Overcome the restriction of the SALV model using the power of the
* Object Oriented ABAP
TYPES:
ty_t_sflights TYPE STANDARD TABLE OF sflights.
DATA:
gt_data TYPE ty_t_sflights.
* Class declaration
CLASS:
lcl_salv_model DEFINITION DEFERRED,
lcl_objdragdropapp DEFINITION DEFERRED.
* Data declarations for alv
DATA:
gr_container TYPE REF TO cl_gui_custom_container,
gr_dragdropapp TYPE REF TO lcl_objdragdropapp,
gr_alv TYPE REF TO cl_gui_alv_grid,
* Reference variable to CL_DRAGDROP
gr_dragdropalv TYPE REF TO cl_dragdrop.
* Declarations for handle event
DATA:
gv_effect TYPE i,
gv_handle_alv TYPE i,
gv_okcode TYPE syucomm.
DATA:
gr_salv TYPE REF TO cl_salv_table,
gr_salv_model TYPE REF TO lcl_salv_model.
*-----------------------------------------------------------------------
* Define the Local class inheriting from the CL_SALV_MODEL_LIST
* to get an access of the model, controller and adapter which inturn
* provides the Grid Object
*-----------------------------------------------------------------------
CLASS lcl_salv_model DEFINITION INHERITING FROM cl_salv_model_list.
PUBLIC SECTION.
DATA:
lo_control TYPE REF TO cl_salv_controller_model,
lo_adapter TYPE REF TO cl_salv_adapter.
METHODS:
grabe_model
IMPORTING
io_model TYPE REF TO cl_salv_model,
grabe_controller,
grabe_adapter.
PRIVATE SECTION.
DATA:
lo_model TYPE REF TO cl_salv_model.
ENDCLASS. "LCL_SALV_MODEL DEFINITION
* LCL_SALV_MODEL implementation
CLASS lcl_salv_model IMPLEMENTATION.
METHOD grabe_model.
* Save the model
lo_model = io_model.
ENDMETHOD. "grabe_model
METHOD grabe_controller.
* Save the controller
lo_control = lo_model->r_controller.
ENDMETHOD. "grabe_controller
METHOD grabe_adapter.
* Save the adapter from controller
lo_adapter ?= lo_model->r_controller->r_adapter.
ENDMETHOD. "grabe_adapter
ENDCLASS. "LCL_SALV_MODEL IMPLEMENTATION
*-----------------------------------------------------------------------
* Event handler for the added buttons
*-----------------------------------------------------------------------
CLASS lcl_event_handler DEFINITION.
PUBLIC SECTION.
METHODS:
on_user_command
FOR EVENT added_function
OF cl_salv_events
IMPORTING e_salv_function.
ENDCLASS. "lcl_event_handler DEFINITION
* Event Handler for the SALV
CLASS lcl_event_handler IMPLEMENTATION.
METHOD on_user_command.
* DATA:
* lo_grid TYPE REF TO cl_gui_alv_grid,
** lo_full_adapter TYPE REF TO cl_salv_fullscreen_adapter.
* lo_full_adapter TYPE REF TO cl_salv_grid_adapter.
DATA:
ls_layout TYPE lvc_s_layo.
CASE e_salv_function.
* Make ALV as Editable ALV
WHEN 'MYFUNCTION'.
** Controller
* CALL METHOD gr_salv_model->grabe_controller.
** Adapter
* CALL METHOD gr_salv_model->grabe_adapter.
** Fullscreen Adapter (Down Casting)
* lo_full_adapter ?= gr_salv_model->lo_adapter.
** Get the Grid
* lo_grid = lo_full_adapter->get_grid( ).
** Got the Grid ?
* IF lo_grid IS BOUND.
** Editable ALV
* ls_layout-edit = 'X'.
** Set the front layout of ALV
* CALL METHOD lo_grid->set_frontend_layout
* EXPORTING
* is_layout = ls_layout.
** Refresh the table
* CALL METHOD lo_grid->refresh_table_display.
* ENDIF.
* Editable ALV
ls_layout-edit = 'X'.
* Set the front layout of ALV
CALL METHOD gr_alv->set_frontend_layout
EXPORTING
is_layout = ls_layout.
* Refresh the table
CALL METHOD gr_alv->refresh_table_display.
ENDCASE.
ENDMETHOD. "on_user_command
ENDCLASS. "lcl_event_handler IMPLEMENTATION
*-----------------------------------------------------------------------
* Class definitions and method implementation for drag and drop
*-----------------------------------------------------------------------
CLASS lcl_dragdrop DEFINITION.
PUBLIC SECTION.
DATA: wa TYPE sflights,
index TYPE i. "Index of Line to be moved
ENDCLASS. "LCL_DRAGDROP DEFINITION
*-----------------------------------------------------------------------
* Application class definition
*-----------------------------------------------------------------------
CLASS lcl_objdragdropapp DEFINITION.
PUBLIC SECTION.
METHODS:
* Handling Event Drag
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
* Application class implementation
CLASS lcl_objdragdropapp IMPLEMENTATION.
* OnDrag event is used to 'fetch' information from the drag source.
METHOD handle_alv_drag.
DATA:
lo_dataobj TYPE REF TO lcl_dragdrop,
ls_line TYPE sflights.
* Read dragged row
READ TABLE gt_data INDEX e_row-index INTO ls_line.
* Create and fill dataobject for events ONDROP
CREATE OBJECT lo_dataobj.
* Remembering row index to move a line
MOVE e_row-index TO lo_dataobj->index.
* Store the dragged line.
READ TABLE gt_data INTO lo_dataobj->wa INDEX e_row-index.
* Assigning data object to the refering event parameter
e_dragdropobj->object = lo_dataobj.
ENDMETHOD. "HANDLE_ALV_DRAG
* Event handler for event 'OnDrop'. This event is used
* to use your dragged information in combination with your drop source.
METHOD handle_alv_drop.
DATA:
lo_dataobj TYPE REF TO lcl_dragdrop.
* lo_grid TYPE REF TO cl_gui_alv_grid,
* lo_full_adapter TYPE REF TO cl_salv_fullscreen_adapter,
* ls_layout TYPE lvc_s_layo,
* ls_stable TYPE lvc_s_stbl.
** Refresh Alv Grid Control without scrolling
* ls_stable-row = 'X'.
* ls_stable-col = 'X'.
* Catch-Statement to ensure the drag&drop-Operation is aborted properly.
CATCH SYSTEM-EXCEPTIONS move_cast_error = 1.
lo_dataobj ?= e_dragdropobj->object.
DELETE gt_data INDEX lo_dataobj->index.
INSERT lo_dataobj->wa INTO gt_data INDEX e_row-index.
** Controller
* CALL METHOD gr_salv_model->grabe_controller.
** Adapter
* CALL METHOD gr_salv_model->grabe_adapter.
** Fullscreen Adapter (Down Casting)
* lo_full_adapter ?= gr_salv_model->lo_adapter.
** Get the Grid
* lo_grid = lo_full_adapter->get_grid( ).
** Got the Grid ?
* IF lo_grid IS BOUND.
*** Editable ALV
** CLEAR ls_layout.
** ls_layout-edit = 'X'.
*** Set the front layout of ALV
** CALL METHOD lo_grid->set_frontend_layout
** EXPORTING
** is_layout = ls_layout.
** refresh the table
* CALL METHOD lo_grid->refresh_table_display
* EXPORTING
* i_soft_refresh = 'X'
* is_stable = ls_stable.
* ENDIF.
gr_salv->refresh( ).
ENDCATCH.
IF sy-subrc <> 0.
* If anything went wrong aborting the drag and drop operation
CALL METHOD e_dragdropobj->abort.
ENDIF.
ENDMETHOD. "HANDLE_ALV_DROP
ENDCLASS. "LCL_objdragdropapp IMPLEMENTATION
* Start of selection
START-OF-SELECTION.
* test data
SELECT *
FROM sflights
INTO TABLE gt_data
UP TO 30 ROWS.
CALL SCREEN 0100 STARTING AT 10 5.
*----------------------------------------------------------------------*
* MODULE pbo OUTPUT
*----------------------------------------------------------------------*
MODULE pbo OUTPUT.
PERFORM pbo.
ENDMODULE. "pbo OUTPUT
*----------------------------------------------------------------------*
* MODULE pai INPUT
*----------------------------------------------------------------------*
MODULE pai INPUT.
PERFORM pai.
ENDMODULE. "pai INPUT
*&---------------------------------------------------------------------*
*& Form pbo
*&---------------------------------------------------------------------*
FORM pbo.
* SET PF-STATUS 'D0100' OF PROGRAM 'SALV_DEMO_TABLE_FORM_EVENTS'.
SET PF-STATUS 'D0100'.
IF gr_container IS NOT BOUND.
IF cl_salv_table=>is_offline( ) EQ if_salv_c_bool_sap=>false.
CREATE OBJECT gr_container
EXPORTING
container_name = 'CONTAINER'.
ENDIF.
* New ALV Instance
TRY.
cl_salv_table=>factory(
EXPORTING
r_container = gr_container
container_name = 'CONTAINER'
IMPORTING
r_salv_table = gr_salv
CHANGING
t_table = gt_data ).
CATCH cx_salv_msg. "#EC NO_HANDLER
ENDTRY.
** New ALV Instance
* TRY.
* cl_salv_table=>factory(
* EXPORTING
* list_display = abap_false
* IMPORTING
* r_salv_table = gr_salv
* CHANGING
* t_table = gt_data ).
* CATCH cx_salv_msg. "#EC NO_HANDLER
* ENDTRY.
* Activate ALV generic Functions
DATA:
lr_functions TYPE REF TO cl_salv_functions_list,
lv_text TYPE string,
lv_icon TYPE string.
lr_functions = gr_salv->get_functions( ).
lr_functions->set_all( 'X' ).
** Add MYFUNCTION from the report SALV_DEMO_TABLE_EVENTS
* gr_salv->set_screen_status(
* pfstatus = 'SALV_STANDARD'
* report = 'SALV_DEMO_TABLE_EVENTS'
* set_functions = gr_salv->c_functions_all ).
* Include own functions
TRY.
lv_text = 'Meine eigene Funktion'.
lv_icon = icon_complete.
lr_functions->add_function(
name = 'MYFUNCTION'
icon = lv_icon
text = lv_text
tooltip = lv_text
position = if_salv_c_function_position=>right_of_salv_functions ).
CATCH cx_salv_wrong_call cx_salv_existing. "#EC NO_HANDLER
ENDTRY.
* Set the selection mode of the Popup: multiple or single row selection
DATA:
lr_selections TYPE REF TO cl_salv_selections.
lr_selections = gr_salv->get_selections( ).
lr_selections->set_selection_mode( if_salv_c_selection_mode=>multiple ).
* lr_selections->set_selection_mode( if_salv_c_selection_mode=>row_column ).
* gr_salv->set_screen_popup(
* start_column = 10
* end_column = 110
* start_line = 2
* end_line = 25 ).
* Event handler for the button
DATA:
lo_events TYPE REF TO cl_salv_events_table,
lo_event_h TYPE REF TO lcl_event_handler.
* Event object
lo_events = gr_salv->get_event( ).
* Event handler
CREATE OBJECT lo_event_h.
* Setting up the event handler
SET HANDLER lo_event_h->on_user_command FOR lo_events.
* Get Model Object
DATA:
lo_alv_model TYPE REF TO cl_salv_model,
* lo_full_adapter TYPE REF TO cl_salv_fullscreen_adapter.
lo_full_adapter TYPE REF TO cl_salv_grid_adapter.
* Narrow casting
lo_alv_model ?= gr_salv.
* Object for the local inherited class from the CL_SALV_MODEL_LIST
CREATE OBJECT gr_salv_model.
* Grabe model to use it later
CALL METHOD gr_salv_model->grabe_model
EXPORTING
io_model = lo_alv_model.
* Generate ALV output
* It is required to execute the display method otherwise the adapter
* will not been instantiated when grabbing the alv grid
gr_salv->display( ).
* Controller
CALL METHOD gr_salv_model->grabe_controller.
* Adapter
CALL METHOD gr_salv_model->grabe_adapter.
* Fullscreen Adapter (Down Casting)
lo_full_adapter ?= gr_salv_model->lo_adapter.
IF lo_full_adapter IS BOUND.
* Get the Grid
gr_alv = lo_full_adapter->get_grid( ).
* Got the Grid ?
IF gr_alv IS BOUND.
* Create the application object to handle the ABAP Objects Events
CREATE OBJECT gr_dragdropapp.
* Events alv control
* For Dragging
SET HANDLER gr_dragdropapp->handle_alv_drag FOR gr_alv.
* For Dropping
SET HANDLER gr_dragdropapp->handle_alv_drop FOR gr_alv.
* Define a drag & Drop behaviour for the whole grid
CREATE OBJECT gr_dragdropalv.
gv_effect = cl_dragdrop=>move + cl_dragdrop=>copy.
CALL METHOD gr_dragdropalv->add
EXPORTING
flavor = 'Line'
dragsrc = 'X'
droptarget = 'X'
effect = gv_effect.
* Getting the handle for drag and drop
CALL METHOD gr_dragdropalv->get_handle
IMPORTING
handle = gv_handle_alv.
* Provide handle to alv control to all rows for same drag & drop behaviour
DATA:
ls_layout TYPE lvc_s_layo.
CLEAR ls_layout.
* Editable ALV
* ls_layout-edit = 'X'.
ls_layout-zebra = 'X'.
ls_layout-s_dragdrop-row_ddid = gv_handle_alv.
* ls_layout-s_dragdrop-cntr_ddid = gv_handle_alv.
* Set the front layout of ALV
CALL METHOD gr_alv->set_frontend_layout
EXPORTING
is_layout = ls_layout.
ENDIF.
ENDIF.
ENDIF.
ENDFORM. "pbo
*&---------------------------------------------------------------------*
*& Form pai
*&---------------------------------------------------------------------*
FORM pai.
CASE gv_okcode.
WHEN 'BACK' OR 'EXIT' OR 'CANC'.
SET SCREEN 0.
LEAVE SCREEN.
WHEN 'ZDDW'.
MESSAGE i000 WITH 'Weiter...'.
SET SCREEN 0.
LEAVE SCREEN.
WHEN 'ZDDZ'.
MESSAGE i000 WITH 'Zurück...'.
SET SCREEN 0.
LEAVE SCREEN.
WHEN OTHERS.
SET SCREEN 0.
LEAVE SCREEN.
ENDCASE.
ENDFORM. "pai
*** Programmende ***
Code: Alles auswählen.
REPORT .
DATA: gt_data TYPE STANDARD TABLE OF sflight WITH NON-UNIQUE DEFAULT KEY,
go_salv TYPE REF TO cl_salv_table,
go_grid TYPE REF TO cl_gui_alv_grid,
ok_code TYPE syucomm.
START-OF-SELECTION.
SELECT * UP TO 100 ROWS
INTO TABLE gt_data
FROM sflight.
END-OF-SELECTION.
PERFORM create_salv.
CALL SCREEN 9000 STARTING AT 40 4.
*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_9000 OUTPUT.
SET PF-STATUS '9000'.
* SET TITLEBAR 'xxx'.
ENDMODULE. " STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
MODULE user_command_9000 INPUT.
CASE ok_code.
WHEN 'ABBR'
OR 'EXIT'
OR 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
*& Form CREATE_SALV
*&---------------------------------------------------------------------*
FORM create_salv .
DATA: ls_child LIKE LINE OF cl_gui_container=>screen1->children.
DATA: lo_salv_functions_list TYPE REF TO cl_salv_functions_list.
TRY.
cl_salv_table=>factory( EXPORTING
r_container = cl_gui_container=>screen1 " 1. Popuplevel vollständig überdecken
IMPORTING
r_salv_table = go_salv
CHANGING
t_table = gt_data ).
lo_salv_functions_list = go_salv->get_functions( ).
lo_salv_functions_list->set_all( ).
go_salv->display( ).
READ TABLE cl_gui_container=>screen1->children INDEX 1 INTO ls_child.
* Sinnvoller Test, ob das überhaupt ein Grid ist sollte man hier wohl einbauen - hab ich aber gerade keine Lust dazu
go_grid ?= ls_child.
BREAK-POINT.
CATCH cx_salv_msg .
ENDTRY.
ENDFORM. " CREATE_SALV
Code: Alles auswählen.
REPORT zkar_dragdrop_6
MESSAGE-ID ztass_util.
* Overcome the restriction of the SALV model using the power of the
* Object Oriented ABAP
*
*-----------------------------------------------------------------------
* PROBLEM: Drag und Drop funktioniert erst, wenn die
* Funktion MYFUNCTION einmal!!! ausgeführt wurde
*-----------------------------------------------------------------------
TYPES:
ty_t_sflights TYPE STANDARD TABLE OF sflights.
DATA:
gt_data TYPE ty_t_sflights.
* Class declaration
CLASS:
lcl_objdragdropapp DEFINITION DEFERRED.
* Data declarations for alv
DATA:
gr_dragdropapp TYPE REF TO lcl_objdragdropapp,
gr_alv TYPE REF TO cl_gui_alv_grid,
* Reference variable to CL_DRAGDROP
gr_dragdropalv TYPE REF TO cl_dragdrop.
* Declarations for handle event
DATA:
gv_effect TYPE i,
gv_handle_alv TYPE i,
gv_okcode TYPE syucomm.
DATA:
gr_salv TYPE REF TO cl_salv_table.
*-----------------------------------------------------------------------
* Event handler for the added buttons
*-----------------------------------------------------------------------
CLASS lcl_event_handler DEFINITION.
PUBLIC SECTION.
METHODS:
on_user_command
FOR EVENT added_function
OF cl_salv_events
IMPORTING e_salv_function.
ENDCLASS. "lcl_event_handler DEFINITION
* Event Handler for the SALV
CLASS lcl_event_handler IMPLEMENTATION.
METHOD on_user_command.
DATA:
ls_layout TYPE lvc_s_layo.
CASE e_salv_function.
* Make ALV as Editable ALV
WHEN 'MYFUNCTION'.
* Get the front layout of ALV
CALL METHOD gr_alv->get_frontend_layout
IMPORTING
es_layout = ls_layout.
IF ls_layout-edit IS INITIAL.
* Editable ALV
ls_layout-edit = 'X'.
ELSE.
* Non-Editable ALV
CLEAR ls_layout-edit.
ENDIF.
* Set the front layout of ALV
CALL METHOD gr_alv->set_frontend_layout
EXPORTING
is_layout = ls_layout.
* Refresh the table
CALL METHOD gr_alv->refresh_table_display.
ENDCASE.
ENDMETHOD. "on_user_command
ENDCLASS. "lcl_event_handler IMPLEMENTATION
*-----------------------------------------------------------------------
* Class definitions and method implementation for drag and drop
*-----------------------------------------------------------------------
CLASS lcl_dragdrop DEFINITION.
PUBLIC SECTION.
DATA: wa TYPE sflights,
index TYPE i. "Index of Line to be moved
ENDCLASS. "LCL_DRAGDROP DEFINITION
*-----------------------------------------------------------------------
* Application class definition
*-----------------------------------------------------------------------
CLASS lcl_objdragdropapp DEFINITION.
PUBLIC SECTION.
METHODS:
* Handling Event Drag
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
* Application class implementation
CLASS lcl_objdragdropapp IMPLEMENTATION.
* OnDrag event is used to 'fetch' information from the drag source.
METHOD handle_alv_drag.
DATA:
lo_dataobj TYPE REF TO lcl_dragdrop,
ls_line TYPE sflights. "#EC NEEDED
* Read dragged row
READ TABLE gt_data INDEX e_row-index INTO ls_line.
* Create and fill dataobject for events ONDROP
CREATE OBJECT lo_dataobj.
* Remembering row index to move a line
MOVE e_row-index TO lo_dataobj->index.
* Store the dragged line.
READ TABLE gt_data INTO lo_dataobj->wa INDEX e_row-index.
* Assigning data object to the refering event parameter
e_dragdropobj->object = lo_dataobj.
ENDMETHOD. "HANDLE_ALV_DRAG
* Event handler for event 'OnDrop'. This event is used
* to use your dragged information in combination with your drop source.
METHOD handle_alv_drop.
DATA:
lo_dataobj TYPE REF TO lcl_dragdrop.
* Catch-Statement to ensure the drag&drop-Operation is aborted properly.
CATCH SYSTEM-EXCEPTIONS move_cast_error = 1.
lo_dataobj ?= e_dragdropobj->object.
DELETE gt_data INDEX lo_dataobj->index.
INSERT lo_dataobj->wa INTO gt_data INDEX e_row-index.
gr_salv->refresh( ).
ENDCATCH.
IF sy-subrc <> 0.
* If anything went wrong aborting the drag and drop operation
CALL METHOD e_dragdropobj->abort.
ENDIF.
ENDMETHOD. "HANDLE_ALV_DROP
ENDCLASS. "LCL_objdragdropapp IMPLEMENTATION
* Start of selection
START-OF-SELECTION.
* Test data
SELECT *
FROM sflights
INTO TABLE gt_data
UP TO 30 ROWS.
CALL SCREEN 0100 STARTING AT 10 5
ENDING AT 100 16.
* CALL SCREEN 0100.
*----------------------------------------------------------------------*
* MODULE pbo OUTPUT
*----------------------------------------------------------------------*
MODULE pbo OUTPUT.
PERFORM pbo.
ENDMODULE. "pbo OUTPUT
*----------------------------------------------------------------------*
* MODULE pai INPUT
*----------------------------------------------------------------------*
MODULE pai INPUT.
PERFORM pai.
ENDMODULE. "pai INPUT
*&---------------------------------------------------------------------*
*& Form pbo
*&---------------------------------------------------------------------*
FORM pbo.
SET PF-STATUS 'D0100C'.
* New ALV Instance
TRY.
cl_salv_table=>factory(
EXPORTING
list_display = abap_false
r_container = cl_gui_container=>screen1
IMPORTING
r_salv_table = gr_salv
CHANGING
t_table = gt_data ).
CATCH cx_salv_msg. "#EC NO_HANDLER
ENDTRY.
* Activate ALV generic Functions
DATA:
lr_functions TYPE REF TO cl_salv_functions_list,
lv_text TYPE string,
lv_icon TYPE string.
lr_functions = gr_salv->get_functions( ).
* lr_functions->set_all( 'X' ).
* Include own functions
TRY.
lv_text = 'Meine eigene Funktion'. "#EC NOTEXT
lv_icon = icon_complete.
lr_functions->add_function(
name = 'MYFUNCTION'
icon = lv_icon
text = lv_text
tooltip = lv_text
position = if_salv_c_function_position=>right_of_salv_functions ).
CATCH cx_salv_wrong_call cx_salv_existing. "#EC NO_HANDLER
ENDTRY.
** Set the selection mode of the Popup: multiple or single row selection
* DATA:
* lr_selections TYPE REF TO cl_salv_selections.
*
* lr_selections = gr_salv->get_selections( ).
* lr_selections->set_selection_mode( if_salv_c_selection_mode=>multiple ).
** lr_selections->set_selection_mode( if_salv_c_selection_mode=>row_column ).
* gr_salv->set_screen_popup(
* start_column = 10
* end_column = 110
* start_line = 2
* end_line = 25 ).
* Event handler for the button
DATA:
lo_events TYPE REF TO cl_salv_events_table,
lo_event_h TYPE REF TO lcl_event_handler.
* Event object
lo_events = gr_salv->get_event( ).
* Event handler
CREATE OBJECT lo_event_h.
* Setting up the event handler
SET HANDLER lo_event_h->on_user_command FOR lo_events.
* Generate ALV output
* It is required to execute the display method otherwise the adapter
* will not been instantiated when grabbing the alv grid
gr_salv->display( ).
DATA:
ls_child LIKE LINE OF cl_gui_container=>screen1->children.
* Variante black-adept
READ TABLE cl_gui_container=>screen1->children
INDEX 1 INTO ls_child.
** ToDo: Test auf Grid ?
* Sinnvoller Test, ob das überhaupt ein Grid ist sollte
* man hier wohl einbauen - hab ich aber gerade keine Lust dazu
gr_alv ?= ls_child.
* Got the Grid ?
IF gr_alv IS BOUND.
* Create the application object to handle the ABAP Objects Events
CREATE OBJECT gr_dragdropapp.
* Events alv control
* For Dragging
SET HANDLER gr_dragdropapp->handle_alv_drag FOR gr_alv.
* For Dropping
SET HANDLER gr_dragdropapp->handle_alv_drop FOR gr_alv.
* Define a drag & Drop behaviour for the whole grid
CREATE OBJECT gr_dragdropalv.
gv_effect = cl_dragdrop=>move + cl_dragdrop=>copy.
CALL METHOD gr_dragdropalv->add
EXPORTING
flavor = 'Line' "#EC NOTEXT
dragsrc = 'X'
droptarget = 'X'
effect = gv_effect.
* Getting the handle for drag and drop
CALL METHOD gr_dragdropalv->get_handle
IMPORTING
handle = gv_handle_alv.
* Provide handle to alv control to all rows for same drag & drop behaviour
DATA:
ls_layout TYPE lvc_s_layo.
CLEAR ls_layout.
* ls_layout-s_dragdrop-cntr_ddid = gv_handle_alv.
ls_layout-s_dragdrop-row_ddid = gv_handle_alv.
* Set the front layout of ALV
CALL METHOD gr_alv->set_frontend_layout
EXPORTING
is_layout = ls_layout.
ENDIF.
ENDFORM. "pbo
*&---------------------------------------------------------------------*
*& Form pai
*&---------------------------------------------------------------------*
FORM pai.
CASE gv_okcode.
WHEN 'BACK' OR 'EXIT' OR 'CANC'.
SET SCREEN 0.
LEAVE SCREEN.
WHEN 'ZDDW'.
MESSAGE i000 WITH 'Weiter...'. "#EC NOTEXT
SET SCREEN 0.
LEAVE SCREEN.
WHEN 'ZDDZ'.
MESSAGE i000 WITH 'Zurück...'. "#EC NOTEXT
SET SCREEN 0.
LEAVE SCREEN.
WHEN OTHERS.
SET SCREEN 0.
LEAVE SCREEN.
ENDCASE.
ENDFORM. "pai
*** Programmende ***
Code: Alles auswählen.
* Set the front layout of ALV
CALL METHOD gr_alv->set_frontend_layout
EXPORTING
is_layout = ls_layout.
gr_alv->refresh_table_display( ). " <---- Diese Zeile bitte einfügen
ENDIF.
ENDFORM. "pbo
Folgende Benutzer bedankten sich beim Autor black_adept für den Beitrag:
einar46
Code: Alles auswählen.
REPORT zkar_dragdrop_6
MESSAGE-ID ztass_util.
* Test: Drag and Drop in einem ALV
TYPES:
ty_t_sflights TYPE STANDARD TABLE OF sflights.
* Class declaration
CLASS:
lcl_objdragdropapp DEFINITION DEFERRED.
DATA:
* Test data
gt_data TYPE ty_t_sflights,
* Data declarations for alv
gr_salv TYPE REF TO cl_salv_table,
gr_alv TYPE REF TO cl_gui_alv_grid,
* Reference variable to CL_DRAGDROP
gr_dragdropalv TYPE REF TO cl_dragdrop,
gr_dragdropapp TYPE REF TO lcl_objdragdropapp,
* Declarations for handle event
gv_effect TYPE i,
gv_handle_alv TYPE i,
gv_okcode TYPE syucomm.
*-----------------------------------------------------------------------
* Class definitions and method implementation for drag and drop
*-----------------------------------------------------------------------
CLASS lcl_dragdrop DEFINITION.
PUBLIC SECTION.
DATA: wa TYPE sflights,
index TYPE i. "Index of Line to be moved
ENDCLASS. "LCL_DRAGDROP DEFINITION
*-----------------------------------------------------------------------
* Application class definition
*-----------------------------------------------------------------------
CLASS lcl_objdragdropapp DEFINITION.
PUBLIC SECTION.
METHODS:
* Handling Event Drag
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
* Application class implementation
CLASS lcl_objdragdropapp IMPLEMENTATION.
* OnDrag event is used to 'fetch' information from the drag source.
METHOD handle_alv_drag.
DATA:
lo_dataobj TYPE REF TO lcl_dragdrop,
ls_line TYPE sflights. "#EC NEEDED
* Read dragged row
READ TABLE gt_data INDEX e_row-index INTO ls_line.
* Create and fill dataobject for events ONDROP
CREATE OBJECT lo_dataobj.
* Remembering row index to move a line
MOVE e_row-index TO lo_dataobj->index.
* Store the dragged line.
READ TABLE gt_data INTO lo_dataobj->wa INDEX e_row-index.
* Assigning data object to the refering event parameter
e_dragdropobj->object = lo_dataobj.
ENDMETHOD. "HANDLE_ALV_DRAG
* Event handler for event 'OnDrop'. This event is used
* to use your dragged information in combination with your drop source.
METHOD handle_alv_drop.
DATA:
lo_dataobj TYPE REF TO lcl_dragdrop.
* Catch-Statement to ensure the drag&drop-Operation is aborted properly.
CATCH SYSTEM-EXCEPTIONS move_cast_error = 1.
lo_dataobj ?= e_dragdropobj->object.
DELETE gt_data INDEX lo_dataobj->index.
INSERT lo_dataobj->wa INTO gt_data INDEX e_row-index.
gr_salv->refresh( ).
ENDCATCH.
IF sy-subrc IS NOT INITIAL.
* If anything went wrong aborting the drag and drop operation
CALL METHOD e_dragdropobj->abort.
ENDIF.
ENDMETHOD. "HANDLE_ALV_DROP
ENDCLASS. "LCL_objdragdropapp IMPLEMENTATION
* Start of selection
START-OF-SELECTION.
* Test data
SELECT *
FROM sflights
INTO TABLE gt_data
UP TO 30 ROWS.
CALL SCREEN 0100 STARTING AT 10 5
ENDING AT 120 16.
*----------------------------------------------------------------------*
* MODULE pbo OUTPUT
*----------------------------------------------------------------------*
MODULE pbo OUTPUT.
PERFORM pbo.
ENDMODULE. "pbo OUTPUT
*----------------------------------------------------------------------*
* MODULE pai INPUT
*----------------------------------------------------------------------*
MODULE pai INPUT.
PERFORM pai.
ENDMODULE. "pai INPUT
*&---------------------------------------------------------------------*
*& Form pbo
*&---------------------------------------------------------------------*
FORM pbo.
DATA:
lr_functions TYPE REF TO cl_salv_functions_list, "#EC NEEDED
lr_selections TYPE REF TO cl_salv_selections, "#EC *
ls_child LIKE LINE OF cl_gui_container=>screen1->children,
ls_layout TYPE lvc_s_layo.
SET PF-STATUS 'D0100C'.
* New ALV Instance
TRY.
cl_salv_table=>factory(
EXPORTING
list_display = abap_false
r_container = cl_gui_container=>screen1
IMPORTING
r_salv_table = gr_salv
CHANGING
t_table = gt_data ).
CATCH cx_salv_msg. "#EC NO_HANDLER
ENDTRY.
lr_functions = gr_salv->get_functions( ).
* IF lr_functions IS BOUND.
* lr_functions->set_all( 'X' ).
* ENDIF.
** ToDo: Ich hätte gern die Markierbarkeit von Zeilen zurück!!!
** Set the selection mode of the Popup: multiple or single row selection
* lr_selections = gr_salv->get_selections( ).
* IF lr_selections IS BOUND.
* lr_selections->set_selection_mode( if_salv_c_selection_mode=>multiple ).
** lr_selections->set_selection_mode( if_salv_c_selection_mode=>row_column ).
* ENDIF.
* Generate ALV output
* It is required to execute the display method otherwise the adapter
* will not been instantiated when grabbing the alv grid
gr_salv->display( ).
* Variante black-adept
READ TABLE cl_gui_container=>screen1->children
INDEX 1 INTO ls_child.
** ToDo: Test auf Grid!!!
* Sinnvoller Test, ob das überhaupt ein Grid ist
gr_alv ?= ls_child.
* Got the Grid ?
IF gr_alv IS BOUND.
* Create the application object to handle the ABAP Objects Events
CREATE OBJECT gr_dragdropapp.
* Events alv control...
* ...for Dragging
SET HANDLER gr_dragdropapp->handle_alv_drag FOR gr_alv.
* ...for Dropping
SET HANDLER gr_dragdropapp->handle_alv_drop FOR gr_alv.
* Define a drag & drop behaviour for the whole grid
CREATE OBJECT gr_dragdropalv.
gv_effect = cl_dragdrop=>move + cl_dragdrop=>copy.
CALL METHOD gr_dragdropalv->add
EXPORTING
flavor = 'Line' "#EC NOTEXT
dragsrc = 'X'
droptarget = 'X'
effect = gv_effect.
* Getting the handle for drag and drop
CALL METHOD gr_dragdropalv->get_handle
IMPORTING
handle = gv_handle_alv.
* Provide handle to alv control to all rows for same drag & drop behaviour
CLEAR ls_layout.
ls_layout-grid_title = 'Drag and Drop - Test'. "#EC NOTEXT
ls_layout-s_dragdrop-row_ddid = gv_handle_alv.
* Set the front layout of ALV
CALL METHOD gr_alv->set_frontend_layout
EXPORTING
is_layout = ls_layout.
* Refresh the ALV
gr_alv->refresh_table_display( ).
ENDIF.
ENDFORM. "pbo
*&---------------------------------------------------------------------*
*& Form pai
*&---------------------------------------------------------------------*
FORM pai.
CASE gv_okcode.
WHEN 'BACK' OR 'EXIT' OR 'CANC'.
SET SCREEN 0.
LEAVE SCREEN.
WHEN 'ZDDW'.
MESSAGE i000 WITH 'Weiter...'. "#EC NOTEXT
SET SCREEN 0.
LEAVE SCREEN.
WHEN 'ZDDZ'.
MESSAGE i000 WITH 'Zurück...'. "#EC NOTEXT
SET SCREEN 0.
LEAVE SCREEN.
WHEN OTHERS.
SET SCREEN 0.
LEAVE SCREEN.
ENDCASE.
ENDFORM. "pai
*** Programmende ***
Code: Alles auswählen.
REPORT zkar_dragdrop_7
MESSAGE-ID salv_exception.
*=======================================================================
* Test: Drag and Drop of multiple rows in an ALV
*=======================================================================
TYPES:
tt_sflights TYPE STANDARD TABLE OF sflights.
* Class declaration
CLASS:
lcl_dragdropobj DEFINITION DEFERRED.
DATA:
* Test data
gt_datas TYPE tt_sflights,
* Data declarations for ALV
gr_salv TYPE REF TO cl_salv_table,
gr_alv TYPE REF TO cl_gui_alv_grid,
* Reference variable to CL_DRAGDROP
gr_dragdropalv TYPE REF TO cl_dragdrop,
gr_dragdropobj TYPE REF TO lcl_dragdropobj,
* Ok-Code for dynpro
gv_okcode TYPE syucomm.
*-----------------------------------------------------------------------
* Class definitions and method implementation for drag and drop
*-----------------------------------------------------------------------
CLASS lcl_dragdrop DEFINITION.
PUBLIC SECTION.
DATA:
* Table of data of (m)ultiple (r)ow(s)
gt_datamrs TYPE tt_sflights,
* Table of (i)ndices of multiple row(s)
gt_datais TYPE STANDARD TABLE OF sytabix.
ENDCLASS. "LCL_DRAGDROP DEFINITION
*-----------------------------------------------------------------------
* Application class definition
*-----------------------------------------------------------------------
CLASS lcl_dragdropobj DEFINITION.
PUBLIC SECTION.
METHODS:
* Handling event DRAG
on_drag
FOR EVENT ondrag
OF cl_gui_alv_grid
IMPORTING e_row e_column e_dragdropobj,
* Handling event DROP
on_drop
FOR EVENT ondrop
OF cl_gui_alv_grid
IMPORTING e_row e_column e_dragdropobj.
ENDCLASS. "LCL_DRAGDROPOBJ DEFINITION
*-----------------------------------------------------------------------
* Application class implementation
*-----------------------------------------------------------------------
CLASS lcl_dragdropobj IMPLEMENTATION.
* OnDrag event is used to 'fetch' information from the drag source
METHOD on_drag.
DATA:
lt_index_rows TYPE lvc_t_row,
ls_index_row TYPE REF TO lvc_s_row,
lo_dataobj TYPE REF TO lcl_dragdrop,
ls_data TYPE REF TO sflights. "#EC NEEDED
* Drag multiple rows
* Get the selected rows
CALL METHOD gr_alv->get_selected_rows
IMPORTING
et_index_rows = lt_index_rows.
IF lt_index_rows IS INITIAL.
* No selected row, then we set a selected row
INSERT INITIAL LINE INTO TABLE lt_index_rows
REFERENCE INTO ls_index_row.
IF sy-subrc IS INITIAL.
ls_index_row->index = e_row-index.
ENDIF.
ENDIF.
* Create and fill dataobject for events ONDROP
CREATE OBJECT lo_dataobj.
CLEAR lo_dataobj->gt_datamrs.
CLEAR lo_dataobj->gt_datais.
* Read the dragged rows
LOOP AT lt_index_rows REFERENCE INTO ls_index_row.
READ TABLE gt_datas REFERENCE INTO ls_data
INDEX ls_index_row->index.
IF sy-subrc IS INITIAL.
* Store the dragged rows and indices
APPEND ls_data->* TO lo_dataobj->gt_datamrs.
APPEND ls_index_row->index TO lo_dataobj->gt_datais.
ENDIF.
ENDLOOP.
* Assigning data object to the refering event parameter
e_dragdropobj->object = lo_dataobj.
ENDMETHOD. "ON_DRAG
* Event handler for event 'OnDrop'. This event is used
* to use your dragged information in combination with your drop source.
METHOD on_drop.
DATA:
lo_dataobj TYPE REF TO lcl_dragdrop,
lt_data_olds TYPE tt_sflights,
ls_data_old TYPE REF TO sflights,
lt_data_news TYPE tt_sflights,
ls_data_new TYPE REF TO sflights,
ls_datamr TYPE REF TO sflights,
lv_datai TYPE sytabix,
lv_tabix TYPE sytabix,
lv_index TYPE sytabix,
lv_position TYPE i,
lv_count TYPE i.
* Drop multiple rows
* REMARK: The selected rows are always positioned
* behind!!! the target position
* EXCEPTION: When the selected rows should be positioned in front
* of the first row, then it has to be done in two steps
* Catch-Statement to ensure the drag&drop-Operation is aborted properly
CATCH SYSTEM-EXCEPTIONS move_cast_error = 1.
lo_dataobj ?= e_dragdropobj->object.
DESCRIBE TABLE lo_dataobj->gt_datamrs LINES lv_count.
IF lv_count IS NOT INITIAL.
* Save the old table
CLEAR lt_data_olds.
lt_data_olds = gt_datas.
* Check and correct the target position
lv_position = e_row-index.
LOOP AT lo_dataobj->gt_datais INTO lv_datai.
IF lv_datai < e_row-index.
lv_position = lv_position - 1.
ENDIF.
ENDLOOP.
* Delete the selected rows in the old table
LOOP AT lo_dataobj->gt_datamrs REFERENCE INTO ls_datamr.
DELETE TABLE lt_data_olds FROM ls_datamr->*.
ENDLOOP.
* Build the new table
CLEAR lt_data_news.
* Insert old data only up to the target position
CLEAR lv_index.
LOOP AT lt_data_olds REFERENCE INTO ls_data_old.
lv_tabix = sy-tabix.
lv_index = lv_index + 1.
IF lv_index <= lv_position.
INSERT INITIAL LINE INTO TABLE lt_data_news
REFERENCE INTO ls_data_new.
IF sy-subrc IS INITIAL.
ls_data_new->* = ls_data_old->*.
DELETE lt_data_olds INDEX lv_tabix.
ENDIF.
ENDIF.
ENDLOOP.
* Insert the selected rows in the new table
LOOP AT lo_dataobj->gt_datamrs REFERENCE INTO ls_datamr.
INSERT INITIAL LINE INTO TABLE lt_data_news
REFERENCE INTO ls_data_new.
IF sy-subrc IS INITIAL.
ls_data_new->* = ls_datamr->*.
ENDIF.
ENDLOOP.
* Insert the rest of the old table in the new table
LOOP AT lt_data_olds REFERENCE INTO ls_data_old.
INSERT INITIAL LINE INTO TABLE lt_data_news
REFERENCE INTO ls_data_new.
IF sy-subrc IS INITIAL.
ls_data_new->* = ls_data_old->*.
ENDIF.
ENDLOOP.
IF lt_data_news IS NOT INITIAL.
CLEAR gt_datas.
gt_datas = lt_data_news.
ENDIF.
* Refresh the ALV
gr_salv->refresh( ).
ENDIF.
ENDCATCH.
* If anything went wrong aborting the drag and drop operation
IF sy-subrc IS NOT INITIAL.
CALL METHOD e_dragdropobj->abort.
ENDIF.
ENDMETHOD. "ON_DROP
ENDCLASS. "LCL_DRAGDROPOBJ IMPLEMENTATION
* Start of selection
START-OF-SELECTION.
* Test data
SELECT *
FROM sflights
INTO TABLE gt_datas
UP TO 30 ROWS. "#EC CI_NOWHERE
END-OF-SELECTION.
* Call the dynpro
* REMARK: The dynpro contains no container and is a normal dynpro!!!
CALL SCREEN 0100 STARTING AT 5 2
ENDING AT 120 15.
*----------------------------------------------------------------------*
* MODULE pbo OUTPUT
*----------------------------------------------------------------------*
MODULE pbo OUTPUT.
PERFORM pbo.
ENDMODULE. "pbo OUTPUT
*----------------------------------------------------------------------*
* MODULE pai INPUT
*----------------------------------------------------------------------*
MODULE pai INPUT.
PERFORM pai.
ENDMODULE. "pai INPUT
*----------------------------------------------------------------------*
* Form pbo
*----------------------------------------------------------------------*
FORM pbo.
DATA:
* REMARK: screen1, ...2, ...3 describes the level of an ALV
ls_child LIKE LINE OF cl_gui_container=>screen1->children,
ls_layout TYPE lvc_s_layo,
ls_stable TYPE lvc_s_stbl,
* Declarations for handle event drag and drop
lv_handle_alv TYPE i,
lv_effect TYPE i.
* Set status
* REMARK: The status is copied from the program
* SALV_DEMO_TABLE_FORM_EVENTS and was extended
* by two buttons ZDDW und ZDDZ.
* See Form-routine PAI for testing the position of the
* own buttons.
SET PF-STATUS 'D0100C'.
* New ALV instance
TRY.
cl_salv_table=>factory(
EXPORTING
list_display = abap_false
r_container = cl_gui_container=>screen1
IMPORTING
r_salv_table = gr_salv
CHANGING
t_table = gt_datas ).
CATCH cx_salv_msg. "#EC NO_HANDLER
ENDTRY.
* Generate ALV output
gr_salv->display( ).
* Get the reference of the class CL_GUI_ALV_GRID
** (Variant of abapforum-user black-adept)
* READ TABLE cl_gui_container=>screen1->children
* INDEX 1 INTO ls_child.
LOOP AT cl_gui_container=>screen1->children INTO ls_child.
TRY.
gr_alv ?= ls_child.
EXIT.
CATCH cx_sy_move_cast_error.
CONTINUE.
ENDTRY.
ENDLOOP.
* Got the reference of the grid ?
IF gr_alv IS BOUND.
* Create the application object to handle the ABAP objects events
CREATE OBJECT gr_dragdropobj.
* Events ALV control...
* ...for dragging
SET HANDLER gr_dragdropobj->on_drag FOR gr_alv.
* ...for dropping
SET HANDLER gr_dragdropobj->on_drop FOR gr_alv.
* Define a drag & drop behaviour for the whole grid
CREATE OBJECT gr_dragdropalv.
lv_effect = cl_dragdrop=>move + cl_dragdrop=>copy.
CALL METHOD gr_dragdropalv->add
EXPORTING
flavor = 'Line' "#EC NOTEXT
dragsrc = 'X'
droptarget = 'X'
effect = lv_effect.
* Getting the handle for drag and drop
CALL METHOD gr_dragdropalv->get_handle
IMPORTING
handle = lv_handle_alv.
* Get the old front layout of ALV
CALL METHOD gr_alv->get_frontend_layout
IMPORTING
es_layout = ls_layout.
* Grid title
ls_layout-grid_title = 'Drag and Drop - Test'. "#EC NOTEXT
* Small title font
ls_layout-smalltitle = 'X'.
* Allow to select multiple rows
ls_layout-sel_mode = 'A'.
* Optimize column width
ls_layout-cwidth_opt = 'X'.
* Off horizontal grid lines
ls_layout-no_hgridln = 'X'.
* Zebra crossing
ls_layout-zebra = 'X'.
* Provide handle to alv control to all rows
* for same drag and drop behaviour
ls_layout-s_dragdrop-row_ddid = lv_handle_alv.
* Set the new front layout of ALV
CALL METHOD gr_alv->set_frontend_layout
EXPORTING
is_layout = ls_layout.
* Refresh the ALV
ls_stable-row = 'X'.
ls_stable-col = 'X'.
CALL METHOD gr_alv->refresh_table_display
EXPORTING
i_soft_refresh = 'X'
is_stable = ls_stable.
ENDIF.
ENDFORM. "pbo
*----------------------------------------------------------------------*
* Form pai
*----------------------------------------------------------------------*
FORM pai.
DATA:
lv_okcode TYPE syucomm.
lv_okcode = gv_okcode.
CLEAR gv_okcode.
CASE lv_okcode.
WHEN 'BACK' OR 'EXIT' OR 'CANC'.
* Destroy the references before leaving the ALV
IF gr_salv IS NOT INITIAL.
CALL METHOD gr_salv->close_screen.
FREE gr_salv.
ENDIF.
IF gr_alv IS NOT INITIAL.
CALL METHOD gr_alv->free
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2.
CLEAR gr_alv.
ENDIF.
LEAVE TO SCREEN 0.
* Only for test to see the position of the button (see status)
WHEN 'ZDDW'.
* Destroy the references before leaving the ALV
IF gr_salv IS NOT INITIAL.
CALL METHOD gr_salv->close_screen.
FREE gr_salv.
ENDIF.
IF gr_alv IS NOT INITIAL.
CALL METHOD gr_alv->free
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2.
CLEAR gr_alv.
ENDIF.
MESSAGE i000 WITH 'More...'. "#EC NOTEXT
LEAVE TO SCREEN 0.
* Only for test to see the position of the button (see status)
WHEN 'ZDDZ'.
* Destroy the references before leaving the ALV
IF gr_salv IS NOT INITIAL.
CALL METHOD gr_salv->close_screen.
FREE gr_salv.
ENDIF.
IF gr_alv IS NOT INITIAL.
CALL METHOD gr_alv->free
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2.
CLEAR gr_alv.
ENDIF.
MESSAGE i000 WITH 'Back...'. "#EC NOTEXT
LEAVE TO SCREEN 0.
WHEN OTHERS.
* Destroy the references before leaving the ALV
IF gr_salv IS NOT INITIAL.
CALL METHOD gr_salv->close_screen.
FREE gr_salv.
ENDIF.
IF gr_alv IS NOT INITIAL.
CALL METHOD gr_alv->free
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2.
CLEAR gr_alv.
ENDIF.
LEAVE TO SCREEN 0.
ENDCASE.
ENDFORM. "pai
*** Program end ***