Você está na página 1de 22

.

Maria Coelho Aguiar, 215


CENESP Bloco C 4. Andar
05804-900 So Paulo SP
Tel.: 3741-4040 Fax : 3741-3562

ALV OO Guia de Implementao

2
Fbrica Software ABAP ALV OO

ndice
ALVSimples..........................................................................................................................................................3
ALVTotal...............................................................................................................................................................7
ALVSub-Total.......................................................................................................................................................8
ALVHotspot...........................................................................................................................................................9
ALVSalvarLayout...............................................................................................................................................11
ALVExibiocone/Colorirlinha.....................................................................................................................13
ALVRefresh........................................................................................................................................................14
ALVExemplo.......................................................................................................................................................15

WAFbricaABAP

Page2de22
WAConsultoria

3
Fbrica Software ABAP ALV OO

ALV Simples
Passo 1:Desenharolayoutdatela,incluindoocontainerondeserexibidooALV.(SE80)

Boto: SAIR
Cd. Fnc. : BT_SAIR

Boto: DELETAR
Cd. Fnc. : BT_DEL

Container: CONTAINER

Tela 1001

Passo 2:Declararasvariveisglobaisconformesintaxeabaixo:
*----------------------------------------------------------*DeclaraesALV
*----------------------------------------------------------DATA:gridTYPEREFTOcl_gui_alv_grid,
containerTYPEREFTOcl_gui_custom_container.
DATA:t_fieldcatTYPETABLEOFlvc_s_fcat,
s_fieldcatTYPE
lvc_s_fcat.
DATA:gs_layoutTYPE

WAFbricaABAP

lvc_s_layo.

Page3de22
WAConsultoria

4
Fbrica Software ABAP ALV OO

*-----------------------------------------------------------*Declaraestabela
internaaserexibidanoALV
*----------------------------------------------------------TYPES:BEGINOFty_vbap,
vbeln
TYPEvbap-vbeln,
matnr
TYPEvbap-matnr,
netwr
TYPEvbap-netwr,
ENDOFty_vbap.
DATA:t_vbapTYPETABLEOFty_vbap.
DATA:s_vbapTYPEty_vbap.
Passo 3:Executarabuscadosdadosaseremexibidosnogrid.
"AbandonararotinacasooALVjestejapreenchido
IFNOTt_vbap[]ISINITIAL.
EXIT.
ENDIF.
SELECTvbeln
matnr
netwr
FROMvbap
INTOTABLEt_vbap.

Passo 4: Setaraspropriedadesgeraisedascolunasdoalv.
*Tamanhodascolunasotimizadas
gs_layout-cwidth_opt='X'.
*linhaszebradas
gs_layout-zebra='X'.

*ttuloALV
gs_layout-grid_title=ALV:OrdemdeVendas.

WAFbricaABAP

Page4de22
WAConsultoria

5
Fbrica Software ABAP ALV OO

*Configurarascolunasaserempreenchidas
s_fieldcat-fieldname='VBELN'."Nomedocampo
s_fieldcat-outputlen='10'."Tamanhodacoluna
s_fieldcat-coltext='NOV'."Labeldacoluna
s_fieldcat-hotspot=''."Hotspot
s_fieldcat-do_sum=''. "Somarizado
APPENDs_fieldcatTOt_fieldcat.
s_fieldcat-fieldname='NETWR'."Nomedocampo
s_fieldcat-outputlen='12'."Tamanhodacoluna
s_fieldcat-coltext='Valor'."Labeldacoluna
s_fieldcat-hotspot=''."Hotspot
s_fieldcat-do_sum=''.
"Somarizado
APPENDs_fieldcatTOt_fieldcat.

Passo 5: ChamaromtodoparaexibiroGridnatela.

CREATEOBJECTcontainerEXPORTING
container_name='CONTAINER'.
CREATEOBJECTgridEXPORTING
i_parent=container.

Passo 6:
"Metodoparaexibirogrid
7:
CALLMETHODgrid->set_table_for_first_display
Passo
EXPORTING
is_layout=gs_layout
CHANGING
it_outtab=t_vbap
it_fieldcatalog=t_fieldcat.

WAFbricaABAP

Page5de22
WAConsultoria

6
Fbrica Software ABAP ALV OO

Passo 6: Limparasclassesapsafinalizaodorelatrio

"LimpaoGrid
CALLMETHODgrid->free
EXCEPTIONS
cntl_error=1
cntl_system_error=2.
"LimpaoContainer
IFcontainerISNOTINITIAL.
CALLMETHODcontainer->free
EXCEPTIONS
cntl_error=1
cntl_system_error=2.
ENDIF.

WAFbricaABAP

Page6de22
WAConsultoria

7
Fbrica Software ABAP ALV OO

ALV Total
Passo 1: Setarocampodo_sumdaestruturafieldcatcomovalorX.

*Configurarascolunasaserempreenchidas
s_fieldcat-fieldname='VBELN'."Nomedocampo
s_fieldcat-outputlen='10'."Tamanhodacoluna
s_fieldcat-coltext='NOV'."Labeldacoluna
s_fieldcat-hotspot=''."Hotspot
s_fieldcat-do_sum=''. "Somarizado
APPENDs_fieldcatTOt_fieldcat.
s_fieldcat-fieldname='NETWR'."Nomedocampo
s_fieldcat-outputlen='12'."Tamanhodacoluna
s_fieldcat-coltext='Valor'."Labeldacoluna
s_fieldcat-hotspot=''."Hotspot
s_fieldcat-do_sum='X'. "Somarizado
APPENDs_fieldcatTOt_fieldcat.

WAFbricaABAP

Page7de22
WAConsultoria

8
Fbrica Software ABAP ALV OO

ALV Sub-Total
Passo 1: Declararasvariveisglobaisconformesintaxeabaixo:
*----------------------------------------------------------*DeclaraesALV
*----------------------------------------------------------DATA:t_sortTYPETABLEOFlvc_s_sort,
s_sortTYPElvc_s_sort.

Passo 2: Preencheratabelasortcomocampopeloqualdeveserfeitaasub-quebra.

CLEARs_sort.
s_sort-fieldname='VBELN'."Nomedocampo
s_sort-subtot='X'."Subtotal
APPENDs_sortTOt_sort.

Passo 3: AcrescentaraomtododeexibiroALVatabelaSort.

"Metodoparaexibirogrid
CALLMETHODgrid->set_table_for_first_display
EXPORTING
is_layout=gs_layout
CHANGING
it_outtab=t_vbap
it_sort=t_sort
it_fieldcatalog=t_fieldcat.

WAFbricaABAP

Page8de22
WAConsultoria

9
Fbrica Software ABAP ALV OO

ALV Hotspot
Passo 1: Declararasseguintesvariaveis.

CLASSlcl_event_receiverDEFINITIONDEFERRED.
DATA:event_receiverTYPEREFTOlcl_event_receiver.

Passo 2: AcrescentaraclassededefiniodoeventoHotspot.
*----------------------------------------------------------*CLASSlcl_event_receiverDEFINITION
*----------------------------------------------------------CLASSlcl_event_receiverDEFINITION.
PUBLICSECTION."SeoPblica
"Definiodoeventohotspot
METHODS:
hotspot_click"Nomeparaoevento
FOREVENThotspot_clickOFcl_gui_alv_grid
IMPORTINGe_row_ide_column_ides_row_no.
PRIVATESECTION."SeoPrivada

ENDCLASS.

WAFbricaABAP

Page9de22
WAConsultoria

10
Fbrica Software ABAP ALV OO

Passo 3: AcrescentaraclassedeimplementaodoeventoHotspot.
*----------------------------------------------------------*CLASSlcl_event_receiverIMPLEMENTATION
*----------------------------------------------------------CLASSlcl_event_receiverIMPLEMENTATION.
"Implementaodoeventohotspot
METHODhotspot_click.
READTABLEt_vbapINTOs_vbapINDEXe_row_id-index.
IFsy-subrc=0.
SETPARAMETERID'AUN'FIELDs_vbap-vbeln.
CALLTRANSACTION'VA03'ANDSKIPFIRSTSCREEN.
ENDIF.
ENDMETHOD.

Passo 4: Acrescentaroseguintetrechodecdigoapsomtododeexibiodogrid.

"Metododehotspot
CREATEOBJECTevent_receiver.
SETHANDLERevent_receiver->hotspot_clickFORgrid.

Passo 5: SetarocampohotspotdaestruturafieldcatcomovalorX.
*Configurarascolunasaserempreenchidas
s_fieldcat-fieldname='VBELN'."Nomedocampo
s_fieldcat-outputlen='10'."Tamanhodacoluna
s_fieldcat-coltext='NOV'."Labeldacoluna
s_fieldcat-hotspot='X'."Hotspot
s_fieldcat-do_sum=''. "Somarizado
APPENDs_fieldcatTOt_fieldcat.

WAFbricaABAP

Page10de22
WAConsultoria

11
Fbrica Software ABAP ALV OO

ALV Salvar Layout


Passo 1: Declararasvariveisglobaisconformesintaxeabaixo:

*----------------------------------------------------------*DeclaraesALV
*----------------------------------------------------------DATA:gs_variantTYPEdisvariant.
DATA:ls_layoutTYPEsalv_s_layout_info,
ls_keyTYPEsalv_s_layout_key.

Passo 2: Declararparameterparacapturadelayoutssalvos:

*----------------------------------------------------------*Parameter
*----------------------------------------------------------PARAMETERS:p_variTYPEslis_vari.

Passo 3: AcrescenteoseguintetrechodecdigonaspropriedadesdoALV.

*layout
gs_variant-report=sy-repid.

*exibevarianteescolhida
gs_variant-variant=p_vari.

WAFbricaABAP

Page11de22
WAConsultoria

12
Fbrica Software ABAP ALV OO
Passo 4: NoeventoATSELECTION-SCREENONVALUEREQUESTdoparmetroacima,acrescentaro
seguintecdigo:
ls_key-report=sy-repid.
ls_layout=cl_salv_layout_service=>f4_layouts(
s_key=ls_key
restrict=if_salv_c_layout=>restrict_none).

p_vari=ls_layout-layout.

Passo 5: AcrescentaraomtododeexibiroALVosseguintesparmetros:

"Metodoparaexibirogrid
CALLMETHODgrid->set_table_for_first_display
EXPORTING
is_variant=gs_variant
i_save='A'
is_layout=gs_layout
CHANGING
it_outtab=t_vbap
it_sort=t_sort
it_fieldcatalog=t_fieldcat.

WAFbricaABAP

Page12de22
WAConsultoria

13
Fbrica Software ABAP ALV OO

ALV Exibio cone / Colorir linha


Passo 1:Acrescentarcamponaestruturadatabelafinal.
TYPES:BEGINOFty_vbap,
vbelnTYPEvbap-vbeln,
matnrTYPEvbap-matnr,
netwrTYPEvbap-netwr,
linecolor(4)TYPEc,"Colorirlinhas
icon(1)TYPEc,"Farol
ENDOFty_vbap.

Passo 2:Declararasvariveisglobais
DATA:icon_nameTYPElvc_cifnmVALUE'ICON'.
Passo 3:ModificaratabelainternaaserexibidapeloALV.
LOOPATt_vbapINTOs_vbap.
IFsy-tabix<3.
"Setaracordalinha
s_vbap-linecolor='C110'.
ELSE.
s_vbap-linecolor='C210'.
ENDIF.

IFs_vbap-netwr<=0.
"Setarofarolaserexibido
s_vbap-icon='1'.
ELSEIFs_vbap-netwr>='100'ANDs_vbap-netwr<='2000'.
s_vbap-icon='2'.
ELSE.
s_vbap-icon='3'.
ENDIF.
MODIFYt_vbapFROMs_vbap.
ENDLOOP.

WAFbricaABAP

Page13de22
WAConsultoria

14
Fbrica Software ABAP ALV OO
Passo 4:AcrescentarasseguintespropriedadesaoALV.

*Colorirlinhas
gs_layout-info_fname='LINECOLOR'."nomecolunadatab.

*Farol
gs_layout-excp_fname=icon_name. "nomevar.declarada

ALV Refresh
Passo 1:Chamaromtodoabaixo.
"MetododerefreshdoALV
CALLMETHODgrid->refresh_table_display.

WAFbricaABAP

Page14de22
WAConsultoria

15
Fbrica Software ABAP ALV OO

ALV Exemplo
*&---------------------------------------------------------------------*
*& Report ZLUCIANA_38
*&
*&---------------------------------------------------------------------*
REPORTzluciana_38.
*----------------------------------------------------------------------* Tables
*----------------------------------------------------------------------TABLES:vbap.
*----------------------------------------------------------------------* Types
*----------------------------------------------------------------------TYPES:BEGINOFty_vbap,
vbelnTYPEvbap-vbeln,
matnrTYPEvbap-matnr,
netwrTYPEvbap-netwr,
linecolor(4)TYPEc,"Colorir linhas
icon(1)TYPEc,"Farol
ENDOFty_vbap.
*----------------------------------------------------------------------* Tabelas Internas
*----------------------------------------------------------------------DATA:t_vbapTYPETABLEOFty_vbap.
*----------------------------------------------------------------------* Estruturas
*----------------------------------------------------------------------DATA:s_vbapTYPEty_vbap.
*----------------------------------------------------------------------* Definies ALV Grid Control, Container e Evento
*----------------------------------------------------------------------CLASSlcl_event_receiverDEFINITIONDEFERRED.
DATA:gridTYPEREFTOcl_gui_alv_grid,
containerTYPEREFTOcl_gui_custom_container,
event_receiverTYPEREFTOlcl_event_receiver.
*----------------------------------------------------------------------* Definies Catlogo, Soma , Layout e Variante de Exibio
*----------------------------------------------------------------------DATA:t_fieldcatTYPETABLEOFlvc_s_fcat,
s_fieldcatTYPElvc_s_fcat,
t_sortTYPETABLEOFlvc_s_sort,
s_sortTYPElvc_s_sort,
gs_layoutTYPElvc_s_layo,
gs_variantTYPEdisvariant.
DATA:ls_layoutTYPEsalv_s_layout_info,
ls_keyTYPEsalv_s_layout_key.
DATA:icon_nameTYPElvc_cifnmVALUE'ICON'.

WAFbricaABAP

Page15de22
WAConsultoria

16
Fbrica Software ABAP ALV OO

*----------------------------------------------------------------------* Tela de Seleo


*----------------------------------------------------------------------SELECT-OPTIONS:
so_vbelnFORvbap-vbelnNOINTERVALS.
PARAMETERS:
p_variTYPEslis_vari.
*---------------------------------------------------------------------* AT SELECTION SCREEN
*---------------------------------------------------------------------ATSELECTION-SCREENONVALUE-REQUESTFORp_vari.
PERFORMf_buscar_layouts.
*----------------------------------------------------------------------* Start-Of-Selection
*----------------------------------------------------------------------START-OF-SELECTION.
PERFORMf_call_screen.

*---------------------------------------------------------------------*
*
CLASS lcl_event_receiver DEFINITION
*---------------------------------------------------------------------*
* define o mtodo de hotspot
*---------------------------------------------------------------------*
CLASSlcl_event_receiverDEFINITION.
PUBLICSECTION."Seo Pblica
"Definio do evento hotspot
METHODS:
hotspot_click"Nome para o evento
FOREVENThotspot_clickOFcl_gui_alv_grid
IMPORTINGe_row_ide_column_ides_row_no.
PRIVATESECTION."Seo Privada
ENDCLASS."lcl_event_receiver DEFINITION

WAFbricaABAP

Page16de22
WAConsultoria

17
Fbrica Software ABAP ALV OO

*---------------------------------------------------------------------*
*
CLASS lcl_event_receiver IMPLEMENTATION
*---------------------------------------------------------------------*
CLASSlcl_event_receiverIMPLEMENTATION.
"Implementao do evento hotspot
METHODhotspot_click.
READTABLEt_vbapINTOs_vbapINDEXe_row_id-index.
IFsy-subrc=0.
SETPARAMETERID'AUN'FIELDs_vbap-vbeln.
CALLTRANSACTION'VA03'ANDSKIPFIRSTSCREEN.
ENDIF.
ENDMETHOD."handle_double_click
ENDCLASS."lcl_event_receiver IMPLEMENTATION
*&---------------------------------------------------------------------*
*&
Form F_SELECIONA_DADOS
*&---------------------------------------------------------------------*
FORMf_seleciona_dados.
"Abandonar a rotina caso o ALV j esteja preenchido
IFNOTt_vbap[]ISINITIAL.
EXIT.
ENDIF.
SELECTvbeln
matnr
netwr
FROMvbap
INTOTABLEt_vbap
WHEREvbelnINso_vbeln.
LOOPATt_vbapINTOs_vbap.
"Setar a cor da linha
IFsy-tabix<3.
s_vbap-linecolor='C110'.
ELSE.
s_vbap-linecolor='C210'.
ENDIF.
"Setar o farol a ser exibido
IFs_vbap-netwr<=0.
s_vbap-icon='1'.
ELSEIFs_vbap-netwr>='100000'ANDs_vbap-netwr<='2000000'.
s_vbap-icon='2'.
ELSE.
s_vbap-icon='3'.
ENDIF.
MODIFYt_vbapFROMs_vbap.
ENDLOOP.
ENDFORM." F_SELECIONA_DADOS

WAFbricaABAP

Page17de22
WAConsultoria

18
Fbrica Software ABAP ALV OO

*&---------------------------------------------------------------------*
*&
Form F_CALL_SCREEN
*&---------------------------------------------------------------------*
FORMf_call_screen.
*Chama tela para a exibio do ALV
*---------------------------------CALLSCREEN1001.
ENDFORM." F_CALL_SCREEN
*&---------------------------------------------------------------------*
*&
Module STATUS_1001 OUTPUT
*&---------------------------------------------------------------------*
MODULEstatus_1001OUTPUT.
PERFORMf_seleciona_dados."Seleo de dados
PERFORMf_declara_alv."Setar propriedades do ALV
PERFORMf_declara_sortUSING'VBELN''X'."Setar quebras de subtotal
PERFORMf_grid."Exibir Grid
ENDMODULE." STATUS_1001

OUTPUT

*&---------------------------------------------------------------------*
*&
Module USER_COMMAND_1001 INPUT
*&---------------------------------------------------------------------*
MODULEuser_command_1001INPUT.
CASEsy-ucomm.
WHEN'BT_SAIR'.
"Limpa o Grid depois de finalizado
CALLMETHODgrid->free
EXCEPTIONS
cntl_error=1
cntl_system_error=2.
"Limpa o Container
IFcontainerISNOTINITIAL.
CALLMETHODcontainer->free
EXCEPTIONS
cntl_error=1
cntl_system_error=2.
ENDIF.
"Volta para a tela anterior
LEAVETOSCREEN0.
WHEN'BT_DEL'.
DELETEt_vbapINDEX1.
PERFORMf_grid.
ENDCASE.
ENDMODULE." USER_COMMAND_1001

INPUT

*&---------------------------------------------------------------------*

WAFbricaABAP

Page18de22
WAConsultoria

19
Fbrica Software ABAP ALV OO
*&
Form F_DECLARA_ALV
*&---------------------------------------------------------------------*
FORMf_declara_alv.
* mxima exibio da coluna
gs_layout-cwidth_opt='X'.
* linhas zebradas no ALV
gs_layout-zebra='X'.
* ttulo ALV
gs_layout-grid_title=TEXT-000.
* Colorir linhas
gs_layout-info_fname='LINECOLOR'."nome da coluna
* layout
gs_variant-report=sy-repid.
* exibe variante escolhida
gs_variant-variant=p_vari.
* Farol
gs_layout-excp_fname=icon_name.
* Montar fieldcat (colunas do alv)
FREE:t_fieldcat.
PERFORMf_monta_alvUSING'VBELN''14''ORDEMVENDA''X'''.
PERFORMf_monta_alvUSING'MATNR''20''MATERIAL'''''.
PERFORMf_monta_alvUSING'NETWR''20''VALORLIQ.''''X'.
ENDFORM." F_DECLARA_ALV
*&---------------------------------------------------------------------*
*&
Form F_MONTA_ALV
*&---------------------------------------------------------------------*
FORMf_monta_alvUSINGp1TYPElvc_s_fcat-fieldname
p2TYPElvc_s_fcat-outputlen
p3TYPElvc_s_fcat-coltext
p4TYPElvc_s_fcat-hotspot
p5TYPElvc_s_fcat-do_sum.
s_fieldcat-fieldname=p1."Nome do campo
s_fieldcat-outputlen=p2."Tamanho da coluna
s_fieldcat-coltext=p3."Label da coluna
s_fieldcat-hotspot=p4."Hotspot
s_fieldcat-do_sum=p5."Soma
APPENDs_fieldcatTOt_fieldcat.
ENDFORM."F_MONTA_ALV

WAFbricaABAP

Page19de22
WAConsultoria

20
Fbrica Software ABAP ALV OO

*&---------------------------------------------------------------------*
*&
Form F_DECLARA_SORT
*&---------------------------------------------------------------------*
FORMf_declara_sortUSINGp1TYPElvc_s_sort-fieldname
p2TYPElvc_s_sort-subtot.
CLEARs_sort.
s_sort-fieldname=p1."Nome do campo
s_sort-subtot=p2."Subtotal
APPENDs_sortTOt_sort.
ENDFORM." F_DECLARA_SORT

*&---------------------------------------------------------------------*
*&
Form GRID
*&---------------------------------------------------------------------*
FORMf_grid.
IFcontainerISINITIALANDt_vbap[]ISNOTINITIAL.
CREATEOBJECTcontainerEXPORTING
container_name='CONTAINER'.
CREATEOBJECTgridEXPORTING
i_parent=container.
"Metodo para exibir o grid
CALLMETHODgrid->set_table_for_first_display
EXPORTING
is_variant=gs_variant
i_save='A'
is_layout=gs_layout
CHANGING
it_outtab=t_vbap
it_sort=t_sort
it_fieldcatalog=t_fieldcat.
"Metodo de hotspot
CREATEOBJECTevent_receiver.
SETHANDLERevent_receiver->hotspot_clickFORgrid.
ELSE.
"Metodo de refresh do ALV
CALLMETHODgrid->refresh_table_display.
ENDIF.
ENDFORM." F_GRID

WAFbricaABAP

Page20de22
WAConsultoria

21
Fbrica Software ABAP ALV OO
*&---------------------------------------------------------------------*
*&
Form F_BUSCAR_LAYOUTS
*&---------------------------------------------------------------------*
FORMf_buscar_layouts.
ls_key-report=sy-repid.
ls_layout=cl_salv_layout_service=>f4_layouts(s_key=ls_key
restrict=if_salv_c_layout=>restrict_none).
p_vari=ls_layout-layout.
ENDFORM." F_BUSCAR_LAYOUTS

WAFbricaABAP

Page21de22
WAConsultoria

22
Fbrica Software ABAP ALV OO

Resultado:

Boto: SAIR
Cd. Fnc. : BT_SAIR
Boto: DELETAR
Cd. Fnc. : BT_DEL

Container: CONTAINER

Tela 1001

WAFbricaABAP

Page22de22
WAConsultoria