Wednesday, December 8, 2010

OOPS ALV

The ALV Grid Control (ALV = SAP List Viewer) is a flexible tool for displaying lists. The tool provides common list operations as generic functions and can be enhanced by self-defined options. This allows you to use the ALV Grid Control in a large range of application programs.

Those Who want the basic knowledge can access the SAP Help file here.

First Steps 
This section describes the easiest way to display a list with selected data in the ALV Grid Control. To do this, you must:

  1. Create an instance of the ALV Grid Control and integrate it into a screen.
  2. Select the data to be displayed and pass it together with a description of the fields to the instance.


Note
See also sample report BCALV_GRID_DEMO in development class SLIS .
Creating an ALV Grid Control You instantiate an ALV Grid Control in the same way as other controls:

  1. Declare reference variables for the ALV Grid Control and the container. In addition, declare an internal table that you fill with selected data later on: DATA: grid TYPE REF TO cl_gui_alv_grid,
          g_custom_container TYPE REF TO cl_gui_custom_container
          gt_sflight TYPE TABLE OF sflight.
    Note
    In order to integrate a control into a screen, you can use four different Structure link container controls (in this example, we use the custom container control).
  2. Create a standard screen and mark an area for the custom container control in the graphical Screen Painter (icon identified by letter 'C'). Assign name CCCONTAINER to this area. Note
    Structure link Exercise 1: Reserving an Area for a Control of the Controls Tutorial explains how to mark an area in the alphanumerical Screen Painter version.
  3. In the PBO module of the screen, you must now instantiate the container control and the ALV Grid Control. By doing this, you create a link between the container control and the screen, using the container created in the Screen Painter. Using parameter parent , you define the container control as the parent of the ALV Grid Control:
IF g_custom_container IS INITIAL.
    CREATE OBJECT g_custom_container
       EXPORTING
          CONTAINER_NAME = 'CCCONTAINER'.
    CREATE OBJECT GRID1
       EXPORTING
         I_PARENT = g_custom_container.
ENDIF.



Note
The IF query of reference variable g_custom_container ensures that the instances are only generated when the PBO is processed for the first time.
Caution
Normally, you must use method cl_gui_cfw=>flush to pass the methods called to the frontend. However, since the Control Framework performs an automatic flush at the end of the PBO module, this step is not required here.
When you start the program, although the two instances (the container control and the ALV Grid Control) are generated, they are not visible. Displaying a List in the ALV Grid Control Once you have created the ALV Grid Control and integrated it into a screen using a container control, you must pass the data and its structure to the ALV Grid Control:

  1. Fill the internal table with data: SELECT * FROM sflight INTO TABLE gt_sflight.
  2. Pass the output table and the structure data to the ALV Grid Control. Again, ensure to call this method only once after the ALV Grid Control is created:
CALL METHOD grid->set_table_for_first_display
             EXPORTING I_STRUCTURE_NAME = 'SFLIGHT'
             CHANGING IT_OUTTAB = gt_sflight.



Note
In this case, the structure data is provided through the Data Dictionary. The ALV Grid Control gets the field information from table SFLIGHT and displays all fields of the table.


Title for ALV Using Commentary Writer
Click here to view the Code extract which will help in creating the title.


Add Button to ALV Toolbar with REUSE_ALV_LIST_DISPLAY
How to add button to ALV toolbar using REUSE_ALV_LIST_DISPLAY?
In the program which calls ALV using REUSE_ALV_LIST_DISPLAY,
There are some demo program like BCALV_GRID_08, which is written using ABAP-Controls.
In that example, the button is added using TOOLBAR event of cl_gui_alv_grid.
Copy a standard GUI Status from Standard
got to SE80 --> Function group --> SALV
or 
SE90 -->Programming SubObjects--> Gui Status.
Copy STANDARD GUI Status
you should copy the 'STANDARD' GUI status from program SALV or SAPLKKBL .
Enter your Z program name and the name you what for this status - you can keep it as 'STANDARD' to be simple.
Then you can edit the new status to add or delete buttons. This will also bring in the standard SAP ALV functionality such as sorting/subtotaling etc...
When you call 'REUSE_ALV_GRID_DISPLAY' make sure you pass it the new status name.
an example of one of mine:
*&---------------------------------------------------------------------*

*&      Form  DISP_ALV
*&---------------------------------------------------------------------*
FORM disp_alv .

 CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
   EXPORTING
     i_callback_program       = sy-repid
     i_callback_pf_status_set = 'SET_PF_STATUS'
     i_callback_user_command  = 'USER_COMMAND'
     is_layout                = gfl_layout
     it_fieldcat              = git_fieldcat
*      i_save                   = 'A'
   TABLES
     t_outtab                 = git_final                         
   EXCEPTIONS
     program_error            = 1
     OTHERS                   = 2.

 IF sy-subrc NE 0.
*    MESSAGE e398(00) WITH 'Can not generate ALV list'.
 ENDIF.

ENDFORM.                    " DISP_ALV
Create a form with tes same name you specified in the FM
In this case :
i_callback_pf_status_set = 'SET_PF_STATUS'
*------------------------------------------------------------------*
*       FORM SET_PF_STATUS                                         *
*------------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
 "Copy of 'STANDARD' pf_status from fgroup SALV
 SET PF-STATUS 'ZSTANDARD'.
ENDFORM.                    "set_pf_status

Note : While editing the copied Status
Give your function code with ‘&’ sign then only it will appear in the tool bar other wise it will be available when you right click the application tool bar
for example
&GENB       Generate Excise Invoice
USER COMMAND - to capture activities happening in ALV
*&---------------------------------------------------------------------*
*&      Form  user_command
*&---------------------------------------------------------------------*
FORM user_command USING ucomm TYPE sy-ucomm
                                                         field TYPE slis_selfield.
 DATA ref1 TYPE REF TO cl_gui_alv_grid.
 CASE ucomm.
   WHEN '&GENB'.
******************Getting the changes from ALV. like the checked line items ***************
     CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
       IMPORTING
         e_grid = ref1.
     CALL METHOD ref1->check_changed_data.
  
  ENDCASE.
ENDFORM.                    "USER_COMM
To refresh...
rs_selfield-refresh = 'X'. "structure slis_selfield
How to display ALV in Custom Container.

Code Extract
data : alv_container  type ref to cl_gui_docking_container.
data : alv_grid    type ref to cl_gui_alv_grid.

data : layout type lvc_s_layo.

data : variant type  disvariant.

check alv_container is initial.

 CREATE OBJECT alv_container
EXPORTING
* parent                   =
  repid                    = sy-repid
  dynnr                    = sy-dynnr
  side                     = alv_container->dock_at_left
*   extension                = 1550
* style                    =
* lifetime                 = lifetime_default
* caption                  =
* metric                   = 0
* ratio                    = '95'
* no_autodef_progid_dynnr =
* name                     =
EXCEPTIONS
  cntl_error               = 1
  cntl_system_error        = 2
  create_error             = 3
  lifetime_error           = 4
  lifetime_dynpro_dynpro_link = 5
  OTHERS                   = 6
  .
 IF sy-subrc  0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
 ENDIF.

*- Make the docking container as parent to the grid

 CREATE OBJECT alv_grid
EXPORTING
  i_parent       = alv_container
EXCEPTIONS
  error_cntl_create = 1
  error_cntl_init   = 2
  error_cntl_link   = 3
  error_dp_create   = 4
  OTHERS         = 5.
 IF sy-subrc  0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
 ENDIF.

*  layout-zebra = 'X'.
 PERFORM build_fieldcat.
 variant-report = sy-repid.

*- Call grid for display

 CALL METHOD alv_grid->set_table_for_first_display
EXPORTING
  i_structure_name            = 'WT_TEST'
  is_variant                 = variant
  i_save                      = 'A'
*   is_layout                   = layout
CHANGING
  it_outtab                   = WT_TEST
  it_fieldcatalog             = wt_fcat_log
EXCEPTIONS
  invalid_parameter_combination = 1
  program_error              = 2
  too_many_lines             = 3
  OTHERS                     = 4
      .
 IF sy-subrc  0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
 ENDIF.