Friday, May 01, 2009

ALV Report: Show selection option

In previous post I mentioned about the function modules that can be used to display the selection options on the header of the report. Here I am going to give an example of the actual use of these function modules. I am going to start with the ALV report template that I have mentioned. I will be creating a private method to the class LCL_REPORT called BUILD_HEADER. This can display the header in either ALV or normal list output. I am going to read the KNA1 table and display the address of the customers. BTW there is a very good FM that will show the address if you pass the address number to it, the address is returned in a format that can be printed on a label. The function module is ADDRESS_INTO_PRINTFORM.

The class CL_SALV_FORM_LAYOUT_GRID will be used to define the layout and will be attached to the ALV object (class CL_SALV_TABLE) as top of list element. The function module that does the main work for us here is PRINT_SELECTIONS. It will return a table filed with preformatted lines. Here is screen shot of the table values from the debugger.

image

Now all we have to do is strip out unwanted lines and match the label against the values. The class CL_SALV_FORM_LAYOUT_GRID has 2 methods for this label and text creation, which are used in this case.

The selection screen of the report is:

image

In a list output it looks like this:

image

And in ALV output it looks like this.

image

Declaration of the method BUILD_HEADER:

    METHODS:
      build_header IMPORTING i_alv_table TYPE REF TO cl_salv_table.


And the code for the method BUILD_HEADER:



  METHOD build_header.
    TYPES:
     BEGIN OF l_ty_info,
             flag(1),
             olength(1) TYPE x,
             line  TYPE infoline,
           END OF l_ty_info.
    DATA:
       l_t_info TYPE TABLE OF l_ty_info,
       l_s_info TYPE l_ty_info,
       l_row TYPE i,
       l_o_uie TYPE REF TO cl_salv_form_layout_grid,
       l_o_any TYPE REF TO object,
       l_selection_flag,
       l_label_flag,
       l_str_report_by TYPE string,
       l_flag_report_by TYPE boolean.
    IF sy-repi2 IS INITIAL.
      sy-repi2 = sy-repid.
    ENDIF.
    CREATE OBJECT l_o_uie.
    CALL METHOD l_o_uie->create_header_information
      EXPORTING
        row     = 1
        column  = 1
        text    = sy-title
      RECEIVING
        r_value = l_o_any.
    CALL FUNCTION 'PRINT_SELECTIONS'
      EXPORTING
        rname     = sy-repi2
        rvariante = sy-slset
        mode      = 'TABLE'
      TABLES
        infotab   = l_t_info.
    DELETE l_t_info WHERE LINE = c_last_line.
    DELETE l_t_info WHERE LINE IS INITIAL.
    LOOP AT l_t_info INTO l_s_info.
      IF l_s_info-line+1(77) = c_last_line.
        EXIT.
      ENDIF.
      IF l_s_info-flag = 'D'.     " prg run Date and time
        l_row = sy-tabix.
        CALL METHOD l_o_uie->create_label
          EXPORTING
            row     = l_row
            column  = 1
            text    = l_s_info-line+1(77)
          RECEIVING
            r_value = l_o_any.
        l_row = l_row + 1.
        CALL METHOD l_o_uie->create_label
          EXPORTING
            row     = l_row
            column  = 1
            text    = c_last_line+1(20)
          RECEIVING
            r_value = l_o_any.
      ENDIF.
      IF l_s_info-flag <> 'H'.     " Header not encountered so far
        IF l_selection_flag IS INITIAL. " Wait for it
          CONTINUE.
        ELSE.
*       write text
        ENDIF.
      ELSE.                  " We have header so set the flag
        l_selection_flag = abap_true. " Set the flag
        CONTINUE.            " Data is in the next line
      ENDIF.
      IF l_s_info-line+3(1) <> space. " This is the Label
        l_label_flag = abap_true.
        l_row  = l_row + 1.
        CONDENSE l_s_info-line+1(32).
        CALL METHOD l_o_uie->create_label
          EXPORTING
            row     = l_row
            column  = 1
*           colspan = 30
            text    = l_s_info-line+1(32)
          RECEIVING
            r_value = l_o_any.
        IF l_s_info-line+1(32) = 'Report by'.
          l_flag_report_by = abap_true.
        ENDIF.
      ELSE. " This is the Value
        IF l_label_flag = abap_true.
          l_label_flag = abap_false.
        ELSE.
          l_row = l_row + 1.
        ENDIF.
        CONDENSE l_s_info-line+1(77).
        IF l_flag_report_by = abap_true.
          l_s_info-line+1(77) = l_str_report_by.
          l_flag_report_by = abap_false.
        ENDIF.
        CALL METHOD l_o_uie->create_text
          EXPORTING
            row     = l_row
            column  = 2
            text    = l_s_info-line+1(77)
*    TOOLTIP =
          RECEIVING
            r_value = l_o_any
            .
      ENDIF.
    ENDLOOP.
    CALL METHOD l_o_uie->create_label
      EXPORTING
        row     = l_row
        column  = 1
        text    = c_last_line+1(20)
      RECEIVING
        r_value = l_o_any.
    CALL METHOD i_alv_table->set_top_of_list
      EXPORTING
        value = l_o_uie.
  ENDMETHOD.                    "build_header

2 comments:

eddai said...

Hi,
good posts..i have just added your site to http://social.sapdocs.info/abap/

Best
Eddai

SAPDev said...

Thanks. You are doing a fine job yourself! And welcome to the world of SAP!