Inicio > ALV, Reports > Mostrar un alv en pocas líneas

Mostrar un alv en pocas líneas

A veces, se nos requiere que construyamos un listado ALV para mostrar los datos de una tabla, unión de tablas o informes un poco más elaborados. Para la visualización de ese informe, desde tiempos inmemoriables se ha utilizado las típicas funciones REUSE_ALV_GRID_DISPLAY y sucedáneos. Esto pertenece ya al pasado. Sap avanza con la programación y nos proporciona herramientas nuevas, una de ellas una clase muy especial la CL_SALV_TABLE. Con ella podemos contruirnos un informe con tan solo llamar a un método el factory y luego al display. Os pongo el siguiente ejemplo de muestra

*&———————————————————————*
*& Report  ZNUMERO_POSICIONES_DOC
*&
*&———————————————————————*
*&
*& Programa que muestra el número de posiciones por clase de documento
*&   y usuario
*&———————————————————————*

report  zlistado_alv_posiciones.

tables: bkpf, t003t.

select-options: s_bukrs for bkpf-bukrs,
s_gjahr for bkpf-bukrs,
s_blart for bkpf-blart,
s_monat for bkpf-monat,
s_usnam for bkpf-usnam.

data: begin of gt_bkpf occurs 0,
bukrs like bkpf-bukrs,
blart like bkpf-blart,
usnam like bkpf-usnam,
belnr like bkpf-belnr,
gjahr like bkpf-gjahr.
data: end of gt_bkpf.

data: wa_bkpf like gt_bkpf.

types: begin of tt_salida,
bukrs type bukrs,
blart type blart,
ltext type t003t-ltext,
usnam type uname,
contador type i,
end of tt_salida.

data: gt_salida type standard table of tt_salida,
wa_salida type tt_salida,
contador type p,
lv_count type p,
lv_columns type ref to cl_salv_columns_table,
lv_column  type ref to cl_salv_column,
lv_alv_object type ref to cl_salv_table,
g_functions type ref to cl_salv_functions.

start-of-selection.
refresh: gt_bkpf, gt_salida.

select blart belnr bukrs gjahr usnam
from bkpf
into corresponding fields of table gt_bkpf
where bukrs in s_bukrs and
gjahr in s_gjahr and
blart in s_blart and
monat in s_monat and
usnam in s_usnam.

sort gt_bkpf by bukrs blart usnam.

loop at gt_bkpf.

clear: wa_bkpf, wa_salida.
move-corresponding gt_bkpf to wa_bkpf.

clear lv_count.
select single count( * )
from   bseg
into   lv_count
where  belnr = gt_bkpf-belnr and
bukrs = gt_bkpf-bukrs and
gjahr = gt_bkpf-gjahr.

contador = contador + lv_count.

at end of usnam.

select single *
from t003t
where blart = wa_bkpf-blart and
spras = ‘S’.

move-corresponding wa_bkpf to wa_salida.
wa_salida-ltext = t003t-ltext.
wa_salida-contador = contador.
append wa_salida to gt_salida.
clear: wa_salida.

clear contador.

endat.

endloop.

if gt_salida[] is not initial.

cl_salv_table=>factory(
importing
r_salv_table = lv_alv_object
changing
t_table = gt_salida
).

g_functions = lv_alv_object->get_functions( ).
g_functions->set_all( abap_true ).

lv_columns = lv_alv_object->get_columns( ).
lv_columns->set_optimize( ‘X’ ).

** Cambiamos el nombre de la columna contador para que saque su descripción
lv_column = lv_columns->get_column(‘CONTADOR’).
lv_column->set_long_text( text-001 ).
lv_column->set_medium_text( text-001 ).
lv_column->set_short_text( text-001 ).
** Visualizamos el ALV
lv_alv_object->display( ).
endif.

Categorías: ALV, Reports Etiquetas: , , ,
  1. Amparo
    14 diciembre, 2011 a las 1:39 am

    Muchas gracias por el ejemplo, me sirvio mucho! :)

  2. 9 febrero, 2013 a las 10:40 pm

    Hello there, I found your website via Google whilst looking for a comparable matter, your website
    got here up, it appears to be like good. I’ve bookmarked it in my google bookmarks.
    Hello there, simply was aware of your weblog through Google, and found that it is really informative. I’m gonna be careful for brussels.
    I’ll appreciate for those who continue this in future. Lots of other folks shall be benefited out of your writing. Cheers!

  1. No trackbacks yet.

Replica a www.slideshare.net Cancelar la respuesta