You are on page 1of 7

Working on ALV with Dynamic

Internal Tables

Applies to:
SAP ECC 6.0 or higher. For more information, visit the ABAP homepage.

Summary
This article demonstrates usage of advanced ALV features like editable ALV at cell level when working with
dynamic internal tables.

Author: Nirav Goradia


Company: Larsen & Toubro Infotech Ltd.
Created on: 21 September 2010

Author Bio
Nirav Goradia is a Technical Consultant working with L&T Infotech, Mumbai, India. His SAP
experience of over 4 years spans various technologies like ABAP, Webdynpro for ABAP,
Webdynpro for Java, Adobe Flex.

SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com | UAC - uac.sap.com
© 2010 SAP AG 1
Working on ALV with Dynamic Internal Tables

Table of Contents
Introduction ......................................................................................................................................................... 3
Create field catalog for ALV ................................................................................................................................ 3
Create field catalog for dynamic internal table ................................................................................................... 3
Populate data in dynamic internal table .............................................................................................................. 4
Display ALV on screen ....................................................................................................................................... 5
Related Content .................................................................................................................................................. 6
Disclaimer and Liability Notice ............................................................................................................................ 7

SAP COMMUNITY NETWORK SDN - sdn.sap.com


© 2010 SAP AG 2
Working on ALV with Dynamic Internal Tables

Introduction
ALV stands for Advanced List Viewer, mainly used to enhance the output of a report. ALV comes with set of
functions that enhance the readability and functionality of ALV report. One important function that we will be
focusing on in this article is creating editable ALV at cell level. Though creating an editable ALV is a straight-
forward process, things can become little tricky if data displayed in ALV is coming from dynamic internal
table and we need to make only certain rows of ALV as editable. We will see in detail, things to take care
when working on ALV with dynamic internal tables.

Create field catalog for ALV


Field catalog define the structure and attributes of data to be displayed on an ALV report. There are two
commonly used methods to create field catalog.
1. Using LVC_FIELDCATALOG_MERGE
2. Populate structure LVC_T_FIELDCAT field by field as below
DATA wa_fieldcat TYPE lvc_s_fcat.
DATA it_fieldcat TYPE lvc_t_fcat.

wa_fieldcat-fieldname = 'FIELD1'.
wa_fieldcat-domname = 'CHAR10'.
wa_fieldcat-edit = 'X'.
APPEND wa_fieldcat TO it_fieldcat.

wa_fieldcat-fieldname = 'FIELD2'.
wa_fieldcat-domname = 'CHAR10'.
wa_fieldcat-edit = 'X'.
APPEND wa_fieldcat TO it_fieldcat.

Create field catalog for dynamic internal table


For creating dynamic internal table, you will also need to create a field catalog to define the structure of the
internal table. One can use the same field catalog created above for ALV or create the field catalog
separately. In our scenario, we will see how to create a field catalog for dynamic internal table separately.
Note that in below example, we are creating a table with 2 data fields and CELLTAB field for ALV
customizing at cell level. We have considered only 2 data fields to keep the scenario simple. Based on the
requirement, there can be any number of fields in the table. Also, assumption here is that number of fields
will be variable. That is the sole reason for going by dynamic internal tables.
DATA: it_field_dom TYPE cl_abap_structdescr=>component_table,
wa_field_dom LIKE LINE OF int_field_dom.

*-Table description for dynamic table


DATA: wr_struct_descr TYPE REF TO cl_abap_structdescr,
wr_tab_descr TYPE REF TO cl_abap_tabledescr.

*- Field symbols for dynamic internal table


FIELD-SYMBOLS: <it_table> TYPE table,
<wa_table> TYPE ANY.

“cl_abap_structdescr=>component_table will have all the components of dynamic


“internal table

wa_field_dom-name = 'FIELD1'.
wa_field_dom-type ?= cl_abap_datadescr=>describe_by_name(

SAP COMMUNITY NETWORK SDN - sdn.sap.com


© 2010 SAP AG 3
Working on ALV with Dynamic Internal Tables

'CHAR10' ).
APPEND wa_field_dom TO it_field_dom.

wa_field_dom-name = 'FIELD2'.
wa_field_dom-type ?= cl_abap_datadescr=>describe_by_name(
'CHAR10' ).
APPEND wa_field_dom TO it_field_dom.

“ Important step: to add CELLTAB field to the dynamic internal table


“ This field will be required to control the ALV settings at cell level.

wa_field_dom-name = 'CELLTAB'.
wa_field_dom-type ?= cl_abap_datadescr=>describe_by_name(
'LVC_T_STYL' ).
APPEND wa_field_dom TO it_field_dom.

***Create line structure


CALL METHOD cl_abap_structdescr=>create
EXPORTING
p_components = it_field_dom
p_strict = cl_abap_structdescr=>false
RECEIVING
p_result = wr_struct_descr.

**Create table type based on the structure


CALL METHOD cl_abap_tabledescr=>create
EXPORTING
p_line_type = wr_struct_descr
p_table_kind = cl_abap_tabledescr=>tablekind_std
RECEIVING
p_result = wr_tab_descr.

**Assign the tables


CREATE DATA wr_table_data TYPE HANDLE wr_tab_descr.
ASSIGN wr_table_data->* TO <it_table>.

CREATE DATA wr_line_data TYPE HANDLE wr_struct_descr.


ASSIGN wr_line_data->* TO <wa_table>.

Populate data in dynamic internal table


Once the internal tables are ready from the step above, next task would be to populate the data in the tables.
For populating data to dynamic structure, you need to have data ready in the static internal table. What we
will do in this step, is to convert the data from static internal table format to dynamic structure which will be
displayed on ALV. Critical part here is to populate CELLTAB table, since it is nested within the dynamic
internal table

DATA wa_style TYPE lvc_s_styl.

FIELD-SYMBOLS: <wa_comp> TYPE ANY,


<it_comp> TYPE SORTED TABLE .

“ it_data_table is the internal table from which data needs to be populated in


“ ALV
LOOP AT it_data_table.
ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <wa_table> TO <fs_comp>.

SAP COMMUNITY NETWORK SDN - sdn.sap.com


© 2010 SAP AG 4
Working on ALV with Dynamic Internal Tables

IF sy-subrc = 0.
<fs_comp> = it_data_table-field1.
ENDIF.

ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_table> TO <fs_comp>.


IF sy-subrc = 0.
<fs_comp> = it_data_table-field2.
ENDIF.

ASSIGN COMPONENT 'CELLTAB' OF STRUCTURE <wa_table> TO <it_comp>.


REFRESH <it_comp>.
IF sy-subrc = 0 AND it_data_table-field1 < 100.
wa_style-fieldname = 'FIELD1'.
wa_style-style = cl_gui_alv_grid=>mc_style_disabled.
APPEND wa_style TO <it_comp>.
ENDIF.
APPEND <wa_table> TO <it_table>.
ENDLOOP.

Display ALV on screen


This step remains same as you would do for any ALV grid using CL_GUI_ALV_GRID class. Instead of static
internal table name, you will be passing the field symbol for data.
*--- Display ALV Grid Report
CALL METHOD wf_grid->set_table_for_first_display
EXPORTING
is_layout = wa_layout
CHANGING
it_outtab = <it_table>
it_fieldcatalog = it_fieldcat[].

Make sure you have provided the style field name in the layout structure as CELLTAB viz. name of the field
of type 'LVC_T_STYL' that is part of the internal table.

wa_layout-stylefname = 'CELLTAB'.

Since we are working with field symbols here, it is mandatory to put check at all relevant places to make sure
field symbols are assigned to avoid runtime errors. Such case is more likely to occur when there is no data to
be displayed in ALV.

SAP COMMUNITY NETWORK SDN - sdn.sap.com


© 2010 SAP AG 5
Working on ALV with Dynamic Internal Tables

Related Content
Simple ALV list for beginners
ALV Grid Control
ABAP Development and Programming
For more information, visit the ABAP homepage.

SAP COMMUNITY NETWORK SDN - sdn.sap.com


© 2010 SAP AG 6
Working on ALV with Dynamic Internal Tables

Disclaimer and Liability Notice


This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not
supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document,
and anyone using these methods does so at his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or
code sample, including any liability resulting from incompatibility between the content within this document and the materials and
services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this
document.

SAP COMMUNITY NETWORK SDN - sdn.sap.com


© 2010 SAP AG 7

You might also like