Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

7 ALV Reports

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24
At a glance
Powered by AI
ALV stands for ABAP List Viewer. It provides a standard list format and user interface for ABAP reports and allows functions like sorting, filtering, downloading to excel etc.

ALV stands for ABAP List Viewer. It gives a standard list format and user interface to all ABAP reports. Some key functions include sorting, filtering, totals, changing column order and hiding columns.

To create a simple ALV report, you include the SLIS type pool, retrieve data into an internal table, build a field catalog, and pass the data table and field catalog to an ALV function module like REUSE_ALV_GRID_DISPLAY.

What is SAP ALV?

ALV stands for ABAP List Viewer. ALV gives us a standard List format and user interface to all our ABAP reports.
ALV is created by a set of standard function modules provided by SAP.
ALV provides a lot of inbuilt functions to our reports and some of the functions are listed below.

Sorting of records
Filtering of records
Totals and Sub-totals
Download the report output to Excel/HTML
Changing the order of the columns in the report
Hide the unwanted columns from the report
Because of the above functions, ALV substantially decreases the report development time. ALV takes care of
rendering the list and we can concentrate only on the data retrieval part.
Some of the function modules used to create ALV reports are listed below.
FUNCTION MODULE

DESCRIPTION

REUSE_ALV_LIST_DISPLAY

Display an ALV list

REUSE_ALV_GRID_DISPLAY

Display an ALV grid

REUSE_ALV_COMMENTARY_WRITE

Output List header information

REUSE_ALV_VARIANT_F4

Display variant selection dialog box

REUSE_ALV_VARIANT_EXISTENCE

Checks whether a variant exists

REUSE_ALV_FIELDCATALOG_MERGE

Create field catalog from dictionary structure or internal table

Simple ALV report.


DATA: it_spfli TYPE TABLE OF spfli.

SELECT * FROM spfli INTO TABLE it_spfli.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


EXPORTING
i_structure_name = 'SPFLI'
TABLES
t_outtab
Simple ALV Output List

= it_spfli.

Create a Simple SAP ALV Report


An ALV report is created using the standard function modules provided by SAP.
An ALV report can be created using the following steps.

Include SLIS type pool SLIS type pool contains all the data types required by ALV function modules.
Data retrieval Code the logic to fetch the data from database table into an Internal Table.
Build Field Catalog Add the columns into an internal that you want to display in the ALV output list.
Pass the data table and field catalog table to ALV function module
TYPE-POOLS: slis.

" SLIS contains all the ALV data types

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook

TYPE TABLE OF sbook.

DATA: it_fieldcat

TYPE slis_t_fieldcat_alv,

wa_fieldcat

TYPE slis_fieldcat_alv.

*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.

*Fetch data from the database


SELECT * FROM sbook INTO TABLE it_sbook.
*Build field catalog
wa_fieldcat-fieldname

= 'CARRID'.

" Fieldname in the data table

wa_fieldcat-seltext_m

= 'Airline'.

" Column description in the output

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'CONNID'.

wa_fieldcat-seltext_m

= 'Con. No.'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'FLDATE'.

wa_fieldcat-seltext_m

= 'Date'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'BOOKID'.

wa_fieldcat-seltext_m

= 'Book. ID'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'PASSNAME'.

wa_fieldcat-seltext_m

= 'Passenger Name'.

APPEND wa_fieldcat TO it_fieldcat.


*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat

= it_fieldcat

TABLES
t_outtab

= it_sbook

EXCEPTIONS
program_error = 1
OTHERS
Output

= 2.

Field Catalog in SAP ALV


The field catalog is a table which contains information on the fields to be displayed on the ALV output.
First we need to build a field catalog before displaying any output in the ALV. We have the following three ways
to build a field catalog.

Automatically through a Data Dictionary structure.


Manually in ABAP program.
Semi-automatically by combining the above two procedures.
The third option is used in the following cases.

We want to display all the fields from the DDIC structure but want to modify certain attributes like
descriptions etc.

We want to display most of the fields i.e. we want to hide certain fields.
Add new fields.
To generate a field catalog semi-automatically:

Declare an internal table of type SLIS_T_FIELDCAT_ALV.


Call function module REUSE_ALV_FIELDCATALOG_MERGE and pass the DDIC structure of the
output table and the internal table for the field catalog. The function module generates the field catalog and fills
the internal table accordingly.

Read the rows you want to change, and adapt the fields accordingly. If your output table contains more
fields than are stored in the Data Dictionary, you must append one row for each new field to the field catalog.
Example Program

TYPE-POOLS: slis.

" SLIS contains all the ALV data types

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*

DATA: BEGIN OF wa_sbook,


status(4).
INCLUDE STRUCTURE sbook.
DATA: END OF wa_sbook.
DATA: it_sbook

TYPE TABLE OF sbook.

DATA: it_fieldcat

TYPE slis_t_fieldcat_alv,

wa_fieldcat

TYPE slis_fieldcat_alv.

*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.

*Fetch data from the database


SELECT * UP TO 10 ROWS FROM sbook INTO TABLE it_sbook.

*Build field catalog


wa_fieldcat-fieldname

= 'STATUS'.

" Fieldname in the data table

wa_fieldcat-seltext_m

= 'Status'.

" Column description in the output

APPEND wa_fieldcat TO it_fieldcat.

*Merge Field Catalog

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'


EXPORTING
i_structure_name

= 'SBOOK'

CHANGING
ct_fieldcat

= it_fieldcat

EXCEPTIONS
inconsistent_interface

= 1

program_error

= 2

OTHERS

= 3.

*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat

= it_fieldcat

TABLES
t_outtab

= it_sbook

EXCEPTIONS
program_error = 1
OTHERS

Output

= 2.

Status column is added to the field catalog along all the fields of structure SBOOK.

Display Header in ABAP ALV Grid


To display header in ALV grid use the following steps.
1.

Build the ALV header table.

2.

Pass

the

TOP-OF-EVENT

subroutine

name

and

callback

program

name

to

I_CALLBACK_TOP_OF_PAGE and I_CALLBACK_PROGRAM parameters of REUSE_ALV_GRID_DISPLAY


function module.
3.

Display the ALV header by passing the ALV header table built in

step one to function module

REUSE_ALV_COMMENTARY_WRITE inside TOP-OF-EVENT subroutine.


TYPE-POOLS: slis.
*----------------------------------------------------------------------*
*

Data Decalaration

*----------------------------------------------------------------------*
DATA: it_spfli TYPE TABLE OF spfli.
DATA: g_repid TYPE sy-repid.
DATA: it_listheader TYPE slis_t_listheader,
wa_listheader TYPE slis_listheader.
*----------------------------------------------------------------------*
*

START-OF-SELECTION

*----------------------------------------------------------------------*
START-OF-SELECTION.

g_repid = sy-repid.

SELECT * FROM spfli INTO TABLE it_spfli.

PERFORM build_alv_header.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


EXPORTING
i_callback_program

= g_repid

i_callback_top_of_page = 'TOP_OF_PAGE'
i_structure_name

= 'SPFLI'

TABLES
t_outtab

= it_spfli.

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

Form

BUILD_ALV_HEADER

*&---------------------------------------------------------------------*
FORM build_alv_header .

Type H is used to display headers i.e. big font


wa_listheader-typ

= 'H'.

wa_listheader-info ='Flight Details'.


APPEND wa_listheader TO it_listheader.
CLEAR wa_listheader.

Type S is used to display key and value pairs


wa_listheader-typ = 'S'.

wa_listheader-key = 'Date :' .


CONCATENATE

sy-datum+6(2)
sy-datum+4(2)
sy-datum(4)
INTO wa_listheader-info
SEPARATED BY '/'.

APPEND wa_listheader TO it_listheader.


CLEAR wa_listheader.

Type A is used to display italic font


wa_listheader-typ = 'A'.
wa_listheader-info ='SAP ALV Report'.
APPEND wa_listheader TO it_listheader.
CLEAR wa_listheader.

ENDFORM.

" BUILD_ALV_HEADER

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

Form

top_of_page

*&---------------------------------------------------------------------*
FORM top_of_page.

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'


EXPORTING
it_list_commentary = it_listheader.

ENDFORM.

Output

"top_of_page

Display Logo in ABAP ALV Header


Use the below steps to display the logo in ABAP ALV header.
1.

First upload the logo that you want to display in ABAP ALV header.

2.

Build the ALV header table.

3.

Pass

the

TOP-OF-EVENT

subroutine

name

and

callback

program

name

to

I_CALLBACK_TOP_OF_PAGE and I_CALLBACK_PROGRAM parameters of REUSE_ALV_GRID_DISPLAY


function module.
4.

Display the ALV header and logo by passing the ALV header table built in step two and logo that you
uploaded

in step one to function module REUSE_ALV_COMMENTARY_WRITE inside TOP-OF-EVENT

subroutine.
TYPE-POOLS: slis.
*----------------------------------------------------------------------*
*

Data Decalaration

*----------------------------------------------------------------------*
DATA: it_spfli TYPE TABLE OF spfli.
DATA: g_repid TYPE sy-repid.
DATA: it_listheader TYPE slis_t_listheader,
wa_listheader TYPE slis_listheader.
*----------------------------------------------------------------------*
*

START-OF-SELECTION

*----------------------------------------------------------------------*
START-OF-SELECTION.
g_repid = sy-repid.
SELECT * FROM spfli INTO TABLE it_spfli.
PERFORM build_alv_header.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


EXPORTING
i_callback_program

= g_repid

i_callback_top_of_page = 'TOP_OF_PAGE'
i_structure_name

= 'SPFLI'

TABLES
t_outtab

= it_spfli.

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

Form

BUILD_ALV_HEADER

*&---------------------------------------------------------------------*
FORM build_alv_header .
*

Type H is used to display headers i.e. big font


wa_listheader-typ

= 'H'.

wa_listheader-info ='Flight Details'.


APPEND wa_listheader TO it_listheader.
CLEAR wa_listheader.
*

Type S is used to display key and value pairs


wa_listheader-typ = 'S'.
wa_listheader-key = 'Date :' .
CONCATENATE

sy-datum+6(2)
sy-datum+4(2)
sy-datum(4)
INTO wa_listheader-info
SEPARATED BY '/'.

APPEND wa_listheader TO it_listheader.


CLEAR wa_listheader.
*

Type A is used to display italic font


wa_listheader-typ = 'A'.
wa_listheader-key = 'Date

:' .

wa_listheader-info ='SAP ALV Report'.


APPEND wa_listheader TO it_listheader.
CLEAR wa_listheader.
ENDFORM.

" BUILD_ALV_HEADER

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

Form

top_of_page

*&---------------------------------------------------------------------*
FORM top_of_page.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = it_listheader
i_logo

= 'MYLOGO'.

ENDFORM.

"top_of_page

Output

Display Subtotal and Total in ABAP ALV Grid


Use below steps to calculate totals in ABAP ALV.
1.

Build field catalog and set the field DO_SUM of field catalog to X for the field for which you want
calculate the totals.

2.

Pass field catalog to function module REUSE_ALV_GRID_DISPLAY.


TYPE-POOLS: slis.

" SLIS contains all the ALV data types

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook

TYPE TABLE OF sbook.

DATA: it_fieldcat

TYPE slis_t_fieldcat_alv,

wa_fieldcat

TYPE slis_fieldcat_alv.

*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
*Fetch data from the database
SELECT * FROM sbook INTO TABLE it_sbook.
*Build field catalog
wa_fieldcat-fieldname

= 'CARRID'.

" Fieldname in the data table

wa_fieldcat-seltext_m

= 'Airline'.

" Column description in the output

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'CONNID'.

wa_fieldcat-seltext_m

= 'Con. No.'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'FLDATE'.

wa_fieldcat-seltext_m

= 'Date'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'BOOKID'.

wa_fieldcat-seltext_m

= 'Book. ID'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'PASSNAME'.

wa_fieldcat-seltext_m

= 'Passenger Name'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'LOCCURAM'.

wa_fieldcat-seltext_m

= 'Price'.

*Calculate Total for Price


wa_fieldcat-do_sum

= 'X'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'LOCCURKEY'.

wa_fieldcat-seltext_m

= 'Currency'.

APPEND wa_fieldcat TO it_fieldcat.


*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat

= it_fieldcat

TABLES
t_outtab

= it_sbook

EXCEPTIONS
program_error = 1
OTHERS
Output

= 2.

Subtotal in ABAP ALV


To calculate subtotal in ALV, build sort catalog and set the field SUBTOT of sort catalog to X.
TYPE-POOLS: slis.

" SLIS contains all the ALV data types

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook

TYPE TABLE OF sbook.

DATA: it_fieldcat

TYPE slis_t_fieldcat_alv,

wa_fieldcat

TYPE slis_fieldcat_alv,

it_sort

TYPE slis_t_sortinfo_alv,

wa_sort

TYPE slis_sortinfo_alv..

*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
*Fetch data from the database
SELECT * FROM sbook INTO TABLE it_sbook.
*Build field catalog
wa_fieldcat-fieldname

= 'CARRID'.

" Fieldname in the data table

wa_fieldcat-seltext_m

= 'Airline'.

" Column description in the output

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'CONNID'.

wa_fieldcat-seltext_m

= 'Con. No.'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'FLDATE'.

wa_fieldcat-seltext_m

= 'Date'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'BOOKID'.

wa_fieldcat-seltext_m

= 'Book. ID'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'PASSNAME'.

wa_fieldcat-seltext_m

= 'Passenger Name'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'LOCCURAM'.

wa_fieldcat-seltext_m

= 'Price'.

wa_fieldcat-do_sum

= 'X'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'LOCCURKEY'.

wa_fieldcat-seltext_m

= 'Currency'.

APPEND wa_fieldcat TO it_fieldcat.


*Build sort catalog
wa_sort-spos

= 1.

wa_sort-fieldname = 'CARRID'.
wa_sort-up

= 'X'.

wa_sort-subtot

= 'X'.

APPEND wa_sort TO it_sort.


*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
it_fieldcat

= it_fieldcat

it_sort

= it_sort

TABLES
t_outtab

= it_sbook

EXCEPTIONS
program_error = 1
OTHERS

= 2.

Output

If you want to calculate subtotals for more fields, just add a record in sort field catalog for each field.
*Build sort catalog
wa_sort-spos

= 1.

wa_sort-fieldname = 'CARRID'.
wa_sort-up

= 'X'.

wa_sort-subtot

= 'X'.

APPEND wa_sort TO it_sort.


wa_sort-spos

= 2.

wa_sort-fieldname = 'CONNID'.
wa_sort-up

= 'X'.

wa_sort-subtot

= 'X'.

APPEND wa_sort TO it_sort.


Output

Disable Icons in SAP ALV Toolbar


Use below steps to disable icons in ABAP ALV toolbar.
1.

Fetch data from database table.

2.

Build field catalog and fill the excluding table with function codes of icons that you want to disable.
&ILT& is the function code for Filter Icon.

3.

Pass field catalog and excluding table to function module REUSE_ALV_GRID_DISPLAY.


TYPE-POOLS: slis.

" SLIS contains all the ALV data types

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook

TYPE TABLE OF sbook.

DATA: it_fieldcat

TYPE slis_t_fieldcat_alv,

wa_fieldcat

TYPE slis_fieldcat_alv.

DATA: it_excluding TYPE


wa_excluding TYPE
DATA: g_repid

slis_t_extab,
slis_extab.

TYPE sy-repid.

*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
g_repid = sy-repid.
*Fetch data from the database
SELECT * FROM sbook INTO TABLE it_sbook.
*Build field catalog
wa_fieldcat-fieldname

= 'CARRID'.

" Fieldname in the data table

wa_fieldcat-seltext_m

= 'Airline'.

" Column description in the output

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'CONNID'.

wa_fieldcat-seltext_m

= 'Con. No.'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'FLDATE'.

wa_fieldcat-seltext_m

= 'Date'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'BOOKID'.

wa_fieldcat-seltext_m

= 'Book. ID'.

APPEND wa_fieldcat TO it_fieldcat.


wa_fieldcat-fieldname

= 'PASSNAME'.

wa_fieldcat-seltext_m

= 'Passenger Name'.

APPEND wa_fieldcat TO it_fieldcat.


*Build excluding table. Append the function codes of the icons to
*excluding table that you want to disable

wa_excluding-fcode = '&ILT'.

" Function code for filter icon

APPEND wa_excluding TO it_excluding.


*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = g_repid
it_fieldcat

= it_fieldcat

it_excluding

= it_excluding

TABLES
t_outtab

= it_sbook

EXCEPTIONS
program_error

= 1

OTHERS

= 2.

Currency wise and Quantity wise Totals in SAP ALV


Use below steps to disable icons in ABAP ALV toolbar.
1.

Fetch data from database table.

2.

Build field catalog.

3.

To get the currency wise totals of price, fill the CFIELDNAME field of field catalog with the currency
column name and set do_sum to X.

4.

To get the quantity wise totals of luggage, fill the QFIELDNAME field of field catalog with the weight unit
column name and set do_sum to X.

5.

Pass field catalog to function module REUSE_ALV_GRID_DISPLAY.


TYPE-POOLS: slis.

" SLIS contains all the ALV data types

*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook

TYPE TABLE OF sbook.

DATA: it_fieldcat

TYPE slis_t_fieldcat_alv,

wa_fieldcat
DATA: g_repid

TYPE slis_fieldcat_alv.
TYPE sy-repid.

*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.

g_repid = sy-repid.
*Fetch data from the database
SELECT * UP TO 20 ROWS FROM sbook INTO TABLE it_sbook.
*Build field catalog
wa_fieldcat-fieldname

= 'CARRID'.

" Fieldname in the data table

wa_fieldcat-seltext_m

= 'Airline'.

" Column description in the output

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'CONNID'.

wa_fieldcat-seltext_m

= 'Con. No.'.

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'FLDATE'.

wa_fieldcat-seltext_m

= 'Date'.

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'BOOKID'.

wa_fieldcat-seltext_m

= 'Book. ID'.

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'FORCURAM'.

wa_fieldcat-seltext_m

= 'Price'.

wa_fieldcat-do_sum

= 'X'.

wa_fieldcat-cfieldname = 'FORCURKEY'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'FORCURKEY'.

wa_fieldcat-seltext_m

= 'Currency'.

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'LUGGWEIGHT'.

wa_fieldcat-seltext_m

= 'Weight'.

wa_fieldcat-do_sum

= 'X'.

wa_fieldcat-qfieldname = 'WUNIT'.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'WUNIT'.

wa_fieldcat-seltext_m

= 'Unit'.

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.

*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = g_repid
it_fieldcat

= it_fieldcat

TABLES
t_outtab

= it_sbook

EXCEPTIONS
program_error

= 1

OTHERS

= 2.

Output

Display Traffic Lights in SAP ALV


Use below steps to disable icons in ABAP ALV toolbar.

1.

Add a column to the internal table to hold the traffic lights value(i.e. 1 for RED, 2 for YELLOW and 3 for
GREEN).

2.

Fetch data from database table and build field catalog.

3.

Assign different traffic lights to each row based on condition.

4.

To display traffic lights in the ALV, fill the lights field name in the LIGHTS_FIELDNAME of ALV layout.

5.

Pass field catalog to function module REUSE_ALV_GRID_DISPLAY.


TYPE-POOLS: slis.

" SLIS contains all the ALV data types

*&---------------------------------------------------------------------*
*& Data Types
*&---------------------------------------------------------------------*
TYPES: BEGIN OF ty_sbook.
INCLUDE STRUCTURE sbook.
TYPES: icon TYPE c,

" Add field to hold traffic light value

END OF ty_sbook.
*&---------------------------------------------------------------------*
*& Data Declaration
*&---------------------------------------------------------------------*
DATA: it_sbook

TYPE TABLE OF ty_sbook.

DATA: wa_sbook

TYPE ty_sbook.

DATA: it_fieldcat

TYPE slis_t_fieldcat_alv,

wa_fieldcat

TYPE slis_fieldcat_alv.

DATA: is_layout

TYPE slis_layout_alv.

DATA: g_repid

TYPE sy-repid.

*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
g_repid = sy-repid.
*Fetch data from the database
SELECT * UP TO 20 ROWS FROM sbook INTO TABLE it_sbook.
*Assign different traffic lights to each row based on condition
LOOP AT it_sbook INTO wa_sbook.
IF wa_sbook-luggweight LE 0.
wa_sbook-icon = 1.

" Red Traffic Light

ELSEIF wa_sbook-luggweight LE 10.


wa_sbook-icon = 2.

" Yellow Traffic Light

ELSE.
wa_sbook-icon = 3.

" Green Traffic Light

ENDIF.
MODIFY it_sbook FROM wa_sbook TRANSPORTING icon.
CLEAR: wa_sbook.
ENDLOOP.
*Build field catalog
wa_fieldcat-fieldname

= 'CARRID'.

" Fieldname in the data table

wa_fieldcat-seltext_m

= 'Airline'.

" Column description in the output

APPEND wa_fieldcat TO it_fieldcat.

CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'CONNID'.

wa_fieldcat-seltext_m

= 'Con. No.'.

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'FLDATE'.

wa_fieldcat-seltext_m

= 'Date'.

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'BOOKID'.

wa_fieldcat-seltext_m

= 'Book. ID'.

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'FORCURAM'.

wa_fieldcat-seltext_m

= 'Price'.

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'FORCURKEY'.

wa_fieldcat-seltext_m

= 'Currency'.

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'LUGGWEIGHT'.

wa_fieldcat-seltext_m

= 'Weight'.

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.
wa_fieldcat-fieldname

= 'WUNIT'.

wa_fieldcat-seltext_m

= 'Unit'.

APPEND wa_fieldcat TO it_fieldcat.


CLEAR wa_fieldcat.
*Fill layout info.
*Fill traffic lights field name in the ALV layout
is_layout-lights_fieldname = 'ICON'.
*Pass data and field catalog to ALV function module to display ALV list
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = g_repid
is_layout

= is_layout

it_fieldcat

= it_fieldcat

TABLES

t_outtab

= it_sbook

EXCEPTIONS

Output

program_error

= 1

OTHERS

= 2.

You might also like