Sie sind auf Seite 1von 67

AB1004 - Advanced ALV using OO v1.

India SAP CoE, Slide 1

AB1004 - Advanced ALV using OO v1.0


1 2

Introduction Syntax Description


3

Demonstration
Exercises HelpMe

5
India SAP CoE, Slide 2

AB1004 - Advanced ALV using OO v1.0


1 2

Introduction Syntax Description


3

Demonstration
Exercises HelpMe

5
India SAP CoE, Slide 3

Introduction
ALV: Object Oriented way CL_GUI_ALV_GRID
Purpose
Use

Challenges

India SAP CoE, Slide 4

Introduction
Purpose The ALV Grid control is a flexible tool for displaying lists & provides common list operations as generic functions and can be enhanced by selfdefined options. Purpose of this Tutorial is to demonstrate how to implement ALV using Classes CL_GUI_ALV_GRID and CL_GUI_CUSTOM_CONTAINER

India SAP CoE, Slide 5

Introduction
Use used to build non-hierarchical, interactive, and modern-design lists. provides typical list functions as sorting, filtering, summing, while also gives the opportunity to develop user functions where needed. It presents numerous interfaces like Excel Inplace and Crystal Reports.

India SAP CoE, Slide 6

Introduction
Challenges To use ALV Grid Control in a simple manner, sufficient just having experience on dialog programming. However, to make use of more capabilities, it is required some knowledge on object-oriented perspective of ABAP programming & control framework

India SAP CoE, Slide 7

AB1004 - Advanced ALV using OO v1.0


1 2

Introduction Syntax description


3

Demonstration
Exercises HelpMe

5
India SAP CoE, Slide 8

SAP Control Framework

India SAP CoE, Slide 9

Custom Controls
Custom control is: an area on a screen used to embed controls are software components of the presentation server control is driven by the application logic, which still runs on the application server have a wrapper class in ABAP Objects

India SAP CoE, Slide 10

Control Framework

The controls on the presentation server and the ABAP application programs on the application server communicate using the SAP Control Framework
India SAP CoE, Slide 11

Container Controls

Before you can work with a custom control on a screen, you must assign a Container Control to it. Container controls are instances of special global classes (global class for custom controls is called CL_GUI_CUSTOM_CONTAINER). To link a custom control to a container control, pass the custom control name to the constructor of the container control when you instantiate it using CREATE OBJECT.
India SAP CoE, Slide 12

ALV Grid Control Components


Wrapper class implemented to encapsulate ALV Grid functionality is CL_GUI_ALV_GRID Building Blocks of ALV Grid Control: List data: data in an internal table to be listed. Field Catalog: specify how fields of list will be displayed (type LVC_T_FCAT) Layout Structure: set general display options, grid customizing, totals options, color adjustments (type LVC_S_LAYO) Event Handler: define and implement an event handler class if we want to handle events triggered by the ALV Grid instance.

India SAP CoE, Slide 13

Grid Control Inheritance Hierarchy

CL_GUI_ALV_GRID class encapsulates communication

with the instance on the presentation server, along with many other functions. For this reason, you should instantiate this class, not its super class.
India SAP CoE, Slide 14

Wrapper Class for ALV Grid

CL_GUI_ALV_GRID class provides various methods. Go to SE24 and explore the various methods provided by this class.
India SAP CoE, Slide 15

Wrapper Class for ALV Grid

CL_GUI_ALV_GRID class provides various events. All the supported events can be viewed under Events Tab

India SAP CoE, Slide 16

Steps for ALV Grid


Step 1 Add a custom control on the screen which will be related to the custom container. Lets give it the name CC_ALV. Step 2 Declare global variables to be used for ALV Grid. Step 3 Declare your internal table which is supposed to hold the list data. Step 4 Flow Logic for the Screen which comprises the ALV Grid control. Step 5 Create Custom Container instance, ALV Grid instance Step 6 Populate the list data to be displayed & prepare field catalog, Layout Step 7 Call the appropriate method SET_TABLE_FOR_FIRST_DISPLAY / REFRESH_TABLE_DISPLAY
India SAP CoE, Slide 17

AB1004 - Advanced ALV using OO v1.0


1 2

Introduction Syntax Description


3

Demonstration
Exercises HelpMe

5
India SAP CoE, Slide 18

Step 1 Create Custom Control

Create a screen. Go to Screen Layout & Add a custom control on the screen. Give it a name say CC_ALV

India SAP CoE, Slide 19

Step 2 & 3 - Global data definition

1. Create instance reference for creating instances of class CL_GUI_ALV_GRID and Custom Container CL_GUI_CUSTOM_CONTAINER. 2. Field Catalog Internal Table will of type LVC_T_FCAT 3. Layout will be of type LVC_S_LAYO 4. Internal Table to hold List Data that is to be displayed as ALV
India SAP CoE, Slide 20

Step 4 Screen Flow Logic

We will now add a PBO Module Display_alv. This routine will have the code for: Instantiating the Custom Control & ALV Grid Populating the Field Catalog & Layout Calling the appropriate method of class CL_GUI_ALV_GRID to display the ALV Grid output
India SAP CoE, Slide 21

Step 5 Creating Custom Container Instance

Instantiate the Container. Pass the name of the Custom Control we defined in the Screen 9000 above

As we are using the concept of Classes, we will now create instance of the Custom Container. Use the Pattern > ABAP Objects > Create Object > Specify the Instance name as the one we specified in the Data Definition Step.
India SAP CoE, Slide 22

Step 5 Creating ALV Grid Instance

Instantiate the ALV Grid. Pass the Container (instance of which we created in the previous slide)

We will now create instance of the ALV Grid. Again use the Pattern > ABAP Objects > Create Object > Specify the Instance name as the one we specified in the Definition Step (type ref to CL_GUI_ALV_GRID) Class Name: CL_GUI_ALV_GRID
India SAP CoE, Slide 23

Step 6 - Building Field Catalog


We will now prepare the Field Catalog. There are 3 methods for doing this: Automatic generation Semi-automatic generation Manual generation Please refer to the attached document for all the options provided by Field Catalog

India SAP CoE, Slide 24

Step 6 - Building Field Catalog


Structure of Field Catalog (LVC_T_FCAT)
FIELDNAME
REF_FIELD

Assign a field name of your output table to a row of field catalog


Must specify this if field name in the output table is not identical to the field name of the field in Data Dictionary Must fill this field only if the output table described by the current entry in the field catalog has a corresponding entry in the DDIC Sequence of the fields Desired width of the field in output Field Labels

REF_TABLE

COL_POS OUTPUTLEN SCRTEXT_L/M/S

India SAP CoE, Slide 25

Building Field Catalog Manually

The work in this procedure is just filling the internal table for the field catalog. We have already seen the structure of a field catalog. To achieve filling the field catalog correctly, one must at least fill the above fields of the field catalog structure for each column of the list.

India SAP CoE, Slide 26

Building Field Catalog Manually


DATA ls_fcat type lvc_s_fcat . ls_fcat-fieldname = 'CARRID. ls_fcat-inttype = 'C'. ls_fcat-outputlen = '3'. ls_fcat-coltext = 'Carrier ID'. ls_fcat-seltext = 'Carrier ID'. APPEND ls_fcat to pt_fieldcat. CLEAR ls_fcat. ls_fcat-fieldname = 'CONNID'. ls_fcat-ref_table = 'SFLIGHT'. ls_fcat-ref_table = 'CONNID'. ls_fcat-outputlen = '3'. ls_fcat-coltext = 'Connection ID'. ls_fcat-seltext = 'Connection ID'. APPEND ls_fcat to pt_fieldcat . . And so on for all the fields to be displayed in the ALV output
India SAP CoE, Slide 27

Building Field Catalog Semi-automatically


It is a boring work to fill and append rows for all columns of our list. And it is not flexible to proceed with automatically generating of the field catalog. Fortunately, there is a middle ground as generating the field catalog semi-automatically. This procedure requires a function module to call. We pass the name of the structure to be the template and the function module generates a field catalog for us. After getting the generated field catalog, we loop at it and change whatever we want. The name of the function module is LVC_FIELDCATALOG_MERGE. We will now look at how to use this FM in the next slide

India SAP CoE, Slide 28

Building Field Catalog Semi-automatically


DATA ls_fcat type lvc_s_fcat . CALL FUNCTION 'LVC_FIELDCATALOG_MERGE' EXPORTING i_structure_name = 'SFLIGHT' CHANGING ct_fieldcat = pt_fieldcat[] EXCEPTIONS inconsistent_interface = 1 program_error = 2 OTHERS = 3. IF sy-subrc <> 0. *--Exception handling ENDIF. LOOP AT pt_fieldcat INTO ls_fcat . CASE pt_fieldcat-fieldname . WHEN 'CARRID' . ls_fcat-outpulen = '10'. ls_fcat-coltext = 'Airline Carrier ID'. MODIFY pt_fieldcat FROM ls_fcat. WHEN 'PAYMENTSUM'. ls_fcat-no_out = 'X' . MODIFY pt_fieldcat FROM ls_fcat. ENDCASE. ENDLOOP.

India SAP CoE, Slide 29

Step 6 Layout Adjustments


It comes now painting our ALV Grid in a general aspect. To define general appearance of our ALV Grid we fill a structure of type LVC_S_LAYO. This table contains fields and functionalities serviced by this adjustment. Some of the generally used options are as below:
ZEBRA SMALLTITLE If this field is set, the list shows a striped pattern in the print preview and when it is printed (SPACE, 'X) If this field is set, the title size in the grid control is set to the font size of the column header. (SPACE, 'X)

India SAP CoE, Slide 30

Step 7 Call the method for ALV display

Pass the Layout, Field Catalog & List Data

Data transfer to the ALV control takes place during the call of method SET_TABLE_FOR_FIRST_DISPLAY of class CL_GUI_ALV_GRID. The method call must be programmed at the PBO event of the screen with the SAP Grid Control container. Remember to use Pattern > ABAP Objects > Method of a Class
India SAP CoE, Slide 31

Step 7 Call the method for ALV display

If the ALV_GRID is initial (First Call) the method SET_TABLE_FOR_FIRST_DISPLAY is called as described in the previous slide. Else on subsequent calls; REFRESH_TABLE_DISPLAY is called. Reason being; there is no need to instantiate the Custom Container, Grid every time in the PBO of the Screen. The parameters of this method: IS_STABLE: If the row or column field of this structure is set, the position of the scroll bar for the rows or columns remains stable. I_SOFT_REFRESH: If set, any totals created, any sort order defined and any filters set for the data displayed remain unchanged when the grid control is refreshed.
India SAP CoE, Slide 32

Execute the Program

Execute the Program to test the output. The ALV Grid output is displayed as expected!!

India SAP CoE, Slide 33

Tweaking the above ALV output..


Non-Event Based Additional Functionalities Additional Functionalities that the ALV Grid can handle: Setting Sort Conditions Filtering Coloring row, column or cell Inserting Hyperlinks We will look at Sorting & Filtering (commonly used functionalities)

India SAP CoE, Slide 34

Non-Event Based Additional Functionalities


Setting Sort Conditions:
It is possible to set sort conditions for the table data. This is achieved by filling an internal table of structure LVC_T_SORT which consists of the sort criteria. To have an initial sorting, pass it to the parameter IT_SORT of the method SET_TABLE_FOR_FIRST_DISPLAY.

India SAP CoE, Slide 35

Non-Event Based Additional Functionalities


Setting Filter Conditions: The procedure is like the one in sorting. Here, the type of the table you must fill is LVC_T_FILT. Filling this table is similar to filling a RANGES variable.

India SAP CoE, Slide 36

Test the ALV Output

India SAP CoE, Slide 37

Event Based Additional Functionalities

Go to SE24 and check the Events available for Class CL_GUI_ALV_GRID

As being developed by object-oriented methodology, ALV Grid control has some events that are triggered during interaction between the user. These events are used to utilize some additional functionalities of the ALV Grid. For these types of functionalities, we require a class to be implemented (generally local in our program) to be the event handler for the ALV Grid instance. It is assumed in this tutorial that the objectoriented perspective of ABAP programming is known.
India SAP CoE, Slide 38

Local Event handler class

In our Example (SE80) Add a new Class Definition

We will implement the TOP_OF_PAGE events in the following slides

India SAP CoE, Slide 39

Event Handler Class (Local) Definition

We will implement the TOP_OF_PAGE events in the following slides

India SAP CoE, Slide 40

Event Handler Class (Local) Implementation

SE80 > Right Click and Create Class Implementation. Give the same name as the Class Definition created above.

India SAP CoE, Slide 41

Event Handler Class (Local) Implementation

Having an event handler class we are now able to instantiate it and register its methods to handle ALV Grid instance events.

India SAP CoE, Slide 42

Register Event Handlers


Add the Event Handler Instance reference in the TOP Include

Create an instance & Register the event handlers. Add this in the routine after the code instantiating the Custom Container & ALV Grid. (before the method call SET_TABLE_FOR_FIRST_DISPLAY
India SAP CoE, Slide 43

Coding the handler routines.

We will now put the handler code (what we want to do when the event is raised?) inside the appropriate event handler method.

India SAP CoE, Slide 44

Adding new buttons (functions)

Use the event toolbar to add the button and the event user_command to implement the new function. In the method handling the toolbar event, we define a new button by filling a structure and appending it to the table attribute mt_toolbar of the object to whose reference we can reach via the parameter e_object of the event.
India SAP CoE, Slide 45

Adding new buttons (functions)

India SAP CoE, Slide 46

Adding menu buttons

India SAP CoE, Slide 47

Run the Program..


Buttons added to the toolbar..

Menu entries added to the toolbar..

India SAP CoE, Slide 48

Handling user command


Implementing functioning codes for new functions
HANDLE_USER_COMMAND

Add a simple Message (I / S) to test the same.

Call this Method after creating the ALV Grid instance.


India SAP CoE, Slide 49

Display ALV Heading


Step 1 Add the Method handle_print_top_of_list in the Local Class Definition we created above Step 2 Implement this method in the implementation part of the local class we created above Step 2 Register this method as the handler for event print_top_of_list Similar steps for other events shown below.

India SAP CoE, Slide 50

Display ALV Heading

India SAP CoE, Slide 51

Test the Output

India SAP CoE, Slide 52

Display ALV Logo


In the previous slide the events used were for List Output, hence they were visible on the list output only To display Logo, Header etc on the ALV Grid Output, use the following steps:
TOP_OF_PAGE event uses the object of class CL_DD_DOCUMENT In this class there are methods ADD_TEXT, ADD_PICTURE, and ADD_GAP which are useful to show the content in the TOP_OF_PAGE

Step 1 Split the screen into two parts using the splitter container and then use the first part to TOP_OF_PAGE and the second one to show the Grid data. Create the Top document object Create Splitter for Custom Container Assigning Part 1 for TOP_OF_PAGE Assigning the Part 2 to ALV GRID (using method GET_CONTAINER) Step 2 Have a local class inside the report to handle the TOP_OF_PAGE event Step 3 Use of methods ADD_TEXT, ADD_PICTURE, ADD_GAP.

India SAP CoE, Slide 53

Display ALV Logo


Container(s) instance reference & Event Handler
Create Container instance reference for Splitter, ALV Top & ALV Grid

Add the Method to handle event TOP_OF_PAGE in Local Class Definition Implement Method as event handler for TOP_OF_PAGE in Local Class Implementation

India SAP CoE, Slide 54

Display ALV Logo


Event Handler Routine..

Use various methods provided by the Class CL_DD_DOCUMENT. Here, we have added Text & Logo

India SAP CoE, Slide 55

Display ALV Logo


CL_DD_DOCUMENT Class & its Methods.

Go to SE24 & see the methods provided by the class CL_DD_DOCUMENT.

India SAP CoE, Slide 56

Display ALV Logo


Usage of HTML Controlfor displaying content in Top Container of Splitter
Adding a blank line

Display the document. Pass the Top as Parent parameter. GR_PARENT_TOP is Top Container where we want to display the Logo & Heading

India SAP CoE, Slide 57

Display ALV Logo


Create instance of CL_DD_DOCUMENT, Splitter

Create instance for Splitter. GR_SPLITTER TYPE REF TO CL_GUI_SPLITTER_CONTAINER

India SAP CoE, Slide 58

Display ALV Logo


Split the Screen into 2 parts: Part 1 for the Logo & Heading and Part 2 for ALV Display

Assign the Areas of the Screen to display Logo & Heading (Top) and ALV Display (Parent Container). Note the Row & Column Parameters passed to the Method

India SAP CoE, Slide 59

Display ALV Logo


Create the ALV Grid (refer to CL_GUI_AV_GRID) instance for ALV Grid Display.

Create the ALV Grid instance (TYPE REF TO CL_GUI_ALV_GRID) and remember to pass the Parent Container instance (GR_PARENT_GRID)

Register the Event Handler for TOP_OF_PAGE


India SAP CoE, Slide 60

AB1004 - Advanced ALV using OO v1.0


1 2

Introduction Syntax Description


3

Demonstration
Exercises HelpMe

5
India SAP CoE, Slide 61

Exercises
1. Add custom buttons to the above ALV & Implement the logic for the same in User Command Handler Routine Try out the various Layout Options Create a Report Variant (of type disvariant) Develop an ALV Report using Object-oriented methods discussed in the previous slides.
Refer the attached technical specification document

2. 3. 4.

India SAP CoE, Slide 62

AB1004 - Advanced ALV using OO v1.0


1 2

Introduction Syntax Description


3

Demonstration
Exercises HelpMe

5
India SAP CoE, Slide 63

HelpMe

Tips and Tricks Additional Info

India SAP CoE, Slide 64

HelpMe
Additional Info:
NEW ALV OBJECT MODEL: Usage of Service Class CL_SALV_TABLE for ALV Display. Refer to SAP Knowledge Warehouse (help.sap.com) for more information. http://help.sap.com/saphelp_nw04/helpdata/en/f9/1ab54099de3726e1000 0000a1550b0/frameset.htm The new object model of the SAP List Viewer (ALV) is an object-oriented encapsulation of the ALV tool that already exists. Simple, two-dimensional table Hierarchical-sequential list Tree structure
Create an Instance reference to Service Class Cl_SALV_TABLE

India SAP CoE, Slide 65

HelpMe
Additional Info: (NEW ALV OBJECT
SE24 Check the Methods supported MODEL)by the Class Cl_SALV_TABLE

India SAP CoE, Slide 66

HelpMe
Additional Info: (NEW ALV OBJECT MODEL)

SE80 > Check the SALV Package and the Various Classes it includes

Check the Demo Programs provided.

India SAP CoE, Slide 67

Das könnte Ihnen auch gefallen