terça-feira, 19 de janeiro de 2016

Mudando o Layout por campo do ALV - LVC_T_STYL

REPORT z_cell_style.
*  Opa Pessoal, estou devolta pra mostrar como mudar o estilo por celula,
* o modo normal hoje para deixar zebrado ou as medidas da coluna conforme
* os valores nela contido usamos a estrutura LVC_S_LAYO mas esse layout
* é para todo o relatório, mas para trabalhar por celula conforme valor,
* ou deixar aberto um campo especifico dentro do alv usando regra de negocio,
*  isso queiremos fazer hoje, como sempre o código está simples para que
* consigam focar nas diferenças de um ALV normal.

*  Aqui, logo de começo já tem uma das diferenças que é a inclusão do campos
* 'CELLTAB' dentro da tabela ´de saída que será exibida, clicando duas vezes
* no tipo do campo 'LVC_T_LAYO' vamos perceber que ele na verdade é uma tabela,
* nesse campo que é uma tabela ira conter todos os layouts referente ao registro.
* Tipos
TYPES :
  
BEGIN OF ty_sflight.
        
INCLUDE STRUCTURE sflight.
TYPES :
  celltab 
TYPE lvc_t_styl,
  
END OF ty_sflight.

* Objetos
DATA :
  o_alv     
TYPE REF TO cl_gui_alv_grid,
  o_docking 
TYPE REF TO cl_gui_docking_container.

* Tabelas Internas
DATA :
  it_fieldcat 
TYPE TABLE OF lvc_s_fcat,
  it_sflight  
TYPE TABLE OF ty_sflight.

* Parametros
PARAMETERS :
  p_seats 
TYPE sflight-seatsocc,
  p_seatm 
TYPE sflight-seatsmax_b.

* Tela Principal com o ALV
CALL SCREEN 9000.

*&---------------------------------------------------------------------*
*&      Module  INITIALIZATION  OUTPUT
*&---------------------------------------------------------------------*
MODULE initialization OUTPUT.


  
BREAK-POINT.
*  Aqui estamos criando a estrutura que ira conter as informações referente
* ao layout do ALV, a unica coisa que vamos fazer nessa estrutura e informar
* no campo 'STYLEFNAME' o nome do campo/tabela que ira conter as informações
* de layout especifico para cada campo do registro atual.
  
DATAlst_layout TYPE lvc_s_layo.
  lst_layout
-no_toolbar abap_true.
  
lst_layout-stylefname 'CELLTAB'.

  
PERFORM  :
* criação do catalogo de campos
  f_fieldcat
,
* Seleção dos dados do Vôo
  f_selection_sflight
,
* Criação dos layouts para cada celula conforme regra
  f_style_celltab
.

  
IF o_docking IS INITIAL.

    
CREATE OBJECT o_docking
      
EXPORTING
        repid     
sy-repid    
        dynnr     
'9000'        
        extension 
9999.       " Control Extension

    
CREATE OBJECT o_alv
      
EXPORTING
        i_parent 
o_docking.

    o_alv
->set_table_for_first_display(
      
EXPORTING
        is_layout                     
lst_layout
      
CHANGING
        it_outtab                     
it_sflight
        it_fieldcatalog               
it_fieldcat
    
).

    o_alv
->set_ready_for_inputEXPORTING i_ready_for_input ).

  
ENDIF.

ENDMODULE.                 " INITIALIZATION  OUTPUT


*&---------------------------------------------------------------------*
*&      Form  F_FIELDCAT
*&---------------------------------------------------------------------*
FORM f_fieldcat .

  
DATAlst_fcat TYPE lvc_s_fcat.

  
IF it_fieldcat IS INITIAL.
    
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
      
EXPORTING
*       I_BUFFER_ACTIVE        =
        i_structure_name       
'SFLIGHT'
*       I_CLIENT_NEVER_DISPLAY = 'X'
*       I_BYPASSING_BUFFER     =
*       I_INTERNAL_TABNAME     =
      
CHANGING
        ct_fieldcat            
it_fieldcat
      
EXCEPTIONS
        inconsistent_interface 
1
        program_error          
2
        
OTHERS                 3.

  
ENDIF.

ENDFORM.                    " F_FIELDCAT

*&---------------------------------------------------------------------*
*&      Form  F_SELECTION_SFLIGHT
*&---------------------------------------------------------------------*
FORM f_selection_sflight .

  
FREE it_sflight.
  
SELECT FROM sflight INTO CORRESPONDING FIELDS OF TABLE it_sflight.

ENDFORM.                    " F_SELECTION_SFLIGHT

*&---------------------------------------------------------------------*
*&      Form  F_STYLE_CELLTAB
*&---------------------------------------------------------------------*
FORM f_style_celltab .

* Variaveis
  
DATA :
    lst_sflight 
LIKE LINE OF it_sflight,
    l_tabix     
TYPE sy-tabix.
*  Estrutura que ira alimentar a tabela com os layout especificos de cada campo
* de cada registro.
  
DATA :
    lst_style   
TYPE lvc_s_styl.

*  Agora vamos dar um loop na tabela de saida e alimentar o estilo de cadacampo
* de cada registro conforme regra de negocio que no nosso caso é bem simples,
* depois de testarem e o negocio funcionar tentem usar as outras constantes 
* da classe do alv, lembrando que essas constantes começam com 'MC_STYLE_'.
BREAK-POINT.
  
LOOP AT it_sflight INTO lst_sflight.

    l_tabix 
sy-tabix.

    
IF lst_sflight-seatsocc > p_seats.
      lst_style
-fieldname 'SEATSOCC'.
      lst_style
-style     cl_gui_alv_grid=>mc_style_enabled.
      
INSERT lst_style INTO TABLE lst_sflight-celltab.

    
ELSE.
      lst_style
-fieldname 'SEATSOCC'.
      lst_style
-style     cl_gui_alv_grid=>mc_style_disabled.
      
INSERT lst_style INTO TABLE lst_sflight-celltab.
    
ENDIF.

    
IF lst_sflight-seatsmax_b > p_seatm .
      lst_style
-fieldname 'SEATSMAX_B'.
      lst_style
-style     cl_gui_alv_grid=>mc_style_button.
      
INSERT lst_style INTO TABLE lst_sflight-celltab.

      
IF lst_sflight-seatsmax_f IS INITIAL.
        lst_style
-fieldname 'SEATSMAX_F'.
        lst_style
-style     cl_gui_alv_grid=>mc_style_enabled.
        lst_style
-style2    cl_gui_alv_grid=>mc_style_f4_no.
        
INSERT lst_style INTO TABLE lst_sflight-celltab.
      
ENDIF.

    
ELSE.
      lst_style
-fieldname 'SEATSMAX_B'.
      lst_style
-style     cl_gui_alv_grid=>mc_style_hotspot.
      
INSERT lst_style INTO TABLE lst_sflight-celltab.
    
ENDIF.

    
MODIFY it_sflight FROM lst_sflight INDEX l_tabix.
  
ENDLOOP.

ENDFORM.                    " F_STYLE_CELLTAB

*&---------------------------------------------------------------------*
*&      Module  STATUS_9000  OUTPUT
*&---------------------------------------------------------------------*
MODULE status_9000 OUTPUT.
  
SET PF-STATUS 'PF_9000'.
*  SET TITLEBAR 'xxx'.

ENDMODULE.                 " STATUS_9000  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_9000  INPUT
*&---------------------------------------------------------------------*
MODULE user_command_9000 INPUT.

  
CASE sy-ucomm.
    
WHEN 'BACK'.
      
LEAVE TO SCREEN 0.
    
WHEN 'EXIT' OR 'CANCEL'.
      
LEAVE PROGRAM.
    
WHEN OTHERS.
  
ENDCASE.

ENDMODULE.                 " USER_COMMAND_9000  INPUT

*  Eu criei uns parametros somente para vocês verem mudando os campos conforme
* os valores colocados, espero que tenha proveito com esse post, segue abaixo
* o alv com o layout novo. Valeoooooo.






Comentários
0 Comentários

Nenhum comentário:

Postar um comentário