[SAP ABAP] ALV Part 1

ALV

In this section, I will present an example of how to create ALVs in two ways. In the same example, I will show you the possibilities of ALVs, also using the selection screen created before.
ALV is an integrated element of the ABAP objects environment. This makes it possible to implement the display of structured datasets. We can display simple and two-dimensional tables, hierarchical-sequential lists, and tree structures.
The possibilities of these two methods are similar, so I will present all examples based on the CL_GUI_ALV_GRIDclass and then I will present code where I created a basic ALV by using SALV classes.

Basics

If we want to create an ALV first, we need to declare a local variable of type references to the cl_gui_alv_grid class, a variable of type references to the cl_gui_custom_container class, and a local constant with the name of the container and the table where we store data that will be shown on screen:
DATA: lt_spfli      TYPE TABLE OF spfli,
      lr_alv        TYPE REF TO cl_gui_alv_grid,
      lr_ccontainer TYPE REF TO cl_gui_custome_container.

CONSTANTS: lc_container_name TYPE scrfname VALUE 'ALV'.
Then, we need to create a screen and two modules—one for the PBO and one for the PAI. In this example, we use screen 100.
In the Flow Logic tab, create the PBO and PAI modules as follows:
Flow logic on screen
It is really important to create a module right after PROCESS BEFORE OUTPUT in the case of the PBO module, and after PROCESS AFTER INPUT for the PAI. If this module is created in a different way, for example, with the PBO module after PROCESS AFTER INPUT, this module will be considered a PAI module and it will not work properly.
After creating a screen, we get two new modules:
*&---------------------------------------------------------------------*
 *&      Module  PBO_100  OUTPUT
 *&---------------------------------------------------------------------*
 *       text
 *----------------------------------------------------------------------*
 MODULE pbo_100 OUTPUT.
 * SET PF-STATUS 'xxxxxxxx'
 * SET TITLEBAR ' xxx'. 
 ENDMODULE.                 " PBO_100  OUTPUT
 *&---------------------------------------------------------------------*
 *&      Module  PAI_100  INPUT
 *&---------------------------------------------------------------------*
 *       text
 *----------------------------------------------------------------------*
 MODULE pai_100 INPUT.

 ENDMODULE.                 " PAI_100  INPUT
In pbo_100, we see the two commented rows. In the first of them is PF_STATUS and in the second is TITLEBAR.
In the TITLEBAR, we can establish the title being shown on the screen.
Using ON PF-Status, we can embed an action to an icon on the screen. If we do not do this, any icon (including back or cancel) will not work. Here is an example of how to create PF-STATUS and the TITLEBAR:
MODULE pbo_100 OUTPUT.
  SET PF-STATUS 'STATUS_100'.
  SET TITLEBAR 'TITLEBAR_100'. 
 ENDMODULE.
PF-STATUS and TITLEBAR can have any name. In our example, we have 'STATUS_100' as PF-STATUS and 'TITILEBAR_100' as the titlebar.
Right after clicking on the title, a window is shown. In the title input field, we can insert text, which will be shown on the screen:
Creating a title
After clicking on OK, a title will be created. Next, click on PF-STATUS and a popup will appear:
Creating a status
When this pops up, input some short text and choose a type of status. We mostly use Normal Screen here. Dialog Box is a status type for screens without a menu bar and Context Menu is a status used for the context menu only.
After creating PF_STATUS, we will see a screen where we can define buttons in the menu bar, application toolbar, and function keys.
In the beginning, we should define at least three buttons (BACKCANCEL, and EXIT). If we do not do this, we cannot move back after creating the screen and we can only create a new session and turn off the present one. We can also create more than these three buttons. In order to create these actions on screen, we can fill the input field as an example and then click on Activate:
Screen status option 
Next, in the PAI module action, we should determine what this button needs to do and code what will be executed.
To do this, create a global field named, for example, OK_CODE, to keep the name of action assigned to the button. Then, we need to assign this field to a variable on screen in the element list:
Element list on the screen
After this, we need to insert the code to the PAI in order to handle these actions:
MODULE pai_100 INPUT.

   CASE ok_code.
     WHEN 'BACK'.
       LEAVE TO SCREEN 0.
     WHEN 'CANCEL'.
       LEAVE TO SCREEN 0.
       LEAVE PROGRAM.
     WHEN 'EXIT'.
       LEAVE PROGRAM.
   ENDCASE.

 ENDMODULE.  
Every action on the PAI can be handled in this way. Just add a new case and code to execute it.
After creating basic things on the screen, we can create a container for our ALV. To do this, enter Screen Painter and click on Layout to enter the Screen Painter|Layout editor:
Screen painter
Your screen should look the same as the preceding screenshot.
To add the element to the screen, we can just click on an interesting element in the left-hand list and drag and drop it onto the screen. In our case, we need theCustom controlelement, so search forCustom controland drag and drop this onto the screen. After doing this, you should see an element with crossed lines, like this:
Custom control
This is the Custom control. To move objects on screen, we can drag and drop them. If we want to change the size of elements, we can double-click on the element and change the variables named Height and Vis.Length. Alternatively, we can simply enlarge or decrease elements on the screen:
 Screen painter—element option
The next field we must fill in is Name (for all of the elements on the screen). Custom control has been named ALV. We need to remember this name because we need to use it in the code.
As we have created a Custom control box in the screen, we can create an ALV and display it in the Custom control box.
As the first step, we need to create the lr_ccontainer object with the container name and lr_alv, which is defined in our global variable:
CREATE OBJECT lr_ccontainer
   EXPORTING
     container_name = lc_container_name.

 CREATE OBJECT lr_alv
   EXPORTING
     i_parent = lr_ccontainer.
The name of the container needs to be declared in Capital Letters, otherwise, it will not work, as all of the letters will be changed to capital letters.
To create a field catalog, we can use two methods—we can use the Data Dictionary (DDIC) structure or create a custom field catalog. In the first example of an ALV, we can use this version.
If we want to create a custom field, add them to the field catalog, which is a table where we store all names of the field and we can define the parameter of the ALV in many ways, including by the following:
  • Displayed name
  • Size of the column—width counted in chars
  • Conversion exit
  • Number of displayed decimals
To create a field catalog, we need to use a variable table of LVC_T_FCATtype and a structure of LVC_S_FCAT type.
An example of one field in a custom field catalog is as follows:
ls_fcat-fieldname = 'CARRID'.
ls_fcat-scrtext_s = 'Airline Code'.
ls_fcat-outputlen = 4.
APPEND ls_fcat TO lt_fcat.
CLEAR ls_fcat.
To display the field in the ALV grid, we need to add it to the field catalog first. In our preceding example, we add a field with the name CARRID (fieldname). In the ALV, we see the short text 'Airline code' (scrtext). We also add a parameter specifying the length of the field (outputlen).  
To display the table on the screen, use the set_table_for_first_display method. In the following code, we have the most basic version of the code to see the ALV:
CALL METHOD lr_alv->set_table_for_first_display
   EXPORTING
     i_structure_name = 'SPFLI'
   CHANGING
     it_outtab        = lt_spfli.
The use of our custom field catalog is depicted in the following example:
CALL METHOD lr_alv->set_table_for_first_display
   CHANGING
     it_outtab        = lt_spfli
     it_fieldcatalog  = lt_fcat.
Now, we can see the differences between those two ALVs.
Here, we can see an example of the ALV created with the field catalog created by the DDIC:
Results of the DDIC field catalog
An example of a field catalog is in the following screenshot:
Results of a custom field catalog
As we can see, the example of the DDIC is prettier. the ALV automatically transports the names of fields, adjust the length of the fields, and highlight key columns. However, if we create a field catalog on our own, we would have many more possibilities. For example, if we create a field catalog on our own, we can change the order of the field.
Every time we make changes on screen or perform a PAI action, PBO is automatically called right before the output screen. But as we create a container, ALV instance, and field catalog and initialize ALV, those methods need some time to complete. If we do not need to create this again, for example, when we only need to change displayed data, we should use the refresh_table_display method. Consequently, we can check whether the container was created. If so, we can only use the method mentioned previously.

Advanced capabilities of ALV sand screens

ALVs and screens also have many more advanced possibilities. We will look at the most commonly used in this section, as follows:
  • Zebra
  • Coloring
  • Events on the ALV in an example of a button click
  • ALV icons 
Following are the advanced possibilities of screens:
  • Text field and translations
  • Input/output field
  • Radiobuttons
  • Buttons
  • Addition of dynamic display possibilities for individual elements and groups, and examples of using all of the discussed possibilities

Comments

Popular posts from this blog

[SAP ABAP] Data declaration

[SAP ABAP] BAPI

[SAP HR] Payroll Schema