Drag and Drop Double List Control
Have you ever struggled to create 2 ALV or List Controls that you can drag and drop to and from?
Or you just want to have a singe list where you want to be able to re-arrange the entries to a specific order using buttons or simple drag and drop?
I have struggled in the past to find a quick solution, but I always end up with complex controls and spending hours, if not days to get a solution to a simple problem!
I have written a simple report, which I will explain step-by-step:
Data Declarations:
REPORT z_drag_drop.
TYPES: BEGIN OF gty_list,
item TYPE text20,
END OF gty_list.
DATA: gr_source_container TYPE REF TO cl_gui_custom_container,
gr_target_container TYPE REF TO cl_gui_custom_container,
gr_source_list TYPE REF TO cl_alv_dd_listbox,
gr_target_list TYPE REF TO cl_alv_dd_listbox,
gr_double_list TYPE REF TO cl_alv_dd_double_listbox.
DATA: gt_source_data TYPE TABLE OF gty_list,
gt_target_data TYPE TABLE OF gty_list,
gt_source_fcat TYPE lvc_t_fcat,
gt_target_fcat TYPE lvc_t_fcat.
DATA: gv_okcode TYPE syucomm.
DATA: gv_okcode TYPE syucofdmm.
- CL_GUI_CUSTOM_CONTAINER
- This is the container for the ALV list box
- CL_ALV_DD_LISTBOX
- This controller has the ability to have the contents be rearranged with buttons, drag and drop etc.
- CL_ALV_DD_DOUBLE_LISTBOX
- This controller “links” 2 CL_ALV_DD_LISTBOX with the ability to drag and drop contents between them
Create a screen with 2 custom containers:
- GR_TARGET_CONTAINER
- This will display the “Target” data
- GR_SOURCE_CONTAINER
- This will display the “Source” data
PBO Module for the created screen (This is the fun part):
MODULE status_0100 OUTPUT.
SET PF-STATUS 'PF0100'.
SET TITLEBAR 'TI0100'.
DATA: ls_layout TYPE lvc_s_layo,
ls_fcat TYPE lvc_s_fcat,
ls_data TYPE gty_list.
IF gr_source_container IS INITIAL.
CREATE OBJECT gr_source_container
EXPORTING
container_name = 'GR_SOURCE_CONTAINER'
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.
CREATE OBJECT gr_source_list
EXPORTING
i_grid_style = 1
i_parent = gr_source_container
i_allow_empty = abap_true
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4.
CLEAR ls_layout.
ls_layout-grid_title = 'Source List'.
ls_layout-sgl_clk_hd = 'X'.
ls_layout-no_merging = 'X'.
ls_data-item = 'Item 4'.
APPEND ls_data TO gt_source_data.
ls_data-item = 'Item 5'.
APPEND ls_data TO gt_source_data.
ls_data-item = 'Item 6'.
APPEND ls_data TO gt_source_data.
ls_fcat-fieldname = 'ITEM'.
ls_fcat-coltext = 'Items'.
APPEND ls_fcat TO gt_source_fcat.
gr_source_list->set_table_for_first_display(
EXPORTING
is_layout = ls_layout
CHANGING
it_outtab = gt_source_data
it_fieldcatalog = gt_source_fcat
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3 ).
ENDIF.
IF gr_target_container IS INITIAL.
CREATE OBJECT gr_target_container
EXPORTING
container_name = 'GR_TARGET_CONTAINER'
EXCEPTIONS
cntl_error = 1
cntl_system_error = 2
create_error = 3
lifetime_error = 4
lifetime_dynpro_dynpro_link = 5.
CREATE OBJECT gr_target_list
EXPORTING
i_grid_style = 0
i_parent = gr_target_container
i_allow_empty = abap_true
EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4.
CLEAR ls_layout.
ls_layout-grid_title = 'Target List'.
ls_layout-sgl_clk_hd = 'X'.
ls_layout-no_merging = 'X'.
ls_data-item = 'Item 1'.
APPEND ls_data TO gt_target_data.
ls_data-item = 'Item 2'.
APPEND ls_data TO gt_target_data.
ls_data-item = 'Item 3'.
APPEND ls_data TO gt_target_data.
ls_fcat-fieldname = 'ITEM'.
ls_fcat-coltext = 'Items'.
APPEND ls_fcat TO gt_target_fcat.
gr_target_list->set_table_for_first_display(
EXPORTING
is_layout = ls_layout
CHANGING
it_outtab = gt_target_data
it_fieldcatalog = gt_target_fcat
EXCEPTIONS
invalid_parameter_combination = 1
program_error = 2
too_many_lines = 3 ).
ENDIF.
IF gr_double_list IS INITIAL.
CREATE OBJECT gr_double_list
EXPORTING
i_grid2 = gr_target_list
i_grid1 = gr_source_list.
ENDIF.
ENDMODULE.
Results:
Now you have two ALV List Controls that you can drag and drop between.
Be the first to leave a comment
You must be Logged on to comment or reply to a post.