пятница, 2 января 2015 г.

ALV Tree

Попытался как можно проще описать создание ALV Tree.
Для начала, нужно создать экран, например 0100. Поместить на экран «Спец. упр. элемент» т.е. контейнер и назвать его, например CONT. Далее зайти в логику экрана и активировать там модули PBO и PAI.
 Должно получится это:



Дальше нужно создать GUI-статус, с одной кнопкой назад и назвать его, например MAIN1.
Теперь все проще, копируйте текст программы к себе и активируйте. По коду есть комментарии, которые должны помочь сориентироваться.
report  ztest_tree.
data: g_alv_tree         type ref to cl_gui_alv_tree,
      g_custom_container 
type ref to cl_gui_custom_container.
data: gt_sflight type sflight occurs 0,      "Выходная таблица данных
      g_max      
type i value 255.
call screen '0100'.
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
module status_0100 output.
  
set pf-status 'MAIN1'.
  
if g_alv_tree is initial.
    
perform init_tree.
  
endif.
endmodule.                 " STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*&      Form  INIT_TREE
*&---------------------------------------------------------------------*
form init_tree .
* 1. Создаем ALV Tree Control.
* создаем контейнер, помещаем в него ALV Tree
  
data: l_tree_container_name(30) type c.
  l_tree_container_name 
= 'CONT'.
  
create object g_custom_container
    
exporting
      container_name              
= l_tree_container_name
    
exceptions
      cntl_error                  
= 1
      cntl_system_error           
= 2
      create_error                
= 3
      lifetime_error              
= 4
      lifetime_dynpro_dynpro_link 
= 5.
  
if sy-subrc <> 0.
    
message x208
(00) with 'ERROR'(100).
  
endif.

  
create object g_alv_tree
    
exporting
      parent                      
= g_custom_container
      node_selection_mode         
= cl_gui_column_tree=>node_sel_mode_single
      item_selection              
= 'X'
      no_html_header              
= 'X'
      no_toolbar                  
= ''
    
exceptions
      cntl_error                  
= 1
      cntl_system_error           
= 2
      create_error                
= 3
      lifetime_error              
= 4
      illegal_node_selection_mode 
= 5
      failed                      
= 6
      illegal_column_name         
= 7.
  
if sy-subrc <> 0.
    
message x208(00) with 'ERROR'.
  
endif.
* 2. Создаем заголовок иерархии
  
data l_hierarchy_header type treev_hhdr.
  
perform build_hierarchy_header changing l_hierarchy_header.
* 3. Создаем пустой Tree Control
  
call method g_alv_tree->set_table_for_first_display
    
exporting
      i_structure_name    
= 'SFLIGHT'
      is_hierarchy_header 
= l_hierarchy_header
    
changing
      it_outtab           
= gt_sflight. "таблица должна быть пустая !
* 4. Создаем узлы и уровни (заполнение дерева происходит именно тут)
  
perform create_hierarchy.
* * 5. Отправим данные в alv-tree
  
call method g_alv_tree->frontend_update.
endform.                    " INIT_TREE
*&---------------------------------------------------------------------*
*&      Form  BUILD_HIERARCHY_HEADER
*&---------------------------------------------------------------------*
*       Заполняем свойства заголовка иерархии
*----------------------------------------------------------------------*
form build_hierarchy_header changing
                               p_hierarchy_header 
type treev_hhdr.

  p_hierarchy_header
-heading = ' Список узлов'.
  p_hierarchy_header-t_image 
= '@BV@'. "иконка вверху списка узлов
  p_hierarchy_header
-tooltip = 'Всплывающая подсказка'.
  p_hierarchy_header
-width   = 30.     "ширина списка узлов

endform.                    " BUILD_HIERARCHY_HEADER
*&---------------------------------------------------------------------*
*&      Form  CREATE_HIERARCHY
*&---------------------------------------------------------------------*
*       Создание узлов и иерархии
*----------------------------------------------------------------------*
form create_hierarchy .

  
data: ls_sflight type sflight,
        l2_sflight 
type sflight,
      lsum_sflight 
type sflight,
        lt_sflight 
type sflight occurs 0.

  
data: p_relat_key type lvc_nkey,
        l_node_text 
type lvc_value,
         p_node_key 
type lvc_nkey
        
.

  
"Выбираем данные
  
select * from sflight into table lt_sflight up to g_max rows.


  
"Выполняем сортировку по полю которое будет узлом/ключом
  
sort  lt_sflight by carrid.

  
loop at lt_sflight into ls_sflight.

    
on change of ls_sflight-carrid.

      
"Т.к. я планирую в заголовке узла так же выводить данные
      
"то для красоты буду выводить там суммы по carrid
      
"в принципе, это можно и не делать <<<
      
at new carrid.
        
sum.
        
move ls_sflight to lsum_sflight.
      
endat.
      
">>>

      
"выберем название узла
      
select single carrname
        
from scarr
        
into l_node_text
        
where
        carrid 
eq ls_sflight-carrid.

      
"создаем узел
      
call method g
_alv_tree->add_node
        
exporting
          i_relat_node_key 
= p_relat_key
          i_relationship   
= cl_gui_column_tree=>relat_last_child
          is_outtab_line   
= lsum_sflight
          i_node_text      
= l_node_text
        
importing
          e_new_node_key   
= p_node_key.

      
"Выведем в данный узел значения <<<
      
loop at lt_sflight into l2_sflight where carrid eq ls_sflight-carrid.
        
l_node_text = ls_sflight-carrid.

        
"создаем строку в узле.
        
"каждая такая строка может содержать свой раскрывающийся список
        
"т.е. быть еще и узлом. Это ж дерево:)

        
call method g_alv_tree->add_node
          
exporting
            i_relat_node_key 
= p_node_key
            i_relationship   
= cl_gui_column_tree=>relat_last_child
            is_outtab_line   
= l2_sflight
            i_node_text      
= l_node_text.

        
"<<< тут вложенность может быть продолжена >>>
      
endloop.

      
"Выведем в данный узел значения >>>
    
endon.
  
endloop.
endform.                    " CREATE_HIERARCHY
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
module user_command_0100 input.
  
case sy-ucomm.
    
when 'EXIT' or 'BACK' or 'CANC'.
      
call method g_custom_container->free.
      
leave program.
    
when others.
      
call method cl_gui_cfw=>dispatch.
  
endcase.
  
call method cl_gui_cfw=>flush.
endmodule.                 " USER_COMMAND_0100  INPUT

Результат:

1 комментарий:

  1. Гайд прекрасен, без вашего сайта было бы тяжко)

    ОтветитьУдалить