SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

TREE Report



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Mon Sep 10, 2007 7:56 pm    Post subject: TREE Report Reply with quote

Code:
****** CLASS DEFINITION
CLASS lcl_application DEFINITION.
  PUBLIC SECTION.
    METHODS:
      handle_node_double_click
        FOR EVENT node_double_click
        OF cl_gui_list_tree
        IMPORTING node_key,
      handle_expand_no_children
        FOR EVENT expand_no_children
        OF cl_gui_list_tree
        IMPORTING node_key,
      handle_item_double_click
        FOR EVENT item_double_click
        OF cl_gui_list_tree
        IMPORTING node_key item_name,
      handle_button_click
        FOR EVENT button_click
        OF cl_gui_list_tree
        IMPORTING node_key item_name,
      handle_link_click
        FOR EVENT link_click
        OF cl_gui_list_tree
        IMPORTING node_key item_name,
      handle_checkbox_change
        FOR EVENT checkbox_change
        OF cl_gui_list_tree
        IMPORTING node_key item_name checked.
ENDCLASS.

****** CLASS IMPLEMENTATION
CLASS lcl_application IMPLEMENTATION.

  METHOD  handle_node_double_click.
    " this method handles the node double click event of the tree
    " control instance

    " show the key of the double clicked node in a dynpro field
    READ TABLE itab_data WITH KEY node_key = node_key INTO wa_data.
* You now have the data to do anything you wish
* ….
  ENDMETHOD.

  METHOD  handle_item_double_click.
    " this method handles the item double click event of the tree
    " control instance. You have variables node_key and item_name

    " show the key of the node and the name of the item
    " of the double clicked item in a dynpro field
    READ TABLE itab_data WITH KEY node_key = node_key INTO wa_data.
* You now have the data to do anything you wish
* ….
  ENDMETHOD.

  METHOD  handle_link_click.
    " this method handles the link click event of the tree
    " control instance

    " show the key of the node and the name of the item
    " of the clicked link in a dynpro field
    g_node_key = node_key.
    g_item_name = item_name.
  ENDMETHOD.

  METHOD  handle_button_click.
    " this method handles the button click event of the tree
    " control instance

    " show the key of the node and the name of the item
    " of the clicked button in a dynpro field
    g_node_key = node_key.
    g_item_name = item_name.
  ENDMETHOD.

  METHOD  handle_checkbox_change.
    " this method handles the checkbox_change event of the tree
    " control instance

    " show the key of the node and the name of the item
    " of the clicked checkbox in a dynpro field
    g_node_key = node_key.
    g_item_name = item_name.
  ENDMETHOD.


  METHOD handle_expand_no_children.
    DATA: node_table TYPE treev_ntab,
          node TYPE treev_node,
          item_table TYPE item_table_type,
          item TYPE mtreeitm.

* show the key of the expanded node in a dynpro field
    g_node_key = node_key.

    IF node_key = 'Child2'.                                 "#EC NOTEXT
* add the children for node with key 'Child2'
* Node with key 'New3'
      CLEAR node.
      node-node_key = 'New3'.                               "#EC NOTEXT
      node-relatkey = 'Child2'.
      node-relatship = cl_gui_list_tree=>relat_last_child.
      APPEND node TO node_table.

* Node with key 'New4'
      CLEAR node.
      node-node_key = 'New4'.                               "#EC NOTEXT
      node-relatkey = 'Child2'.
      node-relatship = cl_gui_list_tree=>relat_last_child.
      APPEND node TO node_table.

* Items of node with key 'New3'
      CLEAR item.
      item-node_key = 'New3'.
      item-item_name = '1'.
      item-class = cl_gui_list_tree=>item_class_text.
      item-length = 11.
      item-usebgcolor = 'X'. "
      item-text = 'SAPTROX1'.
      APPEND item TO item_table.

      CLEAR item.
      item-node_key = 'New3'.
      item-item_name = '2'.
      item-class = cl_gui_list_tree=>item_class_text.
      item-alignment = cl_gui_list_tree=>align_auto.
      item-font = cl_gui_list_tree=>item_font_prop.
      item-text = 'Comment to SAPTROX1'.                    "#EC NOTEXT
      APPEND item TO item_table.

* Items of node with key 'New4'
      CLEAR item.
      item-node_key = 'New4'.
      item-item_name = '1'.
      item-class = cl_gui_list_tree=>item_class_text.
      item-length = 11.
      item-usebgcolor = 'X'. "
      item-text = 'SAPTRIXTROX'.
      APPEND item TO item_table.

      CLEAR item.
      item-node_key = 'New4'.
      item-item_name = '2'.
      item-class = cl_gui_list_tree=>item_class_text.
      item-alignment = cl_gui_list_tree=>align_auto.
      item-font = cl_gui_list_tree=>item_font_prop.
      item-text = 'Comment to SAPTRIXTROX'.                 "#EC NOTEXT
      APPEND item TO item_table.
    ENDIF.

    CALL METHOD g_tree->add_nodes_and_items
      EXPORTING
        node_table = node_table
        item_table = item_table
        item_table_structure_name = 'MTREEITM'
      EXCEPTIONS
        failed = 1
        cntl_system_error = 3
        error_in_tables = 4
        dp_error = 5
        table_structure_name_not_found = 6.
    IF sy-subrc <> 0.
      MESSAGE a000(tree_control_msg).
    ENDIF.
  ENDMETHOD.

ENDCLASS.

****** DATA DEFINITIONS
* Type definitions
types: begin of itab_type,
         folder   type flag,
         node_key type mtreeitm,
         relatkey type tv_nodekey,
         type     like qmel-QMART,
         qmnum    like qmel-qmnum,
         qwrnum    like qmel-qwrnum,
       end of itab_type.

* Data Definitions
data: okcode    like sy-ucomm,
      itab_data type itab_type occurs 0,
      wa_data   type itab_type.

* Tree list definitions
class lcl_application definition deferred.
class cl_gui_cfw      definition load.

* CAUTION: MTREEITM is the name of the item structure which must
* be defined by the programmer. DO NOT USE MTREEITM!
types: item_table_type like standard table of mtreeitm
       with default key.

****** SELECTION SCREEN
*----------------------------------------------------------------------*
* Start of Selection
*----------------------------------------------------------------------*
START-OF-SELECTION.

  REFRESH: itab_data.
  PERFORM create_input_table TABLES itab_data.

* create the application object
* this object is needed to handle the ABAP Objects Events of Controls
  CREATE OBJECT g_application.
  CALL SCREEN 2000.    "Tree Report

END-OF-SELECTION.

****** SUBROUTINES
*&---------------------------------------------------------------------*
*&      Form  update_sel_screen_attributes
*&---------------------------------------------------------------------*
FORM update_sel_screen_attributes.
  LOOP AT SCREEN.

    IF screen-group1 = 'SC1'.
      screen-intensified = '1'.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.
ENDFORM.                    " update_sel_screen_attributes

*&---------------------------------------------------------------------*
*&      Form  create_and_init_tree
*&---------------------------------------------------------------------*
FORM create_and_init_tree.
  DATA: node_table TYPE treev_ntab,
        item_table TYPE item_table_type,
        events TYPE cntl_simple_events,
        event TYPE cntl_simple_event.

* create a container for the tree control
  CREATE OBJECT g_custom_container
    EXPORTING
      " the container is linked to the custom control with the
      " name 'TREE_CONTAINER' on the dynpro
      container_name = 'TREE_CONTAINER'
    EXCEPTIONS
      cntl_error = 1
      cntl_system_error = 2
      create_error = 3
      lifetime_error = 4
      lifetime_dynpro_dynpro_link = 5.
  IF sy-subrc <> 0.
    MESSAGE a000(tree_control_msg).
  ENDIF.


* create a list tree control
  CREATE OBJECT g_tree
    EXPORTING
      parent              = g_custom_container
      node_selection_mode = cl_gui_list_tree=>node_sel_mode_single
      item_selection     = 'X'
      with_headers       = ' '
    EXCEPTIONS
      cntl_system_error           = 1
      create_error                = 2
      failed                      = 3
      illegal_node_selection_mode = 4
      lifetime_error              = 5.
  IF sy-subrc <> 0.
    MESSAGE a000(tree_control_msg).
  ENDIF.

* define the events which will be passed to the backend
  " node double click
  event-eventid = cl_gui_list_tree=>eventid_node_double_click.
  event-appl_event = 'X'.                                   "
  APPEND event TO events.

  " item double click
  event-eventid = cl_gui_list_tree=>eventid_item_double_click.
  event-appl_event = 'X'.
  APPEND event TO events.

  " expand no children
  event-eventid = cl_gui_list_tree=>eventid_expand_no_children.
  event-appl_event = 'X'.
  APPEND event TO events.

  " link click
  event-eventid = cl_gui_list_tree=>eventid_link_click.
  event-appl_event = 'X'.
  APPEND event TO events.

  " button click
  event-eventid = cl_gui_list_tree=>eventid_button_click.
  event-appl_event = 'X'.
  APPEND event TO events.

  " checkbox change
  event-eventid = cl_gui_list_tree=>eventid_checkbox_change.
  event-appl_event = 'X'.
  APPEND event TO events.

  CALL METHOD g_tree->set_registered_events
    EXPORTING
      events = events
    EXCEPTIONS
      cntl_error                = 1
      cntl_system_error         = 2
      illegal_event_combination = 3.
  IF sy-subrc <> 0.
    MESSAGE a000(tree_control_msg).
  ENDIF.

* assign event handlers in the application class to each desired event
  SET HANDLER g_application->handle_node_double_click FOR g_tree.
  SET HANDLER g_application->handle_item_double_click FOR g_tree.
  SET HANDLER g_application->handle_expand_no_children FOR g_tree.
  SET HANDLER g_application->handle_link_click FOR g_tree.
  SET HANDLER g_application->handle_button_click FOR g_tree.
  SET HANDLER g_application->handle_checkbox_change FOR g_tree.

* add some nodes to the tree control
* NOTE: the tree control does not store data at the backend. If an
* application wants to access tree data later, it must store the
* tree data itself.

  PERFORM build_node_and_item_table USING node_table item_table.

  CALL METHOD g_tree->add_nodes_and_items
    EXPORTING
      node_table = node_table
      item_table = item_table
      item_table_structure_name = 'MTREEITM'
    EXCEPTIONS
      failed = 1
      cntl_system_error = 3
      error_in_tables = 4
      dp_error = 5
      table_structure_name_not_found = 6.
  IF sy-subrc <> 0.
    MESSAGE a000(tree_control_msg).
  ENDIF.

ENDFORM.                    " create_and_init_tree
*&---------------------------------------------------------------------*
*&      Form  build_node_and_item_table
*&---------------------------------------------------------------------*
FORM build_node_and_item_table
  USING
    node_table TYPE treev_ntab
    item_table TYPE item_table_type.
  DATA: node TYPE treev_node,
        item TYPE mtreeitm.

* Build the node and item table.
  LOOP AT itab_data INTO wa_data.
    CLEAR node.

    node-node_key = wa_data-node_key.
    node-relatkey = wa_data-relatkey.
    node-isfolder = 'X'.
    IF wa_data-relatkey IS INITIAL.
      CLEAR: node-relatship,
             node-exp_image,
             node-expander.
      node-hidden = ' '.
      node-disabled = ' '.
    ELSE.
      node-relatship = cl_gui_list_tree=>relat_last_child.
    ENDIF.
    APPEND node TO node_table.

* Update Items
    CLEAR item.
    item-node_key = wa_data-node_key.
    item-item_name = '1'.
    item-length = 4.
    item-class = cl_gui_list_tree=>item_class_text. " Text Item
    item-alignment = cl_gui_list_tree=>align_auto.
    item-font = cl_gui_list_tree=>item_font_prop.
    item-usebgcolor = 'X'.
    item-text = wa_data-type.
    APPEND item TO item_table.

    CLEAR item.
    item-node_key = wa_data-node_key.
    item-item_name = '2'.
    item-length = 20.
    item-class = cl_gui_list_tree=>item_class_text. " Text Item
    item-alignment = cl_gui_list_tree=>align_auto.
    item-font = cl_gui_list_tree=>item_font_prop.
    item-text = wa_data-qmnum.
    APPEND item TO item_table.

  ENDLOOP.

ENDFORM.                    " build_node_and_item_table
*&---------------------------------------------------------------------*
*&      Form  create_input_table
*&---------------------------------------------------------------------*
FORM create_input_table TABLES   p_itab_data LIKE itab_data.

  DATA: t_counter(4) TYPE n,
        t_parent(4) TYPE c,
        t_parent1(4) TYPE c,
        t_qmnum LIKE qmel-qmnum,
        t_qmnum1 LIKE qmel-qmnum.

  t_counter = 1.
  CLEAR: t_parent, t_parent1.

  IF NOT s_ecr IS INITIAL.
    SELECT qmnum qmart qwrnum
      INTO (qmel-qmnum, qmel-qmart, qmel-qwrnum)
      FROM qmel
      WHERE qmnum IN s_ecr AND
            qmart = 'C3'.

      CLEAR: wa_data.
      wa_data-folder = 'X'.
      wa_data-node_key = t_counter.
      wa_data-relatkey = t_parent.
      wa_data-type = qmel-qmart.
      wa_data-qmnum = qmel-qmnum.
      wa_data-qwrnum = qmel-qwrnum.
      APPEND wa_data TO p_itab_data.
      t_parent = t_counter.
      ADD 1 TO t_counter.

* Check for ECNs attached to this ECR
      SELECT qmnum qmart qwrnum
        INTO (qmel-qmnum, qmel-qmart, qmel-qwrnum)
        FROM qmel
        WHERE qwrnum = qmel-qmnum AND
              qmart = 'C4'.

        CLEAR: wa_data.
        wa_data-folder = 'X'.
        wa_data-node_key = t_counter.
        wa_data-relatkey = t_parent.
        wa_data-type = qmel-qmart.
        wa_data-qmnum = qmel-qmnum.
        wa_data-qwrnum = qmel-qwrnum.
        APPEND wa_data TO p_itab_data.
        t_parent1 = t_counter.
        ADD 1 TO t_counter.

* Check for ECOs attached to this ECN
        SELECT qmnum qmart qwrnum
          INTO (qmel-qmnum, qmel-qmart, qmel-qwrnum)
          FROM qmel
          WHERE qwrnum = qmel-qmnum AND
                qmart = 'C5'.

          CLEAR: wa_data.
          wa_data-folder = 'X'.
          wa_data-node_key = t_counter.
          wa_data-relatkey = t_parent1.
          wa_data-type = qmel-qmart.
          wa_data-qmnum = qmel-qmnum.
          wa_data-qwrnum = qmel-qwrnum.
          APPEND wa_data TO p_itab_data.
          ADD 1 TO t_counter.

        ENDSELECT.
        CLEAR: t_parent1.

      ENDSELECT.
      CLEAR: t_parent.

    ENDSELECT.
    CLEAR: t_parent, t_parent1.
  ENDIF.

  IF NOT s_ecn IS INITIAL.
    SELECT qmnum qmart qwrnum
      INTO (qmel-qmnum, qmel-qmart, qmel-qwrnum)
      FROM qmel
      WHERE qmnum IN s_ecn AND
            qmart = 'C4'.

      CLEAR: wa_data.
      wa_data-folder = 'X'.
      wa_data-node_key = t_counter.
      wa_data-relatkey = t_parent1.
      wa_data-type = qmel-qmart.
      wa_data-qmnum = qmel-qmnum.
      wa_data-qwrnum = qmel-qwrnum.
      APPEND wa_data TO p_itab_data.
      t_parent1 = t_counter.
      ADD 1 TO t_counter.

      t_qmnum = qmel-qmnum.
* Is an ECR attached?
      IF NOT qmel-qwrnum IS INITIAL.
        SELECT SINGLE qmnum qmart qwrnum
          INTO (qmel-qmnum, qmel-qmart, qmel-qwrnum)
          FROM qmel
          WHERE qmnum = qmel-qwrnum.
        IF sy-subrc = 0.
          CLEAR: wa_data.
          wa_data-folder = 'X'.
          wa_data-node_key = t_counter.
          wa_data-relatkey = t_parent1.
          wa_data-type = qmel-qmart.
          wa_data-qmnum = qmel-qmnum.
          wa_data-qwrnum = qmel-qwrnum.
          APPEND wa_data TO p_itab_data.
          ADD 1 TO t_counter.
        ENDIF.
      ENDIF.
      qmel-qmnum = t_qmnum.

* Check for ECOs attached to this ECN
      SELECT qmnum qmart qwrnum
        INTO (qmel-qmnum, qmel-qmart, qmel-qwrnum)
        FROM qmel
        WHERE qwrnum = qmel-qmnum AND
              qmart = 'C5'.

        CLEAR: wa_data.
        wa_data-folder = 'X'.
        wa_data-node_key = t_counter.
        wa_data-relatkey = t_parent1.
        wa_data-type = qmel-qmart.
        wa_data-qmnum = qmel-qmnum.
        wa_data-qwrnum = qmel-qwrnum.
        APPEND wa_data TO p_itab_data.
        ADD 1 TO t_counter.

      ENDSELECT.
      CLEAR: t_parent1.

    ENDSELECT.
    CLEAR: t_parent, t_parent1.
  ENDIF.

  IF NOT s_eco IS INITIAL.
    SELECT qmnum qmart qwrnum
      INTO (qmel-qmnum, qmel-qmart, qmel-qwrnum)
      FROM qmel
      WHERE qmnum IN s_eco AND
            qmart = 'C5'.

      CLEAR: wa_data.
      wa_data-folder = 'X'.
      wa_data-node_key = t_counter.
      wa_data-relatkey = t_parent.
      wa_data-type = qmel-qmart.
      wa_data-qmnum = qmel-qmnum.
      wa_data-qwrnum = qmel-qwrnum.
      APPEND wa_data TO p_itab_data.
      t_parent = t_counter.
      ADD 1 TO t_counter.

      t_qmnum = qmel-qmnum.
      IF NOT qmel-qwrnum IS INITIAL.
* Check for ECNs attached to this ECO
        SELECT qmnum qmart qwrnum
          INTO (qmel-qmnum, qmel-qmart, qmel-qwrnum)
          FROM qmel
          WHERE qmnum = qmel-qwrnum AND
                qmart = 'C4'.

          CLEAR: wa_data.
          wa_data-folder = 'X'.
          wa_data-node_key = t_counter.
          wa_data-relatkey = t_parent.
          wa_data-type = qmel-qmart.
          wa_data-qmnum = qmel-qmnum.
          wa_data-qwrnum = qmel-qwrnum.
          APPEND wa_data TO p_itab_data.
          t_parent1 = t_counter.
          ADD 1 TO t_counter.

          t_qmnum1 = qmel-qmnum.
          IF NOT qmel-qwrnum IS INITIAL.
* Check for ECRs attached to this ECN
            SELECT qmnum qmart qwrnum
              INTO (qmel-qmnum, qmel-qmart, qmel-qwrnum)
              FROM qmel
              WHERE qmnum = qmel-qwrnum AND
                    qmart = 'C3'.

              CLEAR: wa_data.
              wa_data-folder = 'X'.
              wa_data-node_key = t_counter.
              wa_data-relatkey = t_parent1.
              wa_data-type = qmel-qmart.
              wa_data-qmnum = qmel-qmnum.
              wa_data-qwrnum = qmel-qwrnum.
              APPEND wa_data TO p_itab_data.
              ADD 1 TO t_counter.

            ENDSELECT.
            qmel-qmnum = t_qmnum1.
            CLEAR: t_parent1.
          ENDIF.

        ENDSELECT.
        qmel-qmnum = t_qmnum.
        CLEAR: t_parent1.
      ENDIF.

    ENDSELECT.
    CLEAR: t_parent, t_parent1.
  ENDIF.

ENDFORM.                    " create_input_table

****** PBO Screen 2000
*&---------------------------------------------------------------------*
*&      Module  STATUS_2000  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
module STATUS_2000 output.
  SET PF-STATUS 'ZCM_2000'.
  SET TITLEBAR 'ZCM_2000'.

endmodule.                 " STATUS_2000  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  init_data_2000  OUTPUT
*&---------------------------------------------------------------------*
module init_data_2000 output.
  if g_tree is initial.
    " The Tree Control has not been created yet.
    " Create a Tree Control and insert nodes into it.
    perform create_and_init_tree.
  endif.

endmodule.                 " init_data_2000  OUTPUT

****** PAI Screen 2000
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_2000  INPUT
*&---------------------------------------------------------------------*
module user_command_2000 input.
  data: return_code type i.
* CL_GUI_CFW=>DISPATCH must be called if events are registered
* that trigger PAI
* this method calls the event handler method of an event
  call method cl_gui_cfw=>dispatch
    importing return_code = return_code.
  if return_code <> cl_gui_cfw=>rc_noevent.
    " a control event occured => exit PAI
    clear okcode.
    exit.
  endif.

  case okcode.
    when 'RETURN'. " Finish program
      if not g_custom_container is initial.
        " destroy tree container (detroys contained tree control, too)
        call method g_custom_container->free
          exceptions
            cntl_system_error = 1
            cntl_error        = 2.
        if sy-subrc <> 0.
          message a000(TREE_CONTROL_MSG).
        endif.
        clear g_custom_container.
        clear g_tree.
      endif.
      leave to screen 0.
  endcase.

  clear okcode.

endmodule.                 " USER_COMMAND_2000  INPUT
****** SCREEN 2000



screen2000.gif
 Description:
Screen 2000
 Filesize:  26.19 KB
 Viewed:  10388 Time(s)

screen2000.gif


Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ALV Grid / ALV Tree / ALV List All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.