简体   繁体   中英

CL_GUI_ALV_GRID: Drag and Drop in an empty grid?

I implemented a drag and drop feature in one of my reports and it actually works fine when both grids are filled with data. When one of the grid is empty the drop feature is disabled. How can I change this?

I use the cl_gui_alv_grid class.

You may use the component cntr_ddid which represents the rest of the ALV grid control:

go_table->set_table_for_first_display(
    EXPORTING
        is_layout = VALUE #(
            s_dragdrop = VALUE #(
                cntr_ddid  = l_dragdrop_handle ) )
    ...

Fully-working example (you may drag & drop the rows from the top table to the bottom table, initially empty -- the top table takes rows from the table SCARR that you may need to fill by calling the program SAPBC_DATA_GENERATOR --):

CLASS lcl_app DEFINITION.
  PUBLIC SECTION.
    METHODS main.
    METHODS on_drag FOR EVENT ondrag OF cl_gui_alv_grid IMPORTING es_row_no e_dragdropobj.
    METHODS on_drop FOR EVENT ondrop OF cl_gui_alv_grid.
  PRIVATE SECTION.
    DATA: go_split     TYPE REF TO cl_gui_easy_splitter_container,
          go_table1    TYPE REF TO cl_gui_alv_grid,
          go_table2    TYPE REF TO cl_gui_alv_grid,
          go_dragdrop1 TYPE REF TO cl_dragdrop,
          go_dragdrop2 TYPE REF TO cl_dragdrop,
          gt_scarr1    TYPE TABLE OF scarr,
          gt_scarr2    TYPE TABLE OF scarr,
          gs_scarr     TYPE scarr.
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
  METHOD main.
    DATA: l_effect           TYPE i,
          l_dragdrop_handle1 TYPE i,
          l_dragdrop_handle2 TYPE i.

    go_split = NEW cl_gui_easy_splitter_container( parent = cl_gui_container=>screen0 ).
    go_table1 = NEW cl_gui_alv_grid( i_parent = go_split->top_left_container ).
    go_table2 = NEW cl_gui_alv_grid( i_parent = go_split->bottom_right_container ).
    go_dragdrop1 = NEW cl_dragdrop( ).
    go_dragdrop1->add(
        flavor     = 'DD1'
        dragsrc    = abap_true
        droptarget = abap_false
        effect     = cl_dragdrop=>move ).
    go_dragdrop2 = NEW cl_dragdrop( ).
    go_dragdrop2->add(
        flavor     = 'DD1'
        dragsrc    = abap_false
        droptarget = abap_true
        effect     = cl_dragdrop=>move ).

    go_dragdrop1->get_handle( IMPORTING handle = l_dragdrop_handle1 ).
    go_dragdrop2->get_handle( IMPORTING handle = l_dragdrop_handle2 ).

    SELECT * FROM scarr INTO TABLE gt_scarr1.

    go_table1->set_table_for_first_display(
          EXPORTING
            i_structure_name = 'SCARR'
            is_layout = VALUE #(
                s_dragdrop = VALUE #(
                    row_ddid  = l_dragdrop_handle1 ) )
          CHANGING
            it_outtab = gt_scarr1 ).
    go_table2->set_table_for_first_display(
          EXPORTING
            i_structure_name = 'SCARR'
            is_layout = VALUE #(
                s_dragdrop = VALUE #(
                    cntr_ddid = l_dragdrop_handle2 ) )
          CHANGING
            it_outtab = gt_scarr2 ).

    SET HANDLER on_drag FOR go_table1.
    SET HANDLER on_drop FOR go_table2.
  ENDMETHOD.

  METHOD on_drag.
    DATA: lt_row        TYPE lvc_t_roid.
    FIELD-SYMBOLS:
          <ls_row>      TYPE lvc_s_roid.
    go_table1->get_selected_rows( IMPORTING et_row_no = lt_row ).
    READ TABLE gt_scarr1 INDEX lt_row[ 1 ]-row_id INTO gs_scarr.
    e_dragdropobj->object = me. " dummy to trigger ON_DROP
  ENDMETHOD.

  METHOD on_drop.
    APPEND gs_scarr TO gt_scarr2.
    go_table2->refresh_table_display( is_stable = VALUE #( col = 'X' row = 'X' ) ).
  ENDMETHOD.
ENDCLASS.

PARAMETERS p_dummy.

AT SELECTION-SCREEN OUTPUT.
  NEW lcl_app( )->main( ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM