12
SAP ALV programming techniques Hello Friends!!! In this series of posts,I would be covering the most basic and fundamental aspects of programming SAP ALV grids &lists. This series could be used by novices to start programming with ALV as well as experienced ones to brush up their knowledge. So,without wasting any time,lets start…. SAP ALV or SAP List Viewer is a UI element which is used to work with tabular data in form of lists. In addition to the viewing tabular data,SAP ALV can also be used to create and edit tabular data also. It offers many convenient spreadsheet functionalities relevant to tabular data e.g. exporting to various formats such as spreadsheets,XML etc,sorting the data,grouping the data etc. SAP ALV Most of you would already know much more about the SAP ALV functionalities,so instead of spending any more time on introduction of SAP ALV,let‟s dive into its programming aspects. SAP provides two different ways to incorporate SAP ALV into your reports or programs which need to deal with tabular data. They are mentioned below: Using REUSE_ALV_* function modules SAP offers some easy to use function modules using which different kinds of ALV grid can be displayed from your program. These function modules provide a very simplified programming approach to use SAP ALV to display and work with tabular data. All such function modules can be found under the Function group SALV &SLVC_FULLSCREEN in the package (development class) SLIS. These function modules provide an easy &simple to program way of using SAP ALV,but on the other hand compromise with some advanced functionalities and gives lesser flexibility to the programmer. Still,this is the easiest way to program SAP ALV for simple reports and applications where the main purpose of report/ program is to display a tabular result. There are four different forms of SAP ALV exposed through reuse function modules in order to cater different kinds of data display requirements. (A) Simple ALV list This form of ALV is used to display single table display. All the function modules in the above mentioned function group SALV with prefix REUSE_ALV_LIST_ belong to this form of ALV e.g. REUSE_ALV_LIST_DISPLAY,REUSE_ALV_LIST_LAYOUT_INFO_SET,REUSE_ALV_LIST_SELECTIONS_ SET etc. This form of ALV outputs a single list on one complete screen. There‟s a very limited possibility to control the layout of this output screen.

SAP ALV Programming Techniques

  • Upload
    bas101

  • View
    636

  • Download
    5

Embed Size (px)

Citation preview

Page 1: SAP ALV Programming Techniques

SAP ALV programming techniques Hello Friends!!! In this series of posts,I would be covering the most basic and fundamental aspects of programming SAP ALV grids &lists. This series could be used by novices to start programming with ALV as well as experienced ones to brush up their knowledge. So,without wasting any time,lets

start….

SAP ALV or SAP List Viewer is a UI element which is used to work with tabular data in form of lists.

In addition to the viewing tabular data,SAP ALV can also be used to create and edit tabular data also. It offers many convenient spreadsheet functionalities relevant to tabular data e.g. exporting to

various formats such as spreadsheets,XML etc,sorting the data,grouping the data etc.

SAP ALV

Most of you would already know much more about the SAP ALV functionalities,so instead of

spending any more time on introduction of SAP ALV,let‟s dive into its programming aspects.

SAP provides two different ways to incorporate SAP ALV into your reports or programs which need to deal with tabular data. They are mentioned below:

Using REUSE_ALV_* function modules

SAP offers some easy to use function modules using which different kinds of ALV grid can be

displayed from your program. These function modules provide a very simplified programming approach to use SAP ALV to display and work with tabular data. All such function modules can be

found under the Function group SALV &SLVC_FULLSCREEN in the package (development class) SLIS.

These function modules provide an easy &simple to program way of using SAP ALV,but on the other hand compromise with some advanced functionalities and gives lesser flexibility to the programmer.

Still,this is the easiest way to program SAP ALV for simple reports and applications where the main purpose of report/ program is to display a tabular result.

There are four different forms of SAP ALV exposed through reuse function modules in order to cater

different kinds of data display requirements.

(A) Simple ALV list

This form of ALV is used to display single table display. All the function modules in the above

mentioned function group SALV with prefix REUSE_ALV_LIST_ belong to this form of ALV e.g. REUSE_ALV_LIST_DISPLAY,REUSE_ALV_LIST_LAYOUT_INFO_SET,REUSE_ALV_LIST_SELECTIONS_

SET etc. This form of ALV outputs a single list on one complete screen. There‟s a very limited possibility to control the layout of this output screen.

Page 2: SAP ALV Programming Techniques

Simple SAP ALV List

(B) Hierarchical sequential ALV list

This form of ALV lists is used to display 2 tables of data which form a 2-step hierarchy. The main function module to display this kind of ALV is REUSE_ALV_HIERSEQ_LIST_DISPLAY in function group

SALV. There are some more function modules in function group SALV with prefix REUSE_ALV_HS_ belonging to this form of ALV. Typical to REUSE_ALV_* method of using ALV,it has a very limited

possibility to control the layout of this output screen.

Hierarchical Sequential ALV List

(C) Block ALV List

This form of ALV lists is used to display multiple independent tables. All the function modules in above mentioned function group SALV having prefix REUSE_ALV_BLOCK_ belong to this form of ALV.

Block ALV list is used to display multiple ALV lists on the same output screen one after another.

Block ALV List

(D) ALV Grid

This is the new generation of ALV replacing classical list formats mentioned above with grids. It‟s used for the purpose similar to simple lists,but overtakes simple lists in terms of better usability,more

Page 3: SAP ALV Programming Techniques

in-built functionalities and look &feel. All the function modules related to this form of ALV can be

found under function group SLVC_FULLSCREEN of package SLIS.

ALV Grid

Using ABAP Class CL_GUI_ALV_GRID

This method of programming ALV grid runs on Control framework &is based on ABAP Objects. It

gives 100% control of programming ALV to the developers. You can use this method to work with ALV similar to other screen elements and also freely add other screen elements on the same screen

along with ALV. The main ABAP Class involved in this method is CL_GUI_ALV_GRID.

Multiple ALV Grids &Tree on same screen using CL_GUI_ALV_GRID method

Field Catalog

In my previous post ( SAP ALV Tutorial:SAP ALV programming techniques),I tried to give a brief

overview about what SAP ALV (SAP List Viewer) is and what are the different ways to use this UI tool of SAP in ABAP programs. In this post,we will dive into the details of the first and simpler

method of programming ALV –REUSE_ALV_ function modules.

In this post,we will begin with the most important programming aspect of an ALV termed Field

Catalog. The field catalog is a common input required by all ALV whether you use REUSE_ALV_ methods or ABAP Class CL_GUI_ALV_GRID method. Neverthless,the data type of field catalog for

both the methods are different,but they serve the same purpose.

So what’s an ALV field catalog? In order to display some tabular data in an ALV,the ALV needs some information about the columns/

fields which will be displayed in the ALV. Field catalog is basically the internal table which carries information about each of the columns/ fields with which ALV has to deal with. It need not

necessarily contain information about each of the columns/ fields in the data table used as data source of ALV. Only the fields which are important for display or of some technical use for ALV need

to be informed to ALV through field catalog.

Page 4: SAP ALV Programming Techniques

Ok!! I understood the purpose of field catalog. Tell me its technical details from REUSE_ALV_

method’s perspective!!! Field catalog for REUSE_ALV_* function modules is a normal internal table of type

SLIS_T_FIELDCAT_ALV from type group SLIS. So,in your program,you will have to include type group SLIS before you declare an internal table of type SLIS_T_FIELDCAT_ALV.

Report my_first_alv. TYPE-POOLS:SLIS.

DATA:lt_field_catalog TYPE SLIS_T_FIELDCAT_ALV.

In this field catalog internal table,you have individual records for each of the columns/ fields to be

displayed in ALV.

The only mandatory field in every record of field catalog is FIELDNAME. Your program using ALV

will dump if this field is found empty in the field catalog.

Rest all fields are needed to control different aspects of a particular field but not mandatory. Your program would continue to work even if not completely as per your expectation.

I am mentioning below a list of most frequently used fields in field catalog structure along with their

purpose.

KEY:This field indicates if the respective column is the key field in the tabular data to be displayed. The columns marked as „Key‟fields will always be positioned before the non-key fields. Example..

COL_POS:Specifies the position of column in the ALV display. If this property is not specified,the

columns/fields of data table will appear in ALV display in the order in which their “fieldname”appear

in the field catalog. “Key”fields will always have higher priority than non-key fields. You are free to use the same position number for key fields &non-key fields. However,if two key fields or non-key

fields have been assigned the same position number,they will be displayed in the order in their “fieldname”appear in the field catalog.

Rating:8.3/10 (12 votes cast)

In this post,you will find the detailed exaplanation of properties (fields) KEY &COL_POS of field

catalog (SLIS_T_FIELDCAT_ALV) with example.

As explained in my previous post (Introduction to Field Catalog ( SLIS_T_FIELDCAT_ALV )):

KEY:This field indicates if the respective column is the key field in the tabular data to be displayed.

The columns marked as „Key‟fields will always be positioned before the non-key fields.

COL_POS:Specifies the position of column in the ALV display. If this property is not specified,the columns/fields of data table will appear in ALV display in the order in which their “fieldname”appear

in the field catalog. “Key”fields will always have higher priority than non-key fields. You are free to use the same position number for key fields &non-key fields. However,if two key fields or non-key

fields have been assigned the same position number,they will be displayed in the order in their

“fieldname”appear in the field catalog.

Here,you would be able to experience the real effect of these properties.

Lets,begin with an ALV list where only the mandatory field FIELD_NAME is provided for each of the columns in field catalog. So,the code for field catalog looks as below:

FORM e01_fieldcat_init USING e01_lt_fieldcat TYPE slis_t_fieldcat_alv.

DATA:ls_fieldcat TYPE slis_fieldcat_alv.

CLEAR ls_fieldcat.

Page 5: SAP ALV Programming Techniques

ls_fieldcat-fieldname = „PRICE‟.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CURRENCY‟.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CARRID‟.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CONNID‟.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „FLDATE‟.

APPEND ls_fieldcat TO e01_lt_fieldcat.

ENDFORM.

The above ALV field catalog produces the output as shown below:

ALV with only FIELDNAME specified in Field catalog

Now,lets introduce the property KEY for some of the columns to be displayed on ALV. Find below the modified code:

FORM e01_fieldcat_init USING e01_lt_fieldcat TYPE slis_t_fieldcat_alv.

DATA:ls_fieldcat TYPE slis_fieldcat_alv.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „PRICE‟.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CURRENCY‟.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CARRID‟.

ls_fieldcat-key = ‘X’.

Page 6: SAP ALV Programming Techniques

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CONNID‟.

ls_fieldcat-key = ‘X’.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „FLDATE‟.

ls_fieldcat-key = ‘X’.

APPEND ls_fieldcat TO e01_lt_fieldcat.

ENDFORM.

Just,notice the effect:

ALV with KEY specified in Field catalog

You would notice two important effects:

1. All the columns marked as KEY fields are positioned before non-key fields. This preference of key

fields over non-key fields can not be altered by any other property in field catalog.

2. All the columns marked as KEY fields are highlighed.

Having,understood the effect of property KEY in the field catalog,lets proceed to the usage of property COL_POS.

Property COL_POS is used to arrange the key fields and non-key fields within themselves. If

specified,the columns will appear in the ascending order of COL_POS. In case of conflict e.g. two key

fields or two non-key fields having same COL_POS,they will be positioned in the order in which they appear in the field catalog internal table.

Lets observe the effect of changed code with COL_POS property incorporated.

FORM e01_fieldcat_init USING e01_lt_fieldcat TYPE slis_t_fieldcat_alv.

DATA:ls_fieldcat TYPE slis_fieldcat_alv.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „PRICE‟.

ls_fieldcat-col_pos = 2.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CURRENCY‟.

ls_fieldcat-col_pos = 1.

Page 7: SAP ALV Programming Techniques

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CARRID‟.

ls_fieldcat-key = „X‟.

ls_fieldcat-col_pos = 2.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CONNID‟.

ls_fieldcat-key = „X‟.

ls_fieldcat-col_pos = 2.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „FLDATE‟.

ls_fieldcat-key = „X‟.

ls_fieldcat-col_pos = 1.

APPEND ls_fieldcat TO e01_lt_fieldcat.

ENDFORM.

Here‟s the output:

ALV with COL_POS specified in Field catalog

You would notice that the key fields continue to display before non-key fields,even though the non-

key fields with lesser COL_POS value are placed before key fields in the fields catalog internal table. Apart from this,you will find the following effects because of the COL_POS property introduction over

the previous output. 1. Non-key fields CURRENCY &PRICE have arranged themselves in the ascending order of their

COL_POS value. 2. Among key fields,FLDATE is positioned first,since it has COL_POS value of ‟1′and is not facing any

dispute. However,there‟s a dispute between columns CARRID &CONNID both having COL_POS value of ‟2′. So,here the conflict is resolved based on the order in which they are placed in field catalog

internal table. Since,CARRID appears before CONNID,CARRID wins over CONNID.

Hope,the above example clarifies the usage of properties KEY &COL_POS in field catalog (SLIS_T_FIELDCAT_ALV).

ROW_POS in field catalog Does you business requirement needs single line item in ALV to be displayed in multiple rows/lines for better readability? You then surely need a multi-line ALV,which can be implemented by just a

simple tweaking in your ALV field catalog.

Page 8: SAP ALV Programming Techniques

You can achieve this objective using property ROW_POS of Field catalog. Just assign a value

between 1 to 3 to each of the records (denoting output internal table‟s columns/fields) in field

catalog. That‟s it. Value is restricted between 1 and 3 since ALV can handle maximum of 3 rows for 1 ALV line item.

The example followed will completely clear the usage.

Here is the code to build the field catalog of an ALV.

FORM e01_fieldcat_init USING e01_lt_fieldcat TYPE slis_t_fieldcat_alv. DATA:ls_fieldcat TYPE slis_fieldcat_alv.

ls_fieldcat-fieldname = „CARRID‟. ls_fieldcat-key = „X‟.

ls_fieldcat-ref_fieldname = „CARRID‟.

ls_fieldcat-ref_tabname = „SFLIGHT‟. ls_fieldcat-row_pos = 1.

APPEND ls_fieldcat TO e01_lt_fieldcat. CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CONNID‟. ls_fieldcat-key = „X‟.

ls_fieldcat-ref_fieldname = „CONNID‟. ls_fieldcat-ref_tabname = „SFLIGHT‟.

ls_fieldcat-row_pos = 1.

APPEND ls_fieldcat TO e01_lt_fieldcat. CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „FLDATE‟. ls_fieldcat-key = „X‟.

ls_fieldcat-ref_fieldname = „FLDATE‟. ls_fieldcat-ref_tabname = „SFLIGHT‟.

ls_fieldcat-row_pos = 1. APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „PRICE‟. ls_fieldcat-ref_fieldname = „PRICE‟.

ls_fieldcat-ref_tabname = „SFLIGHT‟. ls_fieldcat-row_pos = 2.

APPEND ls_fieldcat TO e01_lt_fieldcat. CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CURRENCY‟. ls_fieldcat-ref_fieldname = „CURRENCY‟.

ls_fieldcat-ref_tabname = „SFLIGHT‟.

ls_fieldcat-row_pos = 2. APPEND ls_fieldcat TO e01_lt_fieldcat.

ENDFORM.

FORM e01_fieldcat_init USING e01_lt_fieldcat TYPE slis_t_fieldcat_alv.

DATA:ls_fieldcat TYPE slis_fieldcat_alv.

ls_fieldcat-fieldname = „CARRID‟.

ls_fieldcat-key = „X‟.

ls_fieldcat-ref_fieldname = „CARRID‟.

ls_fieldcat-ref_tabname = „SFLIGHT‟.

ls_fieldcat-row_pos = 1.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CONNID‟.

ls_fieldcat-key = „X‟.

Page 9: SAP ALV Programming Techniques

ls_fieldcat-ref_fieldname = „CONNID‟.

ls_fieldcat-ref_tabname = „SFLIGHT‟.

ls_fieldcat-row_pos = 1.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „FLDATE‟.

ls_fieldcat-key = „X‟.

ls_fieldcat-ref_fieldname = „FLDATE‟.

ls_fieldcat-ref_tabname = „SFLIGHT‟.

ls_fieldcat-row_pos = 1.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „PRICE‟.

ls_fieldcat-ref_fieldname = „PRICE‟.

ls_fieldcat-ref_tabname = „SFLIGHT‟.

ls_fieldcat-row_pos = 2.

APPEND ls_fieldcat TO e01_lt_fieldcat.

CLEAR ls_fieldcat.

ls_fieldcat-fieldname = „CURRENCY‟.

ls_fieldcat-ref_fieldname = „CURRENCY‟.

ls_fieldcat-ref_tabname = „SFLIGHT‟.

ls_fieldcat-row_pos = 2.

APPEND ls_fieldcat TO e01_lt_fieldcat.

ENDFORM.

The ALV with the above field catalog gives the following multi-lined ALV output.

Page 10: SAP ALV Programming Techniques

Multi-lined ALV using ROW_POS specified in Field catalog

All the columns with row_pos as 1 in the field catalog are appearing in 1st row where as the ones

with row_pos as 2 are appearing in the 2nd row,even though they are one single line item in the ALV output internal table.

DISPLAY By Fellow@SAP (14 posts),on April 1st,2011

Rating:8.6/10 (7 votes cast)

By default,when you use function module REUSE_ALV_GRID_DISPLAY to display an ALV,it doesn‟t come with a Row-Selector. This is fine until you face the need of performing certain task on user-

selected rows. Then the user would ask for a Row-Selector. In this post,we would see how can we

add this Row-Selector i.e. ‘Row Selection Button’ in an ALV displayed using REUSE_ALV_GRID_DISPLAY and also how to process the selected rows.

Page 11: SAP ALV Programming Techniques

First,we would see how to add the Row-Selector / Row selection button to

ALV.

Step 1: Add a new character field of length 1 in your output internal table,say „SEL‟. This field is used by ALV to store the information if a particular row is selected or not selected.

Suppose,earlier,you were using internal table lt_outtab for your ALV display. So,now,you would

need a new output table lt_outtab_new with the above mentioned field. This is how you would do it.

DATA:

lt_outtab TYPE STANDARD TABLE OF xyz,

ls_outtab TYPE xyz.

TYPES:

BEGIN OF str_xyz,

sel TYPE c.

INCLUDE STRUCTURE xyz.

TYPES:END OF str_xyz.

DATA:

lt_outtab_new TYPE STANDARD TABLE OF str_xyz,

ls_outtab_new TYPE str_xyz.

………………………………………………

………………………………………………

* Moving your output data from old output table to new output table.

LOOP AT lt_outtab INTO ls_outtab.

MOVE-CORRESPONDING ls_outtab TO ls_outtab_new.

APPEND ls_outtab_new TO lt_outtab_new.

ENDLOOP.

Step 2: Inform the ALV frameowrk about the name of the newly added field in your output internal

table,which ALV should use for storing the information about whether a row is selected or not. You can pass this information to ALV framework by setting attribure BOX_FIELDNAME of the ALV Layout.

DATA:ls_layout TYPE slis_layout_alv.

ls_layout-box_fieldname = ‘SEL’.

Step 3: Use REUSE_ALV_GRID_DISPLAY to display your ALV with the new output internal table and

updated ALV layout.

CALL FUNCTION „REUSE_ALV_GRID_DISPLAY‟

EXPORTING

i_callback_program = sy-repid

i_callback_user_command = „USER_COMMAND‟

is_layout = ls_layout

………………………………….

………………………………….

Page 12: SAP ALV Programming Techniques

TABLES

t_outtab = lt_outtab_new.

This is all you need to do in order to get the Row-Selector displayed.

Next,you need to perform tasks on the selected rows. How to handle this?

You can process the selected rows in the callback user command subroutine (in our example,USER_COMMAND) in the callback program (in our example,sy-repid) which you pass to

FM REUSE_ALV_GRID_DISPLAY through parameters i_callback_user_command & i_callback_program.

All the rows which have been selected by user will have the newly added box-field (in

our example,SEL) marked as ‘X’.

You can then have the a logic to perform various tasks on the selected rows based on your needs.

FORM user_command

USING

r_ucomm LIKE sy-ucomm

rs_selfield TYPE slis_selfield.

LOOP AT lt_outtab_new INTO ls_outtab_new WHERE sel = „X‟.

…………………………………………….

…………………………………………….

ENDLOOP.

ENDFORM.

This is how you add a Row-Selector to your ALV and handle the selected rows in your user command callback.