Object oriented approach to ALV Lists in ABAP

33
By Noman Hanif Object Oriented ALV

Transcript of Object oriented approach to ALV Lists in ABAP

Page 1: Object oriented approach to ALV Lists in ABAP

By Noman Hanif

Object Oriented ALV

Page 2: Object oriented approach to ALV Lists in ABAP

Introduction The SAP List Viewer is a generic tool that outputs data in a table form (rows and

columns), with integrated functions to manipulate output (sort, totals, filter,

column order, hide, etc.) and export it (Excel, Crystal report, CSV files, etc.) It is

also possible to make ALV editable via ALV control.

ALV name comes from "ABAP List Viewer", as named initially because it was

only available in ABAP. It is now a more general concept, which is available in

java too (since version 7.1).

ALV tool proposes 3 display types (schemes are taken from the SAP Library -

An Overview of ALV Tools) and in several technologies:

Display types:

o Simple List

Page 3: Object oriented approach to ALV Lists in ABAP

o Hierarchical Sequential List

o Tree

Page 4: Object oriented approach to ALV Lists in ABAP

Technologies

o List

o Control Framework : A control is made available for ALV’s , known as ALV Grid. It can be used in 3 ways:

Function module wrapper. It may be used in Full screen mode, which means that the buttons are displayed in the application toolbar, and also in popup mode (REUSE_ALV_GRID_DISPLAY).

Object oriented wrapper (CL_GUI_ALV_GRID)

Display in full screen using FM: REUSE_ALV_GRID_DISPLAY

Page 5: Object oriented approach to ALV Lists in ABAP

The New Object ModelNew object oriented API

Simplifies programming

Error robustness

o Data-type information about internal table is determined by the ALV

o Wrong parameterization raises exceptions

o Constants for correct parameterization provided

Unified object model for all ALV flavors (Grid, Tree, etc.)

o Single class for handling tabular data

o Unified constants definition

o Common meta data model for all ALV flavors

Page 6: Object oriented approach to ALV Lists in ABAP
Page 7: Object oriented approach to ALV Lists in ABAP
Page 8: Object oriented approach to ALV Lists in ABAP

Main Principle prior to the New Object

Model

Page 9: Object oriented approach to ALV Lists in ABAP

Main Principle of the New Object Model

(Simplest Call)

Page 10: Object oriented approach to ALV Lists in ABAP

In case of object oriented concept the control framework is required as it provides global

classes for various functionalities:

CL_GUI_ALV_GRID

It is the wrapper class implemented to encapsulate ALV grid functionality for list

display. ALV Grid control is a flexible tool which provides following capabilities:

• For building and displaying interactive, non-hierarchical and modern-design lists.

• Provides typical list functions such as sorting, filtering, summing etc.

• Provides common list operations and can be enhanced by user-defined options.

Page 11: Object oriented approach to ALV Lists in ABAP

Basic components required for ALV Grid Control are:

1. List Data: Data to be listed is populated in an internal table. This table can be of

any flat type.

2. Field Catalog: This is an internal table which contains the list of fields as per

specification. It comprises of some additional information about display options for

each column to be displayed.

• It must be referenced to the dictionary type “LVC_T_FCAT” while the work-area

should be of type “LVC_S_FCAT”. Function “LVC_FIELDCATALOG_MERGE” can

also be used to get field catalog by passing structure name.

3. Container: Container is a storage area where the list will be displayed. It should

be of type ”CL_GUI_CUSTOM_CONTAINER”.

Other Container classes are:

• CL_GUI_DOCKING_CONTAINER:

For displaying multiple ALV’s by using methods such as dock_at_left,

dock_at_right, dock_at_top, dock_at_bottom. Internal tables can be displayed

in these containers.

Page 12: Object oriented approach to ALV Lists in ABAP

CL_GUI_EASY_SPLITTER_CONTAINER:

For displaying two ALV Grids on single screen, container is splitted into two

containers by using this class.

4. Layout Structure: It is a structure to specify general layout options for the

grid. With this structure we can set general display options, grid customizing,

totals options, color adjustments etc. The layout structure must be of type

“LVC_S_LAYO”.

4. Event Handler: For handling events, we need to define and implement an

event handler class triggered by the ALV Grid instance. After creating ALV Grid

instance, we must register an instance of this event handler class to handle

ALV Grid events.

Page 13: Object oriented approach to ALV Lists in ABAP

Layout Structure

Page 14: Object oriented approach to ALV Lists in ABAP

Events List

Page 15: Object oriented approach to ALV Lists in ABAP

General Declarations and Steps to Create Object-oriented

ALV:

Create object of class CL_GUI_CUSTOM_CONTAINER for container.

Create object of class CL_GUI_ALV_GRID for putting Grid in above container.

Populate the internal table that you want to display on the Grid.

Page 16: Object oriented approach to ALV Lists in ABAP

General Declarations and Steps to Create Object-oriented

ALV:

Call the screen that contains Custom Container which is done at PBO of screen.

METHOD SET_TABLE_FOR_FIRST_DISPLAY of class CL_GUI_ALV_GRID is

used to display the output

Page 17: Object oriented approach to ALV Lists in ABAP

In SAP Netweaver 2004, SAP introduced a new Object Oriented ALV list family

class, called CL_SALV. It consists of different ALVs such as table, hierarchy,

and tree.

CL_SALV_TABLE in SE24 transaction.

Page 18: Object oriented approach to ALV Lists in ABAP
Page 19: Object oriented approach to ALV Lists in ABAP

Development In this subroutine we use get_flight_schedule, the initialize_alv, and

the display_alv.

Get the list of flight schedule

Page 20: Object oriented approach to ALV Lists in ABAP

In this subroutine, we are going to query the top 100 rows from the database

table,SPFLI and save the received records into an internal table, flight_schedule.

To use the OO CL_SALV_TABLE class, we need to create an instance of it. We can

instantiate it by calling its factory() method

Page 21: Object oriented approach to ALV Lists in ABAP

We are going to define it as reference variable to the class,CL_SALV_TABLE.

Display the ALV List

Page 22: Object oriented approach to ALV Lists in ABAP

The Result

Page 23: Object oriented approach to ALV Lists in ABAP

Enable Layout Settings The subroutine,INITIALIZE_ALV in the following order: first we call

the FACTORY() method of the CL_SALV_TABLE to get an instance of it, then

call the subroutines that responsible for different settings.

Page 24: Object oriented approach to ALV Lists in ABAP

We are going to get and save an instance of the CL_SALV_LAYOUT in the variable,

called LAYOUT_SETTINGS by calling the instance method of the CL_SALV_TABLE

class, called GET_LAYOUT().

We usually set a unique key to make the different ALV layout settings unique between

each other, and set a restriction for saving the layouts.

To set a unique key we use the SET_KEY() method of the CL_SALV_LAYOUT class

that requires a SALV_S_LAYOUT_KEY structure as an import parameter.

Page 25: Object oriented approach to ALV Lists in ABAP

If we check the SET_SAVE_RESTRICTION() method, we find that it requires a

parameter with the type SALV_DE_LAYOUT_RESTRICTION. To provide a value with

this type, we are going to use the IF_SALV_C_LAYOUT interface that contains three

different attributes:

The attributes mean the following:

Page 26: Object oriented approach to ALV Lists in ABAP

The Result

Page 27: Object oriented approach to ALV Lists in ABAP

Optimize Column Width We are going to place the call of our next setting (OPTIMIZE_COLUMN_WIDTH) in

the subroutine, called INITIALIZE_ALV.

Page 28: Object oriented approach to ALV Lists in ABAP

To customize the columns of an ALV, we need an instance of the

class, CL_SALV_COLUMNS_TABLE that we are going to get by calling the

CL_SALV_TABLE method, called GET_COLUMNS(), and save the received instance

reference in a reference variable, named COLUMNS (it refers to the class,

CL_SALV_COLUMNS_TABLE ).

We want to optimize the width of the columns, so that we call the

method, SET_OPTIMIZE(). It has an optional importing bool parameter,

named VALUE that is true by default.

Page 29: Object oriented approach to ALV Lists in ABAP

Before optmizing the columns width.

After optimizing the columns width.

Page 30: Object oriented approach to ALV Lists in ABAP

Individual Column Settings

OPTIMIZE_COLUMN_WIDTH subroutine into the INITIALIZE_ALV subroutine, since

we want to initialize only once, and reuse it several times, so I think it belongs to this

place.

Page 31: Object oriented approach to ALV Lists in ABAP

In the OPTIMIZE_COLUMN_WIDTH subroutine

Get the reference of the MANDT column by calling the method, GET_COLUMN() on

the columns object (columns contains all of the columns from the table, SPFLI), and

then let's call its method, named SET_VISIBLE() with false. At last, let's wrap these

method calls into a TRY-CATCH block to handle exceptions (now we have to handle

the case when doesn't exist any field in the structure with the name, MANDT).

Page 32: Object oriented approach to ALV Lists in ABAP

Place its declaration in the global area for the sake of the simplicity with the

type CL_SALV_COLUMN.

Set text for column

Page 33: Object oriented approach to ALV Lists in ABAP

THANK YOU!!