Muchas veces he tenido que desarrollar ALV's dinámicos, los casos en lo que lo he necesitado es cuando he tenido que mostrar data con meses como columnas o días, tipo calendario donde muestre valores o importes por mes, años o días.
Para dar un ejemplo de como implementar he desarrollado un código de prueba que pueden probar y adaptar según sus necesidades.
El código es el siguiente:
TYPE-POOLS: slis.
FIELD-SYMBOLS: <t_dyntable> TYPE STANDARD TABLE,
<t_prueba> TYPE STANDARD TABLE, " Dynamic internal table name
<fs_dyntable>,
<t_dinamic>, " Field symbol to create work area
<fs_fldval> TYPE any. " Field symbol to assign values
PARAMETERS: p_cols(5) TYPE c, " Input number of columns
p_rows(5) TYPE c.
DATA: t_newtable TYPE REF TO data,
t_newline TYPE REF TO data,
t_newline2 TYPE REF TO data,
fs_fldcat TYPE slis_t_fieldcat_alv,
t_fldcat TYPE lvc_t_fcat,
wa_it_fldcat TYPE lvc_s_fcat,
wa_colno(2) TYPE n,
wa_flname(5) TYPE c.
* Create fields .
wa_it_fldcat-fieldname = 'CAMPO1'.
wa_it_fldcat-datatype = 'CHAR'.
wa_it_fldcat-intlen = 10.
APPEND wa_it_fldcat TO t_fldcat.
wa_it_fldcat-fieldname = 'CAMPO2'.
wa_it_fldcat-datatype = 'CHAR'.
wa_it_fldcat-intlen = 10.
APPEND wa_it_fldcat TO t_fldcat.
DO p_cols TIMES.
CLEAR wa_it_fldcat.
MOVE sy-index TO wa_colno.
CONCATENATE 'COL'
wa_colno
INTO wa_flname.
wa_it_fldcat-fieldname = wa_flname.
wa_it_fldcat-datatype = 'CHAR'.
wa_it_fldcat-intlen = 10.
APPEND wa_it_fldcat TO t_fldcat.
ENDDO.
* Crea tabla interna dinamica y asigna a FieldSymbols
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = t_fldcat
IMPORTING
ep_table = t_newtable.
ASSIGN t_newtable->* TO <t_dyntable>.
* Create dynamic work area and assign to FS
CREATE DATA t_newline LIKE LINE OF <t_dyntable>.
ASSIGN t_newline->* TO <fs_dyntable>.
************************
** Tablas
************************
DATA: fieldname(20) TYPE c.
DATA: fieldvalue(10) TYPE c.
DATA: lv_numcol TYPE i.
DATA: lv_numfil TYPE i.
DATA: INDEX2 TYPE char3,
acu TYPE CHAR3,
lv_acu TYPE i.
data: x_filcat LIKE LINE OF t_fldcat.
DATA : ti_details TYPE abap_compdescr_tab, "<- Aqui tenemos la estructura.
wa_details TYPE abap_compdescr.
DATA : ref_table_des TYPE REF TO cl_abap_structdescr.
types: BEGIN OF TY_PRUEBA,
INDEx TYPE CHAR5,
codigo type char1,
END OF TY_PRUEBA.
types: itty TYPE ty_prueba.
DATA: IT TYPE TY_PRUEBA,
TB TYPE STANDARD TABLE OF ITTY,
WA LIKE LINE OF tb.
FIELD-SYMBOLS: <f> LIKE LINE OF t_fldcat.
ref_table_des ?= cl_abap_typedescr=>describe_by_data( IT ).
*ref_table_des ?= cl_abap_typedescr=>DESCRIBE_BY_NAME( wa-index ).
ti_details[] = ref_table_des->components[].
DESCRIBE TABLE t_fldcat LINES lv_numcol.
READ TABLE t_fldcat into x_filcat INDEX 1.
DO p_rows TIMES.
index2 = sy-index.
lv_acu = 0.
DO p_cols TIMES.
while lv_acu < lv_numcol.
lv_acu = lv_acu + 1.
acu = lv_acu.
condense ACU NO-GAPS.
READ TABLE t_fldcat into wa_it_fldcat index lv_acu.
ASSIGN COMPONENT lv_acu OF STRUCTURE t_fldcat TO <f>.
" Se Asigna el valor
CONCATENATE 'VALUE '
index2
'-'
acu
INTO fieldvalue.
CONDENSE fieldvalue NO-GAPS.
ASSIGN COMPONENT wa_it_fldcat-fieldname
OF STRUCTURE <fs_dyntable>
TO <fs_fldval>.
<fs_fldval> = fieldvalue.
ENDWHILE.
ENDDO.
APPEND <fs_dyntable> TO <t_dyntable>.
ENDDO.
************************
** Muestra tabla interna
************************
DATA: wa_cat LIKE LINE OF fs_fldcat.
wa_cat-fieldname = 'CAMPO1'.
wa_cat-seltext_s = 'CAMPO1'.
wa_cat-outputlen = '10'.
APPEND wa_cat TO fs_fldcat.
wa_cat-fieldname = 'CAMPO2'.
wa_cat-seltext_s = 'CAMPO2'.
wa_cat-outputlen = '10'.
APPEND wa_cat TO fs_fldcat.
DO p_cols TIMES.
CLEAR wa_cat.
MOVE sy-index TO wa_colno.
CONCATENATE 'COL'
wa_colno
INTO wa_flname.
wa_cat-fieldname = wa_flname.
wa_cat-seltext_s = wa_flname.
wa_cat-outputlen = '10'.
APPEND wa_cat TO fs_fldcat.
ENDDO.
* Muestra lista
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat = fs_fldcat
TABLES
t_outtab = <t_dyntable>."<t_dyntable>.
0 comentarios:
Publicar un comentario