Showing posts with label OO ALV. Show all posts
Showing posts with label OO ALV. Show all posts

Move Cursor to next Row by Pressing Enter in OO ALV

Posted by Krishh Webworld | 6:18 PM | , , | 2 comments »

I was wondering if it is possible to have the same functionality of the Excel: Press Enter and cursor will move to next row.

As OO ABAP using the class CL_GUI_ALV_GRID provides so many methods for the processing at the Cell Levels. After looking to all the methods of the CL_GUI_ALV_GRID, I found there are so many methods which can do the processing at the Cell level. I have started to give the try. After certain tries, I was able to achieve what I was looking for.


Code Snippet for the Class handler for ALV Grid Call
*&---------------------------------------------------------------------*
*& This code snippet sets the handler of the evnets for the OO ALV
*&---------------------------------------------------------------------*
* Data Declaration

DATA: g_grid type ref to cl_gui_alv_grid,
g_event_receiver type ref to lcl_event_receiver.

*
* Registering the Edit event
CALL METHOD g_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter.
*
* Event Handler
create object g_event_receiver.
*
* Event listner method for Edit Event
SET HANDLER g_event_receiver->handle_data_changed FOR g_grid.
*
* calling the instance of the ALV
call method g_grid->set_table_for_first_display
changing it_fieldcatalog = gt_fieldcat
it_outtab = gt_outtab[].


Code Snippet for the Class handler definition and Implementation
*&---------------------------------------------------------------------*
*& This code snippet shows the Event Handler Class's Definition
*& and implementation
*&---------------------------------------------------------------------*
* Definition

class lcl_event_receiver definition.

public section.
methods:
handle_data_changed FOR EVENT data_changed
OF cl_gui_alv_grid
IMPORTING er_data_changed.

endclass.


* Class Implementation
class lcl_event_receiver implementation.
method handle_data_changed.
*
* Local data
data: LE_ROW TYPE I,
LE_VALUE TYPE C,
LE_COL TYPE I,
LES_ROW_ID TYPE LVC_S_ROW,
LES_COL_ID TYPE LVC_S_COL,
LES_ROW_NO TYPE LVC_S_ROID.
*
* Getting Current Cell
CALL METHOD G_GRID->GET_CURRENT_CELL
IMPORTING
E_ROW = le_row
E_VALUE = le_value
E_COL = le_col
ES_ROW_ID = les_row_id
ES_COL_ID = les_col_id
ES_ROW_NO = les_row_no.
*
* Total number of tables
describe table gt_outtab lines sy-index.
*
* Getting next row
les_row_id-INDEX = les_row_id-INDEX + 1.
les_row_no-ROW_ID = les_row_no-ROW_ID + 1.
*
* Set the Next row
if les_row_id-index le sy-index.
CALL METHOD G_GRID->SET_CURRENT_CELL_VIA_ID
EXPORTING
IS_ROW_ID = les_row_id
IS_COLUMN_ID = LES_COL_ID
IS_ROW_NO = les_row_no.
endif.
endmethod.
endclass.

Posted by Krishh Webworld | 12:43 PM | , , | 0 comments »

The requirement is to capture the changed contents – updated, deleted or inserted – after the table maintenance call using the FM VIEW_MAINTENANCE_CALL.

The simple solution to handle this requirement is:
1. Get all the data from the database table for which we will call the FM VIEW_MAINTENANCE_CALL.
2. Call FM VIEW_MAINTENANCE_CALL
3. Get all the data from the database table again in separate internal table Compare the contents of the data using some utility.

This kind of generic solution would be possible, but the performance of the report would be slow. I thought of having something which can tell us directly just after the FM call, which line was changed: update or deleted or inserted. Suddenly, I thought of implementing the Event and use the same data which are there in the global tables of the Maintenance Function.

Based on the thought, new steps to achieve this solution would be:
1. Implement Event in the Maintenance. Export the Main internal table to ABAP memory, which can be accessed later from the calling program.
2. Call the FM VIEW_MAINTENANCE_CALL
3. Import the data from the ABAP memory

In this later approach, we can get the data instantly from the memory. Since we will have the indicators (ACTION field in the table) which will help us to identify the type of change made to table entries.

You can find more information on the Events of the Table Maintenance at: Extended Table Maintenance Events

To implement the solution, I have used the Event 01 - Before Saving the data into Database. We need to create the subroutine which can be called when the event gets hit. When we create a subroutine, it will be created in the same Function group in which we have created the Table mainteinance. Hence, it will provide us the access to all the global tabbles. From all of these tables, we will export the internal table TOTAL to the memory.

TOTAL table contains the fields: ACTION and MARK.

Content of the ACTION field would help us to indetify the type of change:
D – Delete
U – Update
N – New Entry



Code Snippet to Export data in Event 01
 
*&---------------------------------------------------------------------*
*& Form EXPORT_TOTAL_TABLE
*&---------------------------------------------------------------------*
* This form is called in the Event 01 of the table maintainence
* events
*----------------------------------------------------------------------*
FORM EXPORT_TOTAL_TABLE.
*
* Table of the same structure of the TOTAL which can be exported
DATA: BEGIN OF ITAB OCCURS 0.
INCLUDE STRUCTURE ZTEST_NP_EVENT.
INCLUDE STRUCTURE VIMFLAGTAB.
DATA: END OF ITAB.
*
* Moving data to ITAB from total
ITAB[] = TOTAL[].
*
* Clearing memory
FREE MEMORY ID 'ZTEST_EVENT'.
*
* Exporting data to Memory
EXPORT ITAB = ITAB TO MEMORY ID 'ZTEST_EVENT'.
*
ENDFORM. "EXPORT_TOTAL_TABLE



Code Snippet of the Report
 
*&---------------------------------------------------------------------*
*& Report ZTEST_NP_EVENT
*&---------------------------------------------------------------------*
*& This Report will start the Table Maintainence and after saving the
*& data in the table maintinence, it will generate a list which will
*& what data has been newly added, updated or deleted
*&---------------------------------------------------------------------*
REPORT ZTEST_NP_EVENT.
*
* Table with the Same structure of the table which is exported.
DATA: BEGIN OF ITAB OCCURS 0.
INCLUDE STRUCTURE ZTEST_NP_EVENT.
INCLUDE STRUCTURE VIMFLAGTAB.
DATA: END OF ITAB.
*
* Output table
TYPES: BEGIN OF TY_OUT.
INCLUDE TYPE ZTEST_NP_EVENT.
INCLUDE TYPE VIMFLAGTAB.
TYPES: END OF TY_OUT.
*
DATA: IT_OUTPUT TYPE STANDARD TABLE OF TY_OUT.
*
* Output ALV reference
DATA: O_ALV TYPE REF TO CL_SALV_TABLE.
*
*
START-OF-SELECTION.
* Start the Table maintainence
CALL FUNCTION 'VIEW_MAINTENANCE_CALL'
EXPORTING
ACTION = 'U'
VIEW_NAME = 'ZTEST_NP_EVENT'
EXCEPTIONS
CLIENT_REFERENCE = 1
FOREIGN_LOCK = 2
INVALID_ACTION = 3
NO_CLIENTINDEPENDENT_AUTH = 4
NO_DATABASE_FUNCTION = 5
NO_EDITOR_FUNCTION = 6
NO_SHOW_AUTH = 7
NO_TVDIR_ENTRY = 8
NO_UPD_AUTH = 9
ONLY_SHOW_ALLOWED = 10
SYSTEM_FAILURE = 11
UNKNOWN_FIELD_IN_DBA_SELLIST = 12
VIEW_NOT_FOUND = 13
MAINTENANCE_PROHIBITED = 14
OTHERS = 15.
IF SY-SUBRC <> 0.
ENDIF.
*
* Importing the memory from the Memory ID
* Table has been exported in the Event 01 of the Table
* maintenance events.
IMPORT ITAB TO ITAB FROM MEMORY ID 'ZTEST_EVENT'.
*
* Clear the memory id
FREE MEMORY ID 'ZTEST_EVENT'.
*
* Moving data to output
IT_OUTPUT = ITAB[].
DELETE IT_OUTPUT WHERE ACTION IS INITIAL.
*
* New ALV Instance
TRY.
CL_SALV_TABLE=>FACTORY(
EXPORTING
LIST_DISPLAY = ABAP_FALSE
IMPORTING
R_SALV_TABLE = O_ALV
CHANGING
T_TABLE = IT_OUTPUT ).
CATCH CX_SALV_MSG. "#EC NO_HANDLER
ENDTRY.
*
* Display the ALV
O_ALV->DISPLAY( ).

Dynamic Internal Table Creation

Posted by Krishh Webworld | 12:35 PM | , , | 1 comments »

Shows how to use the RTTS to create a dynamic internal table


From the ABAP release 6.40, SAP has provided RTTS - Run Time Type Services to create types, internal tables at run-time. This RTTS can also be used to describe the properties of the types as well as the fields, internal tables etc.

Sometimes, when we write a program, we don't have all the information of the fields of the internal table. For example: we are accessing the cost element Actul posting data from the table COSP. Now, we have a requirement to generate an output which will have some specified columns - like amount of period 4 to 8 or Amount of period 1 to 3 or some other combination. This kind of secnarioes are perfect examples of the RTTS. We will see how to create a dynamic internal table using this example.

To create a dynamic internal table, we need to:
1. Gather all the Components
2. Generate a Type from this components
3. Generate a Table Type from this created type
4. Create a Data reference of this Table Type
5. Assign this data reference to the Field-Symbol of table type. This Field-symbol will act as our dyanmic internal table

In today's example we will see how to:
1. Create Dynamic internal table
2. Write dynamic select query to data into this dynamic table
3. Change the contents of this dynamic internal table
4. Generate the ALV display using SALV model for this dynamic internal table. Explore how to create ALV using SALV from Tutorials > SALV Table

We will provide the selection of the period for which user wants to generate an output. Based on the entered periods we will create a dynamic type containg the KSTAR (Costing Element) and Amount fields for the month. After creating the dynamic type, we will create a dynamic table type. Using this table type we will create a reference of the data. From this data reference we will assign the internal table to field-symbols.

So, let's see the code snippet:
Code Snippet to create Dynamic ITAB
  
*&---------------------------------------------------------------------*
*& This Code snippet shows how to
*& Create Dynamic Internal Table
*& Dynamic Selection of data
*& Accessing Dynamic data selection
*& Displaying Dynamic internal table in ALV
*&---------------------------------------------------------------------*
report zdynamic_itab.
*
* Exisiting Table type
TYPES: BEGIN OF ty_kstar,
kstar TYPE kstar,
END OF ty_kstar.
*
* Dynamic Table creation
DATA: lo_struct TYPE REF TO cl_abap_structdescr,
lo_element TYPE REF TO cl_abap_elemdescr,
lo_new_type TYPE REF TO cl_abap_structdescr,
lo_new_tab TYPE REF TO cl_abap_tabledescr,
lo_data TYPE REF TO data,
lt_comp TYPE cl_abap_structdescr=>component_table,
lt_tot_comp TYPE cl_abap_structdescr=>component_table,
la_comp LIKE LINE OF lt_comp,
lf_months TYPE monat,
lf_run_mon TYPE monat.
*
* Dynamic Selection fields
TYPES: BEGIN OF ty_fields,
field TYPE char30,
END OF ty_fields.
*
DATA: lt_fields TYPE STANDARD TABLE OF ty_fields,
la_fields TYPE ty_fields.
*
* field symbols to access the dynamic table
FIELD-SYMBOLS: TYPE ANY TABLE,
TYPE ANY,
TYPE ANY.
*
* Selection Screen
PARAMETERS: p_mon_fr TYPE monat,
p_mon_to TYPE monat.
*
START-OF-SELECTION.
*
*$*$*...............Dynamic Internal Table........................*$*$*
* 1. Getting Compoents from existing type
lo_struct ?= cl_abap_typedescr=>describe_by_name( 'TY_KSTAR' ).
lt_comp = lo_struct->get_components( ).
APPEND LINES OF lt_comp TO lt_tot_comp.
*
* 2. Adding required fields based on the single data element
* Determining Number of fields
lf_months = ( p_mon_to - p_mon_fr ) + 1.
lf_run_mon = p_mon_fr.
*
DO lf_months TIMES.
*
* Element Description
lo_element ?= cl_abap_elemdescr=>describe_by_name( 'WTGXXX' ).
*
* Field name
CONCATENATE 'WTG0' lf_run_mon INTO la_comp-name.
*
* Field type
la_comp-type = cl_abap_elemdescr=>get_p(
p_length = lo_element->length
p_decimals = lo_element->decimals ).
*
* Filling the component table
APPEND la_comp TO lt_tot_comp.
CLEAR: la_comp.
*
lf_run_mon = lf_run_mon + 1.
ENDDO.
*
* 3. Create a New Type
lo_new_type = cl_abap_structdescr=>create( lt_tot_comp ).
*
* 4. New Table type
lo_new_tab = cl_abap_tabledescr=>create(
p_line_type = lo_new_type
p_table_kind = cl_abap_tabledescr=>tablekind_std
p_unique = abap_false ).
*
* 5. data to handle the new table type
CREATE DATA lo_data TYPE HANDLE lo_new_tab.
*
* 6. New internal table in the fieldsymbols
ASSIGN lo_data->* TO .
*
*$*$*...............Dynamic Selection.............................*$*$*
* Filling up the table for the Selection fields of Select Query
LOOP AT lt_tot_comp INTO la_comp.
la_fields-field = la_comp-name.
APPEND la_fields TO lt_fields.
CLEAR: la_comp, la_fields.
ENDLOOP.
*
* Selecting data
SELECT (lt_fields)
INTO TABLE
FROM cosp
UP TO 10 ROWS.
*
*$*$*...............Accessing dynamic table.......................*$*$*
LOOP AT ASSIGNING .
ASSIGN COMPONENT 'WTG004' OF STRUCTURE TO .
= '100.00'.
ENDLOOP.
*
*
*$*$*...............Displaying using SALV model...................*$*$*
*
DATA: lo_alv TYPE REF TO cl_salv_table.
*
TRY.
cl_salv_table=>factory(
EXPORTING
list_display = abap_false
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = ).
CATCH cx_salv_msg .
ENDTRY.
*
lo_alv->display( ).

SALV Hierarchical Table 3 - Add Expand/Collapse Option

Posted by Krishh Webworld | 12:31 PM | , , | 0 comments »

Today in the series of the SALV Hiearchical list display, we will see how to add the Expand/Collapse button in the Hierarchical ALV. All discussion related to the Hierarchical table display can be found under Tutorials > SALV HS List.

Expand/Collapse button will provide an option to users to only see the Header List. By selecting the expand button, it will show both Header and detail List. By selecting the same button again it will collapse the list and gives only the header list.

Here is the code Snippet which provides the ADD-ON code. This ADD-ON code can be replaced with the relevent section from this code snippet in the base program. You can find the base program code snippet in the post SALV Hierarchical Table 1 : Simple table display. Adding code to base program is like adding the code correction from the OSS Note. Additionally, we will always include the default PF-status in all the code snippet.

UML diagram for the application would be like:

Code snippet to get the Expand / Collapse option in Hierarchical ALV.

Code Snippet to get Expand / Collapse option
 *$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the GENERATE_OUTPUT method
*
PRIVATE SECTION.
METHODS:
set_default_pf
CHANGING

co_hs_alv TYPE REF TO cl_salv_hierseq_table.
*
METHODS:
set_expand_option
CHANGING
co_hs_alv TYPE REF TO cl_salv_hierseq_table.
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*

* In this area we will call the methods which will set the
* different properties to the ALV
*
* Default PF status
CALL METHOD me->set_default_pf
CHANGING
co_hs_alv = o_hs_alv.
*
* Expand Option
CALL METHOD me->set_expand_option
CHANGING

co_hs_alv = o_hs_alv.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
*
METHOD set_default_pf.
DATA: lo_functions TYPE REF TO cl_salv_functions_list.

lo_functions = co_hs_alv->get_functions( ).
lo_functions->set_all( abap_true ).
ENDMETHOD. "set_default_pf
*
METHOD set_expand_option.
*
DATA:
lo_columns TYPE REF TO cl_salv_columns_hierseq.
*
* Get the Columns of the Master
TRY.
lo_columns = co_hs_alv->get_columns( 1 ).
CATCH cx_salv_not_found.
ENDTRY.
*
* set expand column
TRY.
lo_columns->set_expand_column( 'EXPAND' ).
CATCH cx_salv_data_error. "#EC NO_HANDLER
ENDTRY.
*
DATA: lo_level TYPE REF TO cl_salv_hierseq_level.
*
* Set items expanded by default
TRY.
lo_level = co_hs_alv->get_level( 1 ).
lo_level->set_items_expanded( ).
CATCH cx_salv_not_found.
ENDTRY.
*
ENDMETHOD. "set_expand_option
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*



This code will generate output like:

SALV Hierarchical Table 2 - Adding Default PF Status

Posted by Krishh Webworld | 11:45 AM | , , | 0 comments »

Today we will discuss how easy it is to add a Default PF status. In the previous post, we have discussed how to create a simple hierarchical table: SALV Hierarchical Table 1 - Simple table display. All discussion related to hierarchical (sequential) table can be found under Tutorials > SALV HS List.

To add a default PF status in the hierarchial (sequential) list created using the reference of the class CL_SALV_HIERSEQ_TABLE, we need to get the all the default PF status functions using the method GET_FUNCTIONS of the reference of the CL_SALV_HIERSEQ_TABLE.

Here is the code Snippet which provides the ADD-ON code. This ADD-ON code can be replaced with the relevent section from this code snippet in the base program. You can find the base program code snippet in the post SALV Hierarchical Table 1 : Simple table display. Adding code to base program is like adding the code correction from the OSS Note.

UML diagram of the our test program would be like:
Code snippet to add the default pf status is as follows:


Code Snippet
  
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which can

* be implemented to set the properties of the ALV and can be
* called in the GENERATE_OUTPUT method
*
PRIVATE SECTION.
METHODS:
set_default_pf
CHANGING
co_hs_alv TYPE REF TO cl_salv_hierseq_table.
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*

*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
* In this area we will call the methods which will set the
* different properties to the ALV
*
CALL METHOD me->set_default_pf
CHANGING
co_hs_alv = o_hs_alv.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*

*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
*
METHOD set_default_pf.
DATA: lo_functions TYPE REF TO cl_salv_functions_list.
lo_functions = co_hs_alv->get_functions( ).
lo_functions->set_all( abap_true ).
ENDMETHOD. "set_default_pf
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*


This code add-on will generate the default pf status like this:

SALV Hierarchical Table 1 - Simple table display

Posted by Krishh Webworld | 11:40 AM | , | 0 comments »

The new model of ALV - SALV Model - also provides the interface for application developer to develop the Hierarchical list. This type of list are also known as the Sequential List.

We use the hierarchical-sequential table to display simple hierarchical structures. In addition, exactly two hierarchy levels are available for the use: the header level and the position level. For each hierarchy level, we have to specify a table and also the key columns, like foreign keys.

We can use the model by using the reference of the calss CL_SALV_HIERSEQ_TABLE. We can use this class instead of the function module REUSE_ALV_HIERSEQ_LIST_DISPLAY.
Class CL_SALV_HIERSEQ_TABLE has a FACTORY method. We will call this method and ask for the object for the Hierarchical Table. After getting the HS (Hierarchical - Sequential) Object, we will call the DISPLAY method to generate a list on the screen. Isn't it sound too simple? Infact, it is simple.

You can visit the here to know more about the SALV models: SAP List Viewer : New Programming Model --> https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/5dc3e690-0201-0010-1ebf-b85b3bed962d
Check out the Code snippet for the simplest Hierarchical Table:

Code Snippet to generate Hierarchical List
  
*&---------------------------------------------------------------------*
*& This code snippet will show how to use the CL_SALV_HIERSEQ_TABLE to
*& generate the simplest Hierarchical (Sequential) ALV

*&---------------------------------------------------------------------*
REPORT zsalv_hs_simple.
*
*----------------------------------------------------------------------*
* CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
*
PUBLIC SECTION.
*
* Final Header output table
TYPES: BEGIN OF ty_vbak,
vbeln TYPE vbak-vbeln,
erdat TYPE erdat,

auart TYPE auart,
kunnr TYPE kunnr,
expand TYPE char01, "Column for Expand / Collapse
END OF ty_vbak.
*
* FInal Item output table
TYPES: BEGIN OF ty_vbap,
vbeln TYPE vbap-vbeln,
posnr TYPE vbap-posnr,
matnr TYPE vbap-matnr,

arktx TYPE vbap-arktx,
END OF ty_vbap.
*
* Standard internal tables
DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak,
t_vbap TYPE STANDARD TABLE OF ty_vbap.
*
* Hierarchical ALV reference
DATA: o_hs_alv TYPE REF TO cl_salv_hierseq_table.
*
METHODS:
* data selection
get_data,
*
* Generating output
generate_output.
*
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the GENERATE_OUTPUT method
*
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*

*
ENDCLASS. "lcl_report DEFINITION
*
*
START-OF-SELECTION.
DATA: lo_report TYPE REF TO lcl_report.
*
CREATE OBJECT lo_report.
*
lo_report->get_data( ).
*
lo_report->generate_output( ).
*
*----------------------------------------------------------------------*
* CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
*
METHOD get_data.
* data selection - Header
SELECT vbeln erdat auart kunnr
INTO TABLE t_vbak

FROM vbak
UP TO 10 ROWS.
*
* data selection - Item
SELECT vbeln posnr matnr arktx
INTO TABLE t_vbap
FROM vbap
FOR ALL ENTRIES IN t_vbak
WHERE vbeln = t_vbak-vbeln.
*
ENDMETHOD. "get_data
*
*.......................................................................

METHOD generate_output.
* New ALV instance
* We are calling the static Factory method which will give back
* the ALV object reference.
*
DATA: lx_data_err TYPE REF TO cx_salv_data_error,
lx_not_found TYPE REF TO cx_salv_not_found.
*
* Fill the Binding table. Here we hae to provide the relationship
* between all the Common Key fields in the Master and Sl
ave
* table. Based on this relationship, we will get the output.

DATA: lt_bind TYPE salv_t_hierseq_binding,
la_bind LIKE LINE OF lt_bind.
*
la_bind-master = 'VBELN'. " VBELN as field of my T_VBAK
la_bind-slave = 'VBELN'. " VBELN as field of my T_VBAP
APPEND la_bind TO lt_bind.

*
* call factory method to generate the output
TRY.
CALL METHOD cl_salv_hierseq_table=>factory
EXPORTING

t_binding_level1_level2 = lt_bind
IMPORTING
r_hierseq = o_hs_alv
CHANGING
t_table_level1 = t_vbak
t_table_level2 = t_vbap.

CATCH cx_salv_data_error INTO lx_data_err.
CATCH cx_salv_not_found INTO lx_not_found.
ENDTRY.
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*

* In this area we will call the methods which will set the
* different properties to the ALV
*
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*

*
* Displaying the ALV
* Here we will call the DISPLAY method to get the output on the screen
o_hs_alv->display( ).
*
ENDMETHOD. "generate_output
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*

*
* In this area we will implement the methods which are defined in
* the class definition
*

*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
*
ENDCLASS. "lcl_report IMPLEMENTATION


This code will generate an output like this:
Note: I had tried to build this report in the Object Oriented manner. You can check the UML diagram as below. This desgin will provide us the great flexibility to add more methods will set and get different properties of the O_HS_ALV object. For example, if want to add the expand & collapse button, we will add one private method SET_EXPAND_OPTION. We will implement this method by adding the relative code and call this method before the list display.


In the code snippet, I have tried to differential three section which can be changed easily. In next blog posts, I will use this program as the base program and provide the code snippet to add the new functionality. Like: Section of CODE_ADD_1 can be replaced with the code provided in the future blog post.

SALV Table 9 - Apply Colors

Posted by Krishh Webworld | 11:31 AM | , , | 2 comments »

In the series of the SALV Model Table display, today we will see how to apply colors to the ALV Grid. In this regards, we will see how to apply colors to the Specific Cell, Specific Row or Specific Column. You can find all the Previous discussion at Tutorials > SALV Table Display.

Color plays a very important role in the formatting. It will help us to generate the a rich list which can help users to notice the exceptional data - for example, negative salary to employee or Low material availabilty.

At most colors can be applied to three levels:

  1. Perticular Cell: To apply color at perticular cell, we need to add the details about the field and color in COLOR table at each record. Than we have to set this Color Table in the object of the COLUMNS which contains the information about all our information (CL_SALV_COLUMNS_TABLE).
  2. Entire Row: To apply color to Entire row, we have to do the same thing as the applying the color to Perticular Cell, but we will not specify the FIELDNAME. This way system will understand that it has to apply the color to entire row.
  3. Entire Column: To apply color to Entire column, we have to get the specific Column from the COLUMNS object and than we need to set the Color Property of the Specific Column and we are done.


In this example I need to change the output table sturcture as compared to previous discussion in this series, so I will provide the entire code which generate the output which has all three scenario. For test purpose, We will apply: Red color to Sales Doc Type in 3rd Row, Green Color to 5th row, Yellow color to Created on Column.

UML Diagram for this application is like:
Code snippet to generate the ALV with Colors using the SALV model.
Code Snippet to Generate ALV with Colors
  
*&---------------------------------------------------------------------*
*& This code snippet will show how to use the CL_SALV_TABLE to
*& generate the ALV and COLORs for Column, Row and Specific Cell
*&---------------------------------------------------------------------*
REPORT ztest_oo_alv_color.
*
*----------------------------------------------------------------------*

* CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
*
PUBLIC SECTION.
*
* Final output table
TYPES: BEGIN OF ty_vbak,
vbeln TYPE vbak-vbeln,
erdat TYPE erdat,
auart TYPE auart,

kunnr TYPE kunnr,
t_color TYPE lvc_t_scol,
END OF ty_vbak.
TYPES: ty_t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
DATA: t_vbak TYPE STANDARD TABLE OF ty_vbak.
*
* ALV reference
DATA: o_alv TYPE REF TO cl_salv_table.
*
METHODS:
* data selection
get_data,
*
* Generating output
generate_output.
*
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the
*

PRIVATE SECTION.
METHODS:
set_pf_status
CHANGING
co_alv TYPE REF TO cl_salv_table.
*
METHODS:
set_colors
CHANGING
co_alv TYPE REF TO cl_salv_table
ct_vbak TYPE ty_t_vbak.
*
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
ENDCLASS. "lcl_report DEFINITION
*
*
START-OF-SELECTION.
DATA: lo_report TYPE REF TO lcl_report.
*
CREATE OBJECT lo_report.
*
lo_report->get_data( ).
*
lo_report->generate_output( ).
*
*----------------------------------------------------------------------*
* CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
*
METHOD get_data.
* data selection
SELECT vbeln erdat auart kunnr
INTO CORRESPONDING FIELDS OF TABLE t_vbak
FROM vbak

UP TO 20 ROWS.
*
ENDMETHOD. "get_data
*
*.......................................................................
METHOD generate_output.
* New ALV instance
* We are calling the static Factory method which will give back
* the ALV object reference.

*
* exception class

DATA: lx_msg TYPE REF TO cx_salv_msg.
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = o_alv
CHANGING
t_table = t_vbak ).
CATCH cx_salv_msg INTO lx_msg.
ENDTRY.

*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*

* In this area we will call the methods which will set the
* different properties to the ALV
*
* Set default PF status
CALL METHOD set_pf_status
CHANGING
co_alv = o_alv.
*
* Set the colors to ALV display
CALL METHOD set_colors
CHANGING

co_alv = o_alv
ct_vbak = t_vbak.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
* Displaying the ALV
* Here we will call the DISPLAY method to get the output on the screen
o_alv->display( ).
*
ENDMETHOD. "generate_output
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*

*
* In this area we will implement the methods which are defined in
* the class definition
*
*
METHOD set_pf_status.
*
DATA: lo_functions TYPE REF TO cl_salv_functions_list.
*
lo_functions = co_alv->get_functions( ).
lo_functions->set_default( abap_true ).

*
ENDMETHOD. "set_pf_status
*
METHOD set_colors.
*
*.....Color for COLUMN.....
DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table,
lo_col_tab TYPE REF TO cl_salv_column_table.

DATA: ls_color TYPE lvc_s_colo. " Colors strucutre
*
* get Columns object
lo_cols_tab = co_alv->get_columns( ).
*
INCLUDE .
*
* Get ERDAT column & set the yellow Color fot it
TRY.
lo_col_tab ?= lo_cols_tab->get_column( 'ERDAT' ).
ls_color-col = col_total.

lo_col_tab->set_color( ls_color ).
CATCH cx_salv_not_found.
ENDTRY.
*
*.......Color for Specific Cell & Rows.................

* Applying color on the 3rd Row and Column AUART
* Applying color on the Entire 5th Row
*
DATA: lt_s_color TYPE lvc_t_scol,
ls_s_color TYPE lvc_s_scol,
la_vbak LIKE LINE OF ct_vbak,
l_count TYPE i.

*
LOOP AT ct_vbak INTO la_vbak.
l_count = l_count + 1.
CASE l_count.

* Apply RED color to the AUART Cell of the 3rd Column
WHEN 3.
ls_s_color-fname = 'AUART'.
ls_s_color-color-col = col_negative.
ls_s_color-color-int = 0.
ls_s_color-color-inv = 0.
APPEND ls_s_color TO lt_s_color.

CLEAR ls_s_color.
*
* Apply GREEN color to the entire row # 5
* For entire row, we don't pass the Fieldname

WHEN 5.
ls_s_color-color-col = col_positive.
ls_s_color-color-int = 0.
ls_s_color-color-inv = 0.
APPEND ls_s_color TO lt_s_color.
CLEAR ls_s_color.

ENDCASE.
* Modify that data back to the output table
la_vbak-t_color = lt_s_color.
MODIFY ct_vbak FROM la_vbak.
CLEAR la_vbak.
CLEAR lt_s_color.

ENDLOOP.
*
* We will set this COLOR table field name of the internal table to
* COLUMNS tab reference for the specific colors
TRY.
lo_cols_tab->set_color_column( 'T_COLOR' ).

CATCH cx_salv_data_error. "#EC NO_HANDLER
ENDTRY.
*
ENDMETHOD. "set_colors
*
*
*
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*
ENDCLASS. "lcl_report IMPLEMENTATION


Without Colors it will look like:

With Colors it will look like:

SALV Table 8 - Add & Handle Hotspot

Posted by Krishh Webworld | 11:17 AM | , , | 0 comments »

In the series of the SALV model table display, today we will see how to add the hotspot and after adding, how to handle that hotspot. You can find all the previous discussion at Tutorials > SALV Table Display.

Hotspot is useful in most of the ALV reports to drill-down from the main list. For example, in the Sales order report, it would be great if we provide a drill-down to VA03 (Sales Order display). By this way users would have better flexibility to look into the respective document information.

To add the Hotspot, we need to get the object reference of the CL_SALV_COLUMNS_TABLE from the ALV object (reference to CL_SALV_TABLE). This object reference contains the properties related to ALL the columns. For implementing HOTSPOT, we need to change the specific column properties (i.e. Sales Order or Customer). For this purpose, we need to have an access of the object reference CL_SALV_COLUMN_TABLE - which will contain the properties of the speicific column. We will set the specific Cell type for the HOTSPOT. Here we will use the Casting concept of the Object Oriented to refer the properties of the CL_SAL_COLUMN_TABLE. Read this for casting: ABAP OBjects - Narrowing Cast & ABAP Objects: Widening Cast

Ater implementing the Hotspot, we need to implement the Event Hanlder to handle the Hotspot. Here, we will implement the event listner method for the event LINK_CLICK of the class CL_SALV_EVENTS_TABLE. We need to set the handler of this events to let the system know, where to go when the event occurs.

Note: Here we have used the class CL_SALV_COLUMNS_TABLE which is different than the class CL_SALV_COLUMNS discussed as in the previous post SALV Model 7 - Changing Column settings.

Here is the code snippet to which provides the ADD-ON code to our Base program. The base program can be found in the SALV Model 1: Normal ALV Table Display.

UML Diagram would be like:

Code Snippet for Hotspot and event handling
  
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which ca
n
* be implemented to set the properties of the ALV and can be
* called in the

*
PRIVATE SECTION.
* Set the various column properties
METHODS:
set_hotspot_vbeln
CHANGING
co_alv TYPE REF TO cl_salv_table
co_report TYPE REF TO lcl_report.

*
* Event Handler for HOTSPOT event
METHODS:
on_link_click
FOR EVENT link_click OF cl_salv_events_table
IMPORTING
row
column .
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*

* In this area we will call the methods which will set the
* different properties to the ALV

*
* Set Up the Hotspot & Event Handler
CALL METHOD set_hotspot_vbeln
CHANGING
co_alv = o_alv
co_report = lo_report.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*

*
* In this area we will implement the methods which are defined in
* the class definition
*
METHOD set_hotspot_vbeln.
*
*...HotSpot
DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table,
lo_col_tab TYPE REF TO cl_salv_column_table.
*
* get Columns object
lo_cols_tab = co_alv->get_columns( ).
*
* Get VBELN column
TRY.
lo_col_tab ?= lo_cols_tab->get_column( 'VBELN' ).
CATCH cx_salv_not_found.
ENDTRY.
*
* Set the HotSpot for VBELN Column
TRY.
CALL METHOD lo_col_tab->set_cell_type
EXPORTING

value = if_salv_c_cell_type=>hotspot.
.
CATCH cx_salv_data_error .
ENDTRY.
*
*...Events
DATA: lo_events TYPE REF TO cl_salv_events_table.
*
* all events
lo_events = o_alv->get_event( ).
*
* event handler
SET HANDLER co_report->on_link_click FOR lo_events.
*
ENDMETHOD. "set_hotspot_vbeln
*
* Handles the UI on the VBELN (HotSpot)
METHOD on_link_click.
*
DATA: la_vbak TYPE ty_vbak.
*
* Get the Sales Order number from the table
READ TABLE lo_report->t_vbak INTO la_vbak INDEX row.
IF la_vbak-vbeln IS NOT INITIAL.

MESSAGE i398(00) WITH 'You have selected' la_vbak-vbeln.
ENDIF.
*
ENDMETHOD. "on_link_click
*
*$*$*.....CODE_ADD_3 - End....................................3..*
$*$*



This code will generate output like this:

After event handling of the Hotspot:

SALV Table 7 - Changing Column settings

Posted by Krishh Webworld | 11:11 AM | , , , | 0 comments »

Today in the SALV model tutorial series, we will see how we can change the Column properites. You can find all the previous discussion in this series can be found at Tutorials > SALV Table Display.

To change the propeties of the Columns first we need to get the Column object from the SALV Object. The reference of the column object would be CL_SALV_COLUMNS. This object reference would provide us the access to the properties of all the columns. For example, if we need to set the column width optimization for the table, we can set using this columns reference.

To change the individual column properties, we need to get the individual column reference from the reference created using the CL_SALV_COLUMNS. The individual column property can be changed by the getting the reference of CL_SALV_COLUMN. Like, to change the column heading, we need to first get the column from the Columns object and than we need to set specific property.

Here is the code snippet to which provides the ADD-ON code to our Base program. The base program can be found in the SALV Model 1: Normal ALV Table Display.

UML Diagram would be like:

Code Snippet
  
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the
*

PRIVATE SECTION.
* Set the various column properties
METHODS:
set_columns
CHANGING
co_alv TYPE REF TO cl_salv_table.
*
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*

*
* In this area we will call the methods which will set the
* different properties to the ALV
*
* Setting up the Columns
CALL METHOD me->set_columns
CHANGING
co_alv = o_alv.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*

*
* In this area we will implement the methods which are defined in
* the class definition
*
METHOD set_columns.
*
*...Get all the Columns
DATA: lo_cols TYPE REF TO cl_salv_columns.
lo_cols = o_alv->get_columns( ).
*
* set the Column optimization

lo_cols->set_optimize( 'X' ).
*
*...Process individual columns
DATA: lo_column TYPE REF TO cl_salv_column.
*
* Change the properties of the Columns KUNNR
TRY.
lo_column = lo_cols->get_column( 'KUNNR' ).
lo_column->set_long_text( 'Sold-To Party' ).
lo_column->set_medium_text( 'Sold-To Party' ).
lo_column->set_short_text( 'Sold-To' ).

lo_column->set_output_length( 10 ).
CATCH cx_salv_not_found. "#EC NO_HANDLER
ENDTRY.
*
ENDMETHOD. "SET_COLUMNS
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*


This code snippet will generat output like this:

SALV Table 6 - Dispaly Settings

Posted by Krishh Webworld | 11:04 AM | , , , | 1 comments »

In the series of the SALV Simple Model, we will see how we can set the Display Settings to the entire ALV. You can find the previous discussions in this blog series at Tutorials > SALV Table Display

To set the display settings, we need to use the reference of the class CL_SALV_DISPLAY_SETTINGS. We will ask the reference of the Display settings from our ALV object created using the CL_SALV_TABLE. By using the dispaly settings, we can set the Zebra style, Title of the ALV etc.

Here is the code snippet to which provides the ADD-ON code to our Base program. The base program can be found in the SALV Model 1: Normal ALV Table Display.

UML diagram for this example is like:

Code Snippet
  
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*
* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the
*

PRIVATE SECTION.
METHODS:
set_display_setting
CHANGING
co_alv TYPE REF TO cl_salv_table.
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
* In this area we will call the methods which will set the

* different properties to the ALV
CALL METHOD set_display_setting
CHANGING
co_alv = o_alv.
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
*
METHOD set_display_setting.
*
DATA: lo_display TYPE REF TO cl_salv_display_settings.
*
* get display object
lo_display = o_alv->get_display_settings( ).
*
* set ZEBRA pattern
lo_display->set_striped_pattern( 'X' ).
*
* Title to ALV
lo_display->set_list_header( 'ALV Test for Display Settings' ).
*
ENDMETHOD. "SET_DISPLAY_SETTING
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*


This code will generate the output like this:

SALV Table 5 - Add Header (Top of page) & Footer (End of Page)

Posted by Krishh Webworld | 10:56 AM | , , | 0 comments »

Today we will discuss how to add the Header and Footer using the SALV model. In ALV, header (top-of-page) and footer (end-of-page) play important role in presentation of the data. Header and footer are imortant when we need to print the report and use it for later decisions. Assume the report which has only columns and no information. Does it really define "Information"? - NO. So, let's get started on how to create Header and Footer.

Header and Footer both can be created using the class reference CL_SALV_FORM_LAYOUT_GRID. We will create:
- A reference of the class CL_SALV_FORM_LAYOUT_GRID
- Use methods of this reference to create a Lables & Flow.
- Set this reference into the main ALV object (reference to CL_SALV_TABLE).

Labels are useful to generate the text output in intensified or Bold letters. This can be used to print the Header, like - Sales Revenue Report. We can also use this kind of lables in the Footer section to highlight the total amount of all orders - for example. Flow is useful to dispaly the information in the tabular format. This flow can be used to display the Selection parameters, selected rows etc.

Here is the code snippet to which provides the ADD-ON code to our Base program. The base program can be found in the SALV Model 1: Normal ALV Table Display.

UML diagram for the test program will be like:

Code Snippet to generate Header & Footer
  
*$*$*.....CODE_ADD_1 - Begin..................................1..*$*$*
*

* In this section we will define the private methods which can
* be implemented to set the properties of the ALV and can be
* called in the
*
PRIVATE SECTION.
* Default Pf Status
METHODS:
set_pf_status
CHANGING
co_alv TYPE REF TO cl_salv_table.
* Set Top of page
METHODS:
set_top_of_page
CHANGING
co_alv TYPE REF TO cl_salv_table.

*
* Set End of page
METHODS:
set_end_of_page
CHANGING
co_alv TYPE REF TO cl_salv_table.
*
*$*$*.....CODE_ADD_1 - End....................................1..*$*$*
*
*$*$*.....CODE_ADD_2 - Begin..................................2..*$*$*
*
* In this area we will call the methods which will set the
* different properties to the ALV

*
* Setting up the default PF status
CALL METHOD set_pf_status
CHANGING
co_alv = o_alv.

*
* Calling the top of page method
CALL METHOD me->set_top_of_page
CHANGING
co_alv = o_alv.
*
* Calling the End of Page method

CALL METHOD me->set_end_of_page
CHANGING
co_alv = o_alv.
*
*$*$*.....CODE_ADD_2 - End....................................2..*$*$*
*
*$*$*.....CODE_ADD_3 - Begin..................................3..*$*$*
*
* In this area we will implement the methods which are defined in
* the class definition
*

METHOD set_pf_status.
*
DATA: lo_functions TYPE REF TO cl_salv_functions_list.
* Default Functions
lo_functions = co_alv->get_functions( ).
lo_functions->set_default( abap_true ).
*
ENDMETHOD. "set_pf_status
*
METHOD set_top_of_page.
*
DATA: lo_header TYPE REF TO cl_salv_form_layout_grid,
lo_h_label TYPE REF TO cl_salv_form_label,
lo_h_flow TYPE REF TO cl_salv_form_layout_flow.
*
* header object
CREATE OBJECT lo_header.
*
* To create a Lable or Flow we have to specify the target
* row and column number where we need to set up the output
* text.
*

* information in Bold
lo_h_label = lo_header->create_label( row = 1 column = 1 ).
lo_h_label->set_text( 'Header in Bold' ).
*
* information in tabular format
lo_h_flow = lo_header->create_flow( row = 2 column = 1 ).
lo_h_flow->create_text( text = 'This is text of flow' ).
*
lo_h_flow = lo_header->create_flow( row = 3 column = 1 ).
lo_h_flow->create_text( text = 'Number of Records in the output' ).
*
lo_h_flow = lo_header->create_flow( row = 3 column = 2 ).
lo_h_flow->create_text( text = 20 ).
*
* set the top of list using the header for Online.

co_alv->set_top_of_list( lo_header ).
*
* set the top of list using the header for Print.
co_alv->set_top_of_list_print( lo_header ).
*
ENDMETHOD. "set_top_of_page
*
METHOD set_end_of_page.
*
DATA: lo_footer TYPE REF TO cl_salv_form_layout_grid,
lo_f_label TYPE REF TO cl_salv_form_label,
lo_f_flow TYPE REF TO cl_salv_form_layout_flow.

*
* footer object
CREATE OBJECT lo_footer.
*
* information in bold
lo_f_label = lo_footer->create_label( row = 1 column = 1 ).
lo_f_label->set_text( 'Footer .. here it goes' ).

*
* tabular information
lo_f_flow = lo_footer->create_flow( row = 2 column = 1 ).
lo_f_flow->create_text( text = 'This is text of flow in footer' ).

*
lo_f_flow = lo_footer->create_flow( row = 3 column = 1 ).
lo_f_flow->create_text( text = 'Footer number' ).
*
lo_f_flow = lo_footer->create_flow( row = 3 column = 2 ).
lo_f_flow->create_text( text = 1 ).
*
* Online footer
co_alv->set_end_of_list( lo_footer ).
*
* Footer in print
co_alv->set_end_of_list_print( lo_footer ).
*
ENDMETHOD. "set_end_of_page
*
*$*$*.....CODE_ADD_3 - End....................................3..*$*$*



This code will generate output like this:


When you print this output, it will generate a spool like:


All SALV discussions can be found under Tutorials > SALV Table Display.

Archives

Subscribe Now: Feed Icon