Skip to Content

The main content in the blog is the Reusable Function module which will display the Internal table contents in HTML format in a Module pool screen.

The Function Module can also handle Currencies, Date and Time format, No. of decimal places, Alignment and other basic needs.

This Function Module can be compared to the FM REUSE_ALV_GRID_DISPLAY.

The following self-explanatory Code snippet will give an idea of how to use this reusable FM in programs.

Step 1: Fetch the required data

Step 2: Build the Field Catalog (very similar to building field catalog in ALV)

Step 3: Create a screen with Custom Container

Step 4: In the PBO of the screen, create an Object for the HTML document

Step 5: Call the Reusable Function Module

Step 6: Embed the HTML document to the Custom Container.

REPORT  zdemo_int_table_to_html.

TYPES: tty_fcat_html TYPE TABLE OF zst_html.

DATA: gt_sflight  TYPE TABLE OF sflight,
      gt_fcat     TYPE tty_fcat_html,
      gp_document TYPE REF TO cl_dd_document,
      gp_area1    TYPE REF TO cl_dd_area.

START-OF-SELECTION.
  CALL SCREEN 100.

*&———————————————————————*
*&      Module  PBO_0100  OUTPUT
*&———————————————————————*
*       text
*———————————————————————-*
MODULE pbo_0100 OUTPUT.

  SET PF-STATUS ‘100’.

  PERFORM f_get_data.
  PERFORM f_build_fcat.
  PERFORM f_display_html.

ENDMODULE.                 ” PBO_0100  OUTPUT

*&———————————————————————*
*&      Module  PAI_0100  INPUT
*&———————————————————————*
*       text
*———————————————————————-*
MODULE pai_0100 INPUT.

  CASE sy-ucomm.
    WHEN ‘BACK’.
      LEAVE PROGRAM.
    WHEN ‘EXIT’.
      LEAVE PROGRAM.
    WHEN OTHERS.
  ENDCASE.

ENDMODULE.                 ” PAI_0100  INPUT

*&———————————————————————*
*&      Form  F_GET_DATA
*&———————————————————————*
* Get Data to Display in HTML format
*———————————————————————-*
FORM f_get_data .

  SELECT * FROM sflight INTO TABLE gt_sflight
    UP TO 20 ROWS.

ENDFORM.                    ” F_GET_DATA
*&———————————————————————*
*&      Form  F_BUILD_FCAT
*&———————————————————————*
* Build Field Catalog for HTML display
*———————————————————————-*
FORM f_build_fcat .

  PERFORM f_fldcat USING ‘CARRID’
                         ‘Airline Code’
                         ”
                         ”
                         ”
                         ”
                         ”
                         ‘CENTER’
                         ”
                         ”
                CHANGING gt_fcat.

  PERFORM f_fldcat USING ‘CONNID’
                         ‘Flight Connection Number’
                         ”
                         ”
                         ”
                         ”
                         ”
                         ‘CENTER’
                         ”
                         ”
                CHANGING gt_fcat.

  PERFORM f_fldcat USING ‘FLDATE’
                         ‘Flight date’
                         ”
                         ”
                         ”
                         ”
                         ”
                         ‘CENTER’
                         ”
                         ”
                CHANGING gt_fcat.

ENDFORM.                    ” F_BUILD_FCAT
*&———————————————————————*
*&      Form  F_DISPLAY_HTML
*&———————————————————————*
*  Display Internal Table in HTML format
*———————————————————————-*
FORM f_display_html .

  PERFORM f_create_doc_object CHANGING gp_document.

  CALL FUNCTION ‘Z_IR_HTML_DC’
    EXPORTING
      it_data        = gt_sflight
      it_fieldcat    = gt_fcat
      iv_table_width = ‘100%’
      iv_heading     = ‘Flight Details’
    CHANGING
      xp_area        = gp_area1.

* Merge the document to the container
  PERFORM f_merge_to_container USING ‘CUSTCTRL_HTML’ CHANGING gp_document.

ENDFORM.                    ” F_DISPLAY_HTML
*&———————————————————————*
*&      Form  F_FLDCAT
*&———————————————————————*
*———————————————————————-*
FORM f_fldcat USING fpv_fname      TYPE zst_html-fieldname
                    fpv_coltext    TYPE zst_html-coltext
                    fpv_width      TYPE zst_html-width
                    fpv_cfieldname TYPE zst_html-cfieldname
                    fpv_bg_color   TYPE zst_html-bg_color
                    fpv_emphasis   TYPE zst_html-emphasis
                    fpv_no_zero    TYPE zst_html-no_zero
                    fpv_align      TYPE zst_html-align
                    fpv_no_decimal TYPE zst_html-no_decimals
                    fpv_foot_note  TYPE zst_html-foot_note
           CHANGING fpt_fcat       TYPE tty_fcat_html.

  DATA: ls_fldcat TYPE zst_html.

  ls_fldcat-fieldname   = fpv_fname.
  ls_fldcat-coltext     = fpv_coltext.
  ls_fldcat-width       = fpv_width.
  ls_fldcat-cfieldname  = fpv_cfieldname.
  ls_fldcat-bg_color    = fpv_bg_color.
  ls_fldcat-emphasis    = fpv_emphasis.
  ls_fldcat-no_zero     = fpv_no_zero.
  ls_fldcat-align       = fpv_align.
  ls_fldcat-no_decimals = fpv_no_decimal.
  ls_fldcat-foot_note   = fpv_foot_note.

  APPEND ls_fldcat TO fpt_fcat.
  CLEAR ls_fldcat.

ENDFORM.                    ” F_FLDCAT
*&———————————————————————*
*&      Form  F_CREATE_DOC_OBJECT
*&———————————————————————*
*———————————————————————-*
FORM f_create_doc_object  CHANGING p_lp_doc TYPE REF TO cl_dd_document.

  IF p_lp_doc IS INITIAL.
    CREATE OBJECT p_lp_doc
      EXPORTING
        style            = cl_dd_document=>strong
        background_color = cl_dd_document=>col_tree_level3
        no_margins       = ‘X’.

* Split of document into up/below
    CALL METHOD p_lp_doc->horizontal_split
      EXPORTING
        split_area = p_lp_doc
      IMPORTING
        below_area = gp_area1.
  ENDIF.

ENDFORM.                    ” F_CREATE_DOC_OBJECT

*&———————————————————————*
*&      Form  F_MERGE_TO_CONTAINER
*&———————————————————————*
*———————————————————————-*
FORM f_merge_to_container  USING    pv_doc TYPE sdydo_value
                           CHANGING lp_document TYPE REF TO cl_dd_document.

  IF lp_document IS NOT INITIAL.
    CALL METHOD lp_document->merge_document.

    CALL METHOD lp_document->display_document
      EXPORTING
        reuse_control = ‘X’
        container     = pv_doc.
  ENDIF.

ENDFORM.                    “f_merge_to_container

The Structure used in this program ZST_HTML will be of the following type. This Structure could be compared to the Field Catalog structure in ALV.

ZST_HTML.jpg

The Code Snippet for the Function Module is given below:

FUNCTION z_ir_html_dc.

*”———————————————————————-

*”*”Local Interface:

*”  IMPORTING

*”     REFERENCE(IT_DATA) TYPE  ANY TABLE

*”     VALUE(IT_FIELDCAT) TYPE  ZTT_HTML OPTIONAL

*”     REFERENCE(IV_HEADING) TYPE  CHAR255 OPTIONAL

*”     REFERENCE(IV_TABLE_WIDTH) TYPE  SDYDO_VALUE OPTIONAL

*”  CHANGING

*”     VALUE(XP_AREA) TYPE REF TO  CL_DD_AREA

*”———————————————————————-

TYPES: BEGIN OF lty_col,
          lp_col TYPE REF TO cl_dd_area,
         END OF lty_col.

  CONSTANTS: lc_c310      TYPE char4  VALUE ‘C310’,
             lc_row_color TYPE char30 VALUE ‘ROW_COLOR’.

  DATA: lp_table    TYPE REF TO cl_dd_table_element,
        lp_col      TYPE REF TO cl_dd_area,
        lt_col      TYPE STANDARD TABLE OF lty_col,
        ls_fieldcat TYPE zir_s_html_fldcat_dc,
        lv_waers    TYPE waers,
        lv_tabix    TYPE i,
        ls_col      TYPE lty_col,
        lv_cols     TYPE i.

  FIELD-SYMBOLS: <lfs_data>   TYPE ANY,
                 <lfs_field>  TYPE ANY,
                 <lfs_color>  TYPE ANY,
                 <lfs_cfield> TYPE ANY.

  IF it_fieldcat[] IS NOT INITIAL.
    ” Total number of Columns
    DESCRIBE TABLE it_fieldcat LINES lv_cols.
  ENDIF.

  “If the fieldcat is empty..
  IF it_fieldcat IS INITIAL.
    PERFORM f_fieldcat_initial USING it_data
                                     it_fieldcat.
  ENDIF.

  IF xp_area IS BOUND.
    ” Write the Documents heading
    CALL METHOD xp_area->add_text
      EXPORTING
        text          = iv_heading
*        sap_color     = cl_dd_area=>list_background
        sap_color     = cl_dd_area=>list_key
*        sap_fontsize  = cl_dd_document=>large
*        sap_fontstyle = cl_dd_area=>serif
        sap_emphasis  = cl_dd_document=>strong.

    ” Add an underline
    CALL METHOD xp_area->underline.

    ” Create table
    CALL METHOD xp_area->add_table
      EXPORTING
        no_of_columns               = lv_cols
        cell_background_transparent = space
        border                      = ‘1’
        width                       = iv_table_width
      IMPORTING
        table                       = lp_table
      EXCEPTIONS
        table_already_used          = 1
        OTHERS                      = 2.

    IF it_fieldcat IS NOT INITIAL.
      LOOP AT it_fieldcat INTO ls_fieldcat.
        CLEAR lv_tabix.
        lv_tabix = sy-tabix.
        FREE lp_col.
        PERFORM f_add_column USING ls_fieldcat
                            CHANGING lp_col
                                     lp_table
                                     lv_tabix.
        ls_col-lp_col = lp_col.
        APPEND ls_col TO lt_col.
      ENDLOOP.

      ” Add Heading
      LOOP AT lt_col INTO ls_col.
        CLEAR lv_tabix.
        lv_tabix = sy-tabix.
        READ TABLE it_fieldcat INTO ls_fieldcat INDEX sy-tabix.
        IF sy-subrc = 0.
          IF lv_tabix = 1.
            CALL METHOD lp_table->new_row
              EXPORTING
                sap_color    = cl_dd_area=>list_key
                sap_emphasis = cl_dd_document=>strong.
          ENDIF.

          PERFORM f_add_row_data USING ls_fieldcat-coltext
                                       space
                                       space
                                       ls_fieldcat-emphasis
                                       space
                                       space
                                       space
                              CHANGING ls_col-lp_col.
        ENDIF.
      ENDLOOP.

      ” Add row data
      LOOP AT it_data ASSIGNING <lfs_data>.
        IF <lfs_data> IS ASSIGNED.
          ASSIGN COMPONENT lc_row_color OF STRUCTURE <lfs_data>
          TO <lfs_color>.
          IF <lfs_color> IS ASSIGNED AND <lfs_color> EQ lc_c310.
            CALL METHOD lp_table->new_row
              EXPORTING
                sap_color    = cl_dd_area=>list_total
                sap_emphasis = cl_dd_document=>strong.
          ELSE.
            CALL METHOD lp_table->new_row.
          ENDIF.
        ENDIF.
        LOOP AT lt_col INTO ls_col.
          CLEAR: lv_waers.
          IF <lfs_cfield> IS ASSIGNED.
             UNASSIGN <lfs_cfield>.
          ENDIF.
          READ TABLE it_fieldcat INTO ls_fieldcat INDEX sy-tabix.
          IF sy-subrc = 0.
            IF <lfs_data> IS ASSIGNED.
              ASSIGN COMPONENT  ls_fieldcat-fieldname OF STRUCTURE
              <lfs_data> TO <lfs_field>.
              IF ls_fieldcat-cfieldname IS NOT INITIAL.
                ASSIGN COMPONENT ls_fieldcat-cfieldname OF STRUCTURE
                <lfs_data> TO <lfs_cfield>.
              ENDIF.
            ENDIF.
            IF <lfs_cfield> IS ASSIGNED.
              lv_waers = <lfs_cfield>.
            ENDIF.
            PERFORM f_add_row_data USING <lfs_field>
                                         lv_waers
                                         ls_fieldcat-bg_color
                                         ls_fieldcat-emphasis
                                         ls_fieldcat-no_zero
                                         ls_fieldcat-no_decimals
                                         ls_fieldcat-foot_note
                                CHANGING ls_col-lp_col.
          ENDIF.
        ENDLOOP.
      ENDLOOP.
    ENDIF.
  ENDIF.

ENDFUNCTION.

*&———————————————————————*
*&      Form  F_ADD_ROW_DATA
*&———————————————————————*
*   Adding the Row.
*———————————————————————-*
FORM f_add_row_data USING    fpv_col_data    TYPE any
                             fpv_curr        TYPE waers
                             fpv_color       TYPE sdydo_attribute
                             fpv_emphasis    TYPE sdydo_attribute
                             fpv_no_zero     TYPE char1
                             fpv_no_decimals TYPE char1
                             fpv_foot_note   TYPE char1
                   CHANGING fpv_col_ref TYPE REF TO cl_dd_area.

* Local varaible declaration
  DATA: lv_txt TYPE sdydo_text_element.
  DATA dref TYPE REF TO data.
  FIELD-SYMBOLS: <fs> TYPE ANY.

  IF fpv_col_ref IS NOT INITIAL.
    IF fpv_curr IS NOT INITIAL.
      WRITE fpv_col_data TO lv_txt CURRENCY fpv_curr.
    ELSE.
      WRITE fpv_col_data TO lv_txt.
    ENDIF.
    IF fpv_no_zero IS NOT INITIAL.
      SHIFT lv_txt LEFT DELETING LEADING ‘0’.
    ENDIF.
    IF fpv_no_decimals IS NOT INITIAL.
      CREATE DATA dref LIKE fpv_col_data.
      ASSIGN dref->* TO <fs>.
      REPLACE ALL OCCURRENCES OF ‘,’ IN lv_txt WITH ”.
      CONDENSE lv_txt.
      IF <fs> IS ASSIGNED.
        <fs> = lv_txt.
        WRITE <fs> TO lv_txt DECIMALS 0.
      ELSE.
        WRITE fpv_col_data TO lv_txt DECIMALS 0.
      ENDIF.
    ENDIF.
    IF fpv_foot_note IS NOT INITIAL.
      CONCATENATE lv_txt ‘*’ INTO lv_txt.
    ENDIF.

    CALL METHOD fpv_col_ref->add_text
      EXPORTING
        text         = lv_txt
        sap_color    = fpv_color
        sap_emphasis = fpv_emphasis.

  ENDIF.

ENDFORM.                    ” F_ADD_ROW_DATA
*&———————————————————————*
*&      Form  F_ADD_COLUMN
*&———————————————————————*
* Adding the column
*———————————————————————-*
FORM f_add_column USING    fps_fieldcat   TYPE zir_s_html_fldcat_dc
                  CHANGING fpp_col        TYPE REF TO cl_dd_area
                           fpp_table      TYPE REF TO cl_dd_table_element
                           fpv_tabix      TYPE i.

  CALL METHOD fpp_table->add_column
    EXPORTING
      width  = fps_fieldcat-width
    IMPORTING
      column = fpp_col.

  CALL METHOD fpp_table->set_column_style
    EXPORTING
      col_no    = fpv_tabix
      sap_align = fps_fieldcat-align.

ENDFORM.                    ” F_ADD_COLUMN
*&———————————————————————*
*&      Form  F_FIELDCAT_INITIAL
*&———————————————————————*
*    If no field catalog is available, build it by ourselves
*———————————————————————-*
FORM f_fieldcat_initial USING fpt_data     TYPE any
                              fpt_fieldcat TYPE zir_tt_html_fldcat_dc.

** Populate Field catalog
  DATA: lt_ddic         TYPE ddfields,
        ls_ddic         TYPE dfies,
        ls_comp         TYPE abap_compdescr.
  DATA lv_ddicflg       TYPE abap_bool.

  DATA:  lp_reftab   TYPE REF TO cl_abap_tabledescr,
         lp_refstruc TYPE REF TO cl_abap_structdescr,
         ls_fieldcat TYPE zir_s_html_fldcat_dc.

  lp_reftab ?= cl_abap_tabledescr=>describe_by_data( fpt_data ).
  IF lp_reftab IS NOT INITIAL.
    lp_refstruc ?= lp_reftab->get_table_line_type( ).
  ENDIF.
  IF lp_refstruc IS NOT INITIAL.
    CLEAR: lv_ddicflg,
           lt_ddic.
** Check if the structure is referenced to a dictionary structure
    CALL METHOD lp_refstruc->is_ddic_type
      RECEIVING
        p_abap_bool = lv_ddicflg.
    IF lv_ddicflg EQ gc_x.
      CALL METHOD lp_refstruc->get_ddic_field_list
        EXPORTING
          p_langu      = sy-langu
        RECEIVING
          p_field_list = lt_ddic
        EXCEPTIONS
          not_found    = 1
          no_ddic_type = 2
          OTHERS       = 3.
      IF sy-subrc EQ 0.
        SORT lt_ddic BY fieldname.
      ENDIF.
    ENDIF.
    LOOP AT lp_refstruc->components INTO ls_comp.
      CLEAR ls_fieldcat.
      ls_fieldcat-fieldname = ls_comp-name.
      IF lv_ddicflg EQ gc_x.
        CLEAR ls_ddic.
        READ TABLE lt_ddic
              INTO ls_ddic
              WITH KEY fieldname = ls_comp-name.
        IF sy-subrc EQ 0.
          ls_fieldcat-coltext = ls_ddic-scrtext_m.
        ENDIF.
      ENDIF.
      IF ls_fieldcat-coltext IS INITIAL.
        ls_fieldcat-coltext = ls_comp-name.
      ENDIF.
      APPEND ls_fieldcat TO fpt_fieldcat.
      CLEAR ls_fieldcat.
    ENDLOOP.
  ENDIF.

ENDFORM.                    ” F_FIELDCAT_INITIAL

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply