메뉴 건너뛰기

SAP 한국 커뮤니티

 z15_21_drag

sapjoy 2022.02.24 16:39 조회 수 : 307

REPORT  z15_21_drag.

DATA: BEGIN OF gt_itab OCCURS 0.
        INCLUDE STRUCTURE sflight.
DATA: linecolor(4) TYPE c.
DATA: END OF gt_itab.

DATA : gt_sflight LIKE TABLE OF gt_itab,
       gs_sflight LIKE LINE OF gt_sflight.


DATA : con1_ref    TYPE REF TO    cl_gui_custom_container.
DATA : g_grid      TYPE REF TO    cl_gui_alv_grid.
DATA : gs_variant  TYPE disvariant.
DATA : gs_layout   TYPE lvc_s_layo.
DATA : gt_toolbar  TYPE ui_functions.
DATA : gt_sort     TYPE lvc_t_sort.
DATA : gt_fieldcat TYPE lvc_t_fcat.
DATA : gt_modified_rows TYPE STANDARD TABLE OF sflight.


DATA : ok_code  TYPE sy-ucomm.

CLASS lcl_application DEFINITION DEFERRED.
DATA: g_application TYPE REF TO lcl_application.
DATA: g_behaviour_alv TYPE REF TO cl_dragdrop.

TYPE-POOLS: icon.


CLASS lcl_dragdropobj DEFINITION.
  PUBLIC SECTION.
    DATA: wa_sflight LIKE gs_sflight,
          index TYPE i.   "Index of Line to be moved or copied.

ENDCLASS.

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 LIKE gs_sflight.

* Read dragged row
    READ TABLE gt_sflight 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_sflight INTO dataobj->wa_sflight 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.

    DATA: lt_row_no TYPE lvc_t_roid,                        "YI3K164391
          ls_row_no TYPE lvc_s_roid.

* Refresh Alv Grid Control without scrolling
    stable-row = 'X'.
    stable-col = 'X'.

*!!!
* Very important: '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).

*      dataobj->wa_sflight-carrid = 'ZZ'.

      IF e_dragdropobj->effect EQ cl_dragdrop=>copy.
        INSERT dataobj->wa_sflight INTO gt_sflight INDEX e_row-index.

*select current row (target)
        ls_row_no-row_id = e_row-index.                     "YI3K164391
        APPEND ls_row_no TO lt_row_no.

      ELSE.
        DELETE gt_sflight INDEX dataobj->index.
        INSERT dataobj->wa_sflight INTO gt_sflight INDEX e_row-index.

*select current row (target)
        ls_row_no-row_id = e_row-index.                     "YI3K164391
        APPEND ls_row_no TO lt_row_no.

      ENDIF.

      CALL METHOD g_grid->refresh_table_display
           EXPORTING i_soft_refresh = 'X'
                     is_stable = stable.

* set selections after refresh !
      CALL METHOD g_grid->set_selected_rows                 "YI3K164391
        EXPORTING
           it_row_no                =  lt_row_no.

    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.



*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver DEFINITION.
  PUBLIC SECTION.
    METHODS : handle_toolbar
     FOR EVENT toolbar OF cl_gui_alv_grid
     IMPORTING e_object e_interactive.

    METHODS : handle_command
     FOR EVENT user_command OF cl_gui_alv_grid
     IMPORTING e_ucomm.


    METHODS:
     handle_data_changed
      FOR EVENT data_changed OF cl_gui_alv_grid
       IMPORTING er_data_changed.


ENDCLASS.                    "lcl_event_receiver DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_event_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_event_receiver IMPLEMENTATION.
  METHOD handle_toolbar.

    DATA: ls_toolbar  TYPE stb_button.

    CLEAR ls_toolbar.
    ls_toolbar-butn_type = 3.
    APPEND ls_toolbar    TO e_object->mt_toolbar.

    CLEAR ls_toolbar.
    ls_toolbar-function  = 'RESH'.
    ls_toolbar-icon      = icon_refresh.
    ls_toolbar-quickinfo = 'Refresh'.
    ls_toolbar-text      = ' '.
    ls_toolbar-disabled  = ' '.
    APPEND ls_toolbar    TO e_object->mt_toolbar.

  ENDMETHOD.                    "handle_toolbar

  METHOD handle_command.
    DATA : l_scroll TYPE lvc_s_stbl.

    CASE e_ucomm.
      WHEN 'RESH'.
        SELECT * FROM sflight INTO TABLE gt_sflight.
        l_scroll-row = 'X'.
        l_scroll-col = 'X'.

        CALL METHOD g_grid->refresh_table_display
           EXPORTING
            i_soft_refresh = ''
            is_stable = l_scroll.
    ENDCASE.
  ENDMETHOD.                    "handle_command

  METHOD handle_data_changed.
    DATA:  ls_sflight TYPE sflight,
     ls_outtab LIKE LINE OF gt_sflight.
    FIELD-SYMBOLS: <fs> TYPE table.
    ASSIGN er_data_changed->mp_mod_rows->* TO <fs>.
    LOOP AT <fs> INTO ls_outtab.
      MOVE-CORRESPONDING ls_outtab TO ls_sflight.
      APPEND ls_sflight TO gt_modified_rows.
    ENDLOOP.
  ENDMETHOD.                    "handle_data_changed


ENDCLASS.                    "lcl_event_receiver IMPLEMENTATION

*
*PARAMETERS: p_vari LIKE disvariant-variant.
*
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_vari.
*   gs_variant-report = sy-repid.
*   CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
*      EXPORTING
*       is_variant = gs_variant
*       i_save = 'A'
*      IMPORTING
*       es_variant = gs_variant
*      EXCEPTIONS
*       not_found = 1
*       program_error = 2
*       OTHERS = 3.
*
*  IF sy-subrc EQ 0.
*     p_vari = gs_variant-variant.
*  ENDIF.

START-OF-SELECTION.

  SELECT * FROM sflight
       INTO TABLE gt_sflight  UP TO 100 ROWS.

  LOOP AT gt_sflight INTO gs_sflight.
    CASE gs_sflight-carrid.
      WHEN 'AA'. " Blue
        gs_sflight-linecolor = 'C100'..
      WHEN 'AZ'. " Yellow.
        gs_sflight-linecolor = 'C300'.
      WHEN 'DL'. " Green.
        gs_sflight-linecolor = 'C500'.
    ENDCASE.
    MODIFY gt_sflight FROM gs_sflight.
  ENDLOOP.



  CALL SCREEN 100.


*&---------------------------------------------------------------------*
*&      Module  INIT_CON  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE init_con OUTPUT.
  IF con1_ref IS INITIAL.
    CREATE OBJECT con1_ref
     EXPORTING
       container_name = 'CON1'.

    CREATE OBJECT g_grid
        EXPORTING
         i_parent = con1_ref.
  ENDIF.

  CREATE OBJECT g_application.
* Events alv control
  SET HANDLER g_application->handle_alv_drag FOR g_grid.
  SET HANDLER g_application->handle_alv_drop FOR g_grid.


  DATA: effect TYPE i,
        handle_alv TYPE i.


* 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.

  GS_LAYOUT-S_DRAGDROP-ROW_DDID = HANDLE_ALV.

  gs_variant-report = sy-repid.
  gs_variant-username = sy-uname.

  PERFORM setting_layout CHANGING gs_layout.
  PERFORM setting_toolbar.
  PERFORM setting_sort.
  PERFORM getting_catalog.
  PERFORM setting_catalog.


  DATA :  event_receiver TYPE REF TO lcl_event_receiver.

  CREATE OBJECT event_receiver.
  SET HANDLER event_receiver->handle_toolbar FOR g_grid.
  SET HANDLER event_receiver->handle_data_changed FOR g_grid.

  CALL METHOD g_grid->set_table_for_first_display
     EXPORTING
*    is_variant = gs_variant
      i_save = 'A'
    i_default = ''
    is_layout = gs_layout
    it_toolbar_excluding = gt_toolbar
     CHANGING
      it_outtab = gt_sflight
    it_fieldcatalog = gt_fieldcat
    it_sort = gt_sort.

*CALL METHOD GRID1->SET_READY_FOR_INPUT
*   EXPORTING I_READY_FOR_INPUT = 0.


ENDMODULE.                 " INIT_CON  OUTPUT
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

  CASE sy-ucomm.
    WHEN 'BACK'.
      LEAVE PROGRAM.
    WHEN 'SAVE'.
      DATA: l_valid TYPE c.
      CALL METHOD g_grid->check_changed_data
         IMPORTING
          e_valid = l_valid.
      IF l_valid IS NOT INITIAL.
        PERFORM update_database.
      ENDIF.
  ENDCASE.



ENDMODULE.                 " USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS '100_STATUS'.
*  SET TITLEBAR 'xxx'.

ENDMODULE.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Form  SETTING_LAYOUT
*&---------------------------------------------------------------------*
FORM setting_layout   CHANGING  p_layout TYPE lvc_s_layo.

  p_layout-cwidth_opt = ''.  "칼럼 길이 자동조절

  p_layout-grid_title = 'Coloring Row Test'.
  "타이틀 바

  p_layout-sel_mode = 'B'.  "선택모드 : 멀티 선택 가능

  p_layout-zebra      = ''.  "Sripe 속성 지정
  p_layout-info_fname = 'LINECOLOR'.
ENDFORM.                    " setting_layout
*&---------------------------------------------------------------------*
*&      Form  SETTING_TOOLBAR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM setting_toolbar .
  DATA: l_exclude TYPE ui_func.

  l_exclude =  cl_gui_alv_grid=>mc_fc_save_variant.
  APPEND l_exclude TO gt_toolbar.

  l_exclude = cl_gui_alv_grid=>mc_fc_maintain_variant.
  APPEND l_exclude TO gt_toolbar.

ENDFORM.                    " SETTING_TOOLBAR
*&---------------------------------------------------------------------*
*&      Form  SETTING_SORT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM setting_sort .
  DATA : ls_sort TYPE lvc_s_sort.

  ls_sort-spos = '1'.
  ls_sort-fieldname = 'CARRID'.
  ls_sort-up = 'X'.
  ls_sort-subtot = 'X'.
  APPEND ls_sort TO gt_sort.

ENDFORM.                    " SETTING_SORT
*&---------------------------------------------------------------------*
*&      Form  SETTING_CATALOG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM setting_catalog .
  FIELD-SYMBOLS: <ls_fcat> TYPE lvc_s_fcat.

  LOOP AT gt_fieldcat ASSIGNING <ls_fcat>.

    IF <ls_fcat>-fieldname EQ 'FLDATE'.
      <ls_fcat>-edit_mask = '____/__/__'.
    ENDIF.

    IF <ls_fcat>-fieldname EQ 'CURRENCY'.
      <ls_fcat>-just = 'R'.
    ENDIF.

  ENDLOOP.

ENDFORM.                    " SETTING_CATALOG
*&---------------------------------------------------------------------*
*&      Form  GETTING_CATALOG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM getting_catalog .

  DATA : lt_fieldcat TYPE kkblo_t_fieldcat.

  CALL FUNCTION 'K_KKB_FIELDCAT_MERGE'
     EXPORTING
    i_callback_program = sy-repid
      i_tabname = 'GT_ITAB'
    i_inclname = sy-repid
     CHANGING
      ct_fieldcat = lt_fieldcat[].

  IF sy-subrc EQ 0.
    CALL FUNCTION 'LVC_TRANSFER_FROM_KKBLO'
       EXPORTING
        it_fieldcat_kkblo = lt_fieldcat[]
       IMPORTING
        et_fieldcat_lvc = gt_fieldcat[].
  ENDIF.


ENDFORM.                    " GETTING_CATALO
*&---------------------------------------------------------------------*
*&      Form  UPDATE_DATABASE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM update_database .
  MODIFY  sflight FROM TABLE gt_modified_rows.
  IF sy-subrc EQ 0.
    MESSAGE 'SAVE OK' TYPE 'I'.
  ENDIF.

ENDFORM.                    " UPDATE_DATABASE

번호 제목 글쓴이 날짜 조회 수
671 직원 데이터 [1] file sapjoy 2024.07.02 241
670 REPORT Z99_24. sapjoy 2024.05.21 83
669 REPORT Z06_01. sapjoy 2024.04.21 60
668 REPORT  z05_36 sapjoy 2023.02.08 270
667 API sapjoy 2022.11.03 351
»  z15_21_drag sapjoy 2022.02.24 307
665 Z18_06_DRAGCLASS sapjoy 2022.02.23 334
664 REPORT Z21_01 sapjoy 2015.10.28 2276
663 subroutine pool sapjoy 2013.11.16 3314
662 REPORT Z20_01. sapjoy 2012.08.02 5097
661 REPORT Z19_02. sapjoy 2012.08.02 3953
660 REPORT z19_01. sapjoy 2012.08.02 3559
659 REPORT z18_06. sapjoy 2012.08.02 3195
658 REPORT z18_05 sapjoy 2012.08.02 3384
657 REPORT z18_04 . sapjoy 2012.08.02 3388
656 REPORT z18_03. sapjoy 2012.08.02 4598
655 REPORT z18_02. sapjoy 2012.08.02 3124
654 REPORT z18_01. sapjoy 2012.08.02 3152
653 REPORT z17_23 sapjoy 2012.08.02 2538
652 REPORT z17_22 sapjoy 2012.08.02 2307