Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

template for ALV

Former Member
0 Kudos

anyone have a template for ALV with FM reuse_alv_grid

PLS////

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hope you mean to ask a sample program for ALV GRID report

Here you go.

RANGES: r_fiscal_period     FOR  s600-spmon.

DATA:   gt_fieldcat         TYPE slis_t_fieldcat_alv,
        gt_fcat             TYPE slis_t_fieldcat_alv,
        gs_fieldcat         LIKE LINE OF gt_fieldcat,
        gs_fcat             LIKE LINE OF gt_fcat,
        gt_fldcat           TYPE lvc_t_fcat,
        gs_fldcat           TYPE lvc_s_fcat,
        gt_layout           TYPE slis_layout_alv,
        gt_events           TYPE slis_t_event,
        gt_list_top_of_page TYPE slis_t_listheader,
        gv_repid            LIKE sy-repid,
        gv_beg_period       LIKE /irm/s_ipcisel-etprd,
        gv_line(2)          TYPE c.

DATA:   gv_year(4)          TYPE c,
        gv_month(2)         TYPE c,
        gv_count            TYPE i,
        gv_total            TYPE mc_umnetwr,
        gv_num_lines        TYPE i,
        gv_text1(100)       TYPE c.


DATA:   gt_dyn_table TYPE REF TO data,
        gs_dyn_line  TYPE REF TO data.

FIELD-SYMBOLS: <fs_quota>   TYPE STANDARD TABLE,
               <fs_amount>,
               <fs_quota_wa>.
*----------------------------------------------------------------------*
*                C O N S T A N T S
*----------------------------------------------------------------------*
CONSTANTS: c_600                  TYPE tabname VALUE 'S600',
           c_601                  TYPE tabname VALUE 'S601',
           c_602                  TYPE tabname VALUE 'S602',
           c_603                  TYPE tabname VALUE 'S603',
           c_a00                  TYPE vrsio   VALUE 'A00',
           c_01(2)                TYPE c       VALUE '01',
           c_formname_top_of_page TYPE slis_formname
                                  VALUE 'TOP_OF_PAGE'.

*----------------------------------------------------------------------*
*   I N T E R N A L  T A B L E S     &      W O R K  A R E A S
*----------------------------------------------------------------------*
*structure to hold the quotas
TYPES: BEGIN OF ty_quotas,
         umnetwr   TYPE mc_umnetwr,                  "Net Value
       END OF ty_quotas.

*list of months
DATA: BEGIN OF gs_months,
        field_1      LIKE s600-umnetwr,
        field_2      LIKE s600-umnetwr,
        field_3      LIKE s600-umnetwr,
        field_4      LIKE s600-umnetwr,
        field_5      LIKE s600-umnetwr,
        field_6      LIKE s600-umnetwr,
        field_7      LIKE s600-umnetwr,
        field_8      LIKE s600-umnetwr,
        field_9      LIKE s600-umnetwr,
        field_10     LIKE s600-umnetwr,
        field_11     LIKE s600-umnetwr,
        field_12     LIKE s600-umnetwr,
        total        LIKE s600-umnetwr,
      END OF gs_months.

DATA: gs_quotas TYPE ty_quotas,
      gt_quotas LIKE gs_quotas OCCURS 0 WITH HEADER LINE.
*----------------------------------------------------------------------*
*            S E L E C T I O N - S C R E E N
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE text-013.
PARAMETER: p_lis  TYPE tabname,                "plan input
           p_date LIKE /irm/s_ipcisel-etprd,   "end period
           p_emp  TYPE /irm/ip_ktonr,          "sales participant
           p_terr LIKE ztip_sales-zzterritory, "territory
           p_dis  TYPE zzdistrict,             "district
           p_reg  TYPE zzregion.               "region
SELECTION-SCREEN END OF BLOCK bl1.
*----------------------------------------------------------------------*
*          S T A R T - O F - S E L E C T I O N
*----------------------------------------------------------------------*
START-OF-SELECTION.

*clear all tables, work areas, variables etc
  PERFORM clear_variables.

*get all the months in the period range
  PERFORM get_periods.

*retrieve component quotas
  PERFORM component_quota_amt.

*build field catalogue using internal table
  PERFORM field_catalog_create.

*build dynamic table
  PERFORM build_dynamic_table.

*build layout for the ALV list
  PERFORM build_layout.

*event table to trigger the TOP OF PAGE event to display the heading
  PERFORM eventtab_build USING gt_events[].

*subroutine for displaying the header which contains the number of
*datasets selected or displayed
  PERFORM header_build  USING gt_list_top_of_page[].

*ALV grid display
  PERFORM display_alv_grid.
*&---------------------------------------------------------------------*
*&      Form  clear_variables
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM clear_variables.

  CLEAR:     gs_quotas,
             gv_count,
             gv_beg_period,
             gv_total.

  REFRESH:   gt_quotas.

  MOVE sy-repid TO gv_repid.

ENDFORM.                    " clear_variables
*&---------------------------------------------------------------------*
*&      Form  get_periods
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_periods.

*get the fiscal year
  gv_month = p_date+4(2).
  gv_year  = p_date+0(4).

  MOVE gv_month TO gv_count.

  CONCATENATE gv_year c_01 INTO gv_beg_period.

  r_fiscal_period-low    = gv_beg_period.
  r_fiscal_period-high   = p_date.
  r_fiscal_period-sign   = 'I'.
  r_fiscal_period-option = 'BT'.
  APPEND r_fiscal_period.

ENDFORM.                    " get_periods
*&---------------------------------------------------------------------*
*&      Form  field_catalog_create
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM field_catalog_create.

  DESCRIBE TABLE gt_quotas LINES sy-tabix.

  gv_num_lines = sy-tabix.
  gv_num_lines = gv_num_lines - 1.

  DO gv_num_lines TIMES.
    MOVE sy-index TO gv_line.
    CONCATENATE 'FIELD_' gv_line INTO gs_fldcat-fieldname.
    APPEND gs_fldcat TO gt_fldcat.
  ENDDO.

  MOVE 'TOTAL' TO gs_fldcat-fieldname.
  APPEND gs_fldcat TO gt_fldcat.

*build field catalog and change the field texts of some of the amount
*fields more specific
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
       EXPORTING
            i_program_name         = gv_repid
            i_internal_tabname     = 'GS_MONTHS'
            i_inclname             = gv_repid
            i_bypassing_buffer     = 'X'
       CHANGING
            ct_fieldcat            = gt_fieldcat[]
       EXCEPTIONS
            inconsistent_interface = 1
            program_error          = 2
            OTHERS                 = 3.


  LOOP AT gt_fieldcat INTO gs_fieldcat.
*condition to check for building a dynamic ALV grid
    CHECK sy-tabix LE gv_count.
    CASE gs_fieldcat-fieldname.
      WHEN 'FIELD_1'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-001 TO gs_fcat-seltext_l.
        MOVE text-001 TO gs_fcat-seltext_m.
        MOVE text-001 TO gs_fcat-seltext_s.
        MOVE text-001 TO gs_fcat-reptext_ddic.

      WHEN 'FIELD_2'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-002 TO gs_fcat-seltext_l.
        MOVE text-002 TO gs_fcat-seltext_m.
        MOVE text-002 TO gs_fcat-seltext_s.
        MOVE text-002 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_3'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-003 TO gs_fcat-seltext_l.
        MOVE text-003 TO gs_fcat-seltext_m.
        MOVE text-003 TO gs_fcat-seltext_s.
        MOVE text-003 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_4'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-004 TO gs_fcat-seltext_l.
        MOVE text-004 TO gs_fcat-seltext_m.
        MOVE text-004 TO gs_fcat-seltext_s.
        MOVE text-004 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_5'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-005 TO gs_fcat-seltext_l.
        MOVE text-005 TO gs_fcat-seltext_m.
        MOVE text-005 TO gs_fcat-seltext_s.
        MOVE text-005 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_6'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-006 TO gs_fcat-seltext_l.
        MOVE text-006 TO gs_fcat-seltext_m.
        MOVE text-006 TO gs_fcat-seltext_s.
        MOVE text-006 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_7'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-007 TO gs_fcat-seltext_l.
        MOVE text-007 TO gs_fcat-seltext_m.
        MOVE text-007 TO gs_fcat-seltext_s.
        MOVE text-007 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_8'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-008 TO gs_fcat-seltext_l.
        MOVE text-008 TO gs_fcat-seltext_m.
        MOVE text-008 TO gs_fcat-seltext_s.
        MOVE text-008 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_9'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-009 TO gs_fcat-seltext_l.
        MOVE text-009 TO gs_fcat-seltext_m.
        MOVE text-009 TO gs_fcat-seltext_s.
        MOVE text-009 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_10'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-010 TO gs_fcat-seltext_l.
        MOVE text-010 TO gs_fcat-seltext_m.
        MOVE text-010 TO gs_fcat-seltext_s.
        MOVE text-010 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_11'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-011 TO gs_fcat-seltext_l.
        MOVE text-011 TO gs_fcat-seltext_m.
        MOVE text-011 TO gs_fcat-seltext_s.
        MOVE text-011 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_12'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-012 TO gs_fcat-seltext_l.
        MOVE text-012 TO gs_fcat-seltext_m.
        MOVE text-012 TO gs_fcat-seltext_s.
        MOVE text-012 TO gs_fcat-reptext_ddic.

    ENDCASE.
    APPEND gs_fcat   TO gt_fcat.
  ENDLOOP.


  LOOP AT gt_fieldcat INTO gs_fieldcat.
    CASE gs_fieldcat-fieldname.
      WHEN 'TOTAL'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-015 TO gs_fcat-seltext_l.
        MOVE text-015 TO gs_fcat-seltext_m.
        MOVE text-015 TO gs_fcat-seltext_s.
        MOVE text-015 TO gs_fcat-reptext_ddic.
        APPEND gs_fcat   TO gt_fcat.
    ENDCASE.
  ENDLOOP.

ENDFORM.                    " field_catalog_create
*&---------------------------------------------------------------------*
*&      Form  component_quota_amt
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM component_quota_amt.

*convert the user to a valid format
  PERFORM partid_input_conversion CHANGING p_emp.

  IF p_lis EQ c_600.

*Employee Monthly Quota Targets
    SELECT   umnetwr
      INTO   TABLE gt_quotas
      FROM   s600                      "Employee Monthly Quota Targets
     WHERE   ssour  EQ space
       AND   vrsio  EQ c_a00
       AND   spmon  IN r_fiscal_period
       AND   pvrtnr EQ p_emp.

  ELSEIF p_lis EQ c_601.

*Territory monthly quota targets
    SELECT   umnetwr
      INTO   TABLE gt_quotas
      FROM   s601                      "Territory monthly quota targets
     WHERE   ssour      EQ space
       AND   vrsio      EQ c_a00
       AND   spmon      IN r_fiscal_period
       AND   zzterritor EQ p_terr.

  ELSEIF p_lis EQ c_602.

*District Monthly Quota Targets
    SELECT   umnetwr
      INTO   TABLE gt_quotas
      FROM   s602                      "District  Monthly Quota Targets
     WHERE   ssour      EQ space
       AND   vrsio      EQ c_a00
       AND   spmon      IN r_fiscal_period
       AND   zzdistrict EQ p_dis.

  ELSEIF p_lis EQ c_603.

*Region Monthly Quota Targets
    SELECT   umnetwr
      INTO   TABLE gt_quotas
      FROM   s603                      "Region Monthly Quota Targets
     WHERE   ssour      EQ space
       AND   vrsio      EQ c_a00
       AND   spmon      IN r_fiscal_period
       AND   zzregion   EQ p_reg.

  ENDIF.

**********************************************************************

  LOOP AT gt_quotas INTO gs_quotas.

    gv_total = gv_total + gs_quotas-umnetwr.

  ENDLOOP.

  MOVE gv_total    TO gs_quotas-umnetwr.
  APPEND gs_quotas TO gt_quotas.

**********************************************************************

ENDFORM.                    " component_quota_amt
*&---------------------------------------------------------------------*
*&      Form  build_dynamic_table
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_dynamic_table.

*Create dynamic internal table
  CALL METHOD cl_alv_table_create=>create_dynamic_table
               EXPORTING
                  it_fieldcatalog = gt_fldcat
               IMPORTING
                  ep_table        = gt_dyn_table.

  ASSIGN gt_dyn_table->* TO <fs_quota>.

  CREATE DATA gs_dyn_line LIKE LINE OF <fs_quota>.
  ASSIGN gs_dyn_line->* TO <fs_quota_wa>.

  LOOP AT gt_quotas INTO gs_quotas.
    ASSIGN COMPONENT sy-tabix OF STRUCTURE <fs_quota_wa> TO <fs_amount>.
    <fs_amount> = gs_quotas-umnetwr.
  ENDLOOP.

  IF sy-subrc = 0.
    APPEND <fs_quota_wa> TO <fs_quota>.
  ENDIF.

ENDFORM.                    " build_dynamic_table
*&---------------------------------------------------------------------*
*&      Form  display_alv_grid
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_alv_grid.

  IF NOT <fs_quota> IS INITIAL.

* ALV grid display
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
         EXPORTING
              i_callback_program = gv_repid
              is_layout          = gt_layout
              it_fieldcat        = gt_fcat
*              it_sort            = gt_sortcat
              i_save             = 'A'
              it_events          = gt_events
         TABLES
              t_outtab           = <fs_quota>
         EXCEPTIONS
              program_error      = 1
              OTHERS             = 2.

    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
  ELSE.
    MESSAGE i013(zip).
  ENDIF.

ENDFORM.                    " display_alv_grid
*&---------------------------------------------------------------------*
*&      Form  build_layout
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_layout.

  gt_layout-zebra             = 'X'.
  gt_layout-no_vline          = ' '.
  gt_layout-reprep            = 'X'.
  gt_layout-detail_popup      = 'X'.
  gt_layout-window_titlebar   = text-014.
  gt_layout-no_scrolling      = ' '.
  gt_layout-detail_titlebar   = text-014.
  gt_layout-numc_sum          = 'X'.
  gt_layout-colwidth_optimize = 'X'.

ENDFORM.                    " build_layout
*&---------------------------------------------------------------------*
*&      Form  partid_input_conversion
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_P_EMP  text
*----------------------------------------------------------------------*
FORM partid_input_conversion CHANGING p_emp.

  DATA: lv_pernr  TYPE pernr_d.

  lv_pernr  = p_emp.
  p_emp     = lv_pernr.

ENDFORM.                    " partid_input_conversion
*&---------------------------------------------------------------------*
*&      Form  eventtab_build
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_EVENTS[]  text
*----------------------------------------------------------------------*
FORM eventtab_build USING p_lt_events TYPE slis_t_event.

  DATA: ls_event TYPE slis_alv_event.

*top of page event is read here to display the heading
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
       EXPORTING
            i_list_type = 0
       IMPORTING
            et_events   = p_lt_events.

  READ TABLE p_lt_events WITH KEY name =  slis_ev_top_of_page
                         INTO ls_event.
  IF sy-subrc = 0.
    MOVE c_formname_top_of_page TO ls_event-form.
    MODIFY p_lt_events FROM  ls_event TRANSPORTING form
                       WHERE name =  slis_ev_top_of_page.
  ENDIF.

ENDFORM.                    " eventtab_build
*&---------------------------------------------------------------------*
*&      FORM  TOP-OF-PAGE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM top_of_page.

  IF NOT gt_quotas[] IS INITIAL.
* function module which displayes the number of records selected in the
*  header part
    CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
         EXPORTING
              it_list_commentary = gt_list_top_of_page.
  ENDIF.

ENDFORM.                    " TOP-OF-PAGE
*&---------------------------------------------------------------------*
*&      Form  header_build
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_LIST_TOP_OF_PAGE[]  text
*----------------------------------------------------------------------*
FORM header_build USING p_lt_list_top_of_page TYPE slis_t_listheader.

  DATA: ls_line       TYPE slis_listheader,
        lv_amount(20) TYPE c.

  MOVE gv_total TO lv_amount.
  CONCATENATE text-016 lv_amount INTO gv_text1.
  CONCATENATE gv_text1 'USD' INTO gv_text1 SEPARATED BY space.
  CLEAR ls_line.
  ls_line-typ  = 'H'.
  ls_line-info = gv_text1.
  APPEND ls_line TO p_lt_list_top_of_page.

ENDFORM.                    " header_build

Thanks,

6 REPLIES 6

Former Member
0 Kudos

Hope you mean to ask a sample program for ALV GRID report

Here you go.

RANGES: r_fiscal_period     FOR  s600-spmon.

DATA:   gt_fieldcat         TYPE slis_t_fieldcat_alv,
        gt_fcat             TYPE slis_t_fieldcat_alv,
        gs_fieldcat         LIKE LINE OF gt_fieldcat,
        gs_fcat             LIKE LINE OF gt_fcat,
        gt_fldcat           TYPE lvc_t_fcat,
        gs_fldcat           TYPE lvc_s_fcat,
        gt_layout           TYPE slis_layout_alv,
        gt_events           TYPE slis_t_event,
        gt_list_top_of_page TYPE slis_t_listheader,
        gv_repid            LIKE sy-repid,
        gv_beg_period       LIKE /irm/s_ipcisel-etprd,
        gv_line(2)          TYPE c.

DATA:   gv_year(4)          TYPE c,
        gv_month(2)         TYPE c,
        gv_count            TYPE i,
        gv_total            TYPE mc_umnetwr,
        gv_num_lines        TYPE i,
        gv_text1(100)       TYPE c.


DATA:   gt_dyn_table TYPE REF TO data,
        gs_dyn_line  TYPE REF TO data.

FIELD-SYMBOLS: <fs_quota>   TYPE STANDARD TABLE,
               <fs_amount>,
               <fs_quota_wa>.
*----------------------------------------------------------------------*
*                C O N S T A N T S
*----------------------------------------------------------------------*
CONSTANTS: c_600                  TYPE tabname VALUE 'S600',
           c_601                  TYPE tabname VALUE 'S601',
           c_602                  TYPE tabname VALUE 'S602',
           c_603                  TYPE tabname VALUE 'S603',
           c_a00                  TYPE vrsio   VALUE 'A00',
           c_01(2)                TYPE c       VALUE '01',
           c_formname_top_of_page TYPE slis_formname
                                  VALUE 'TOP_OF_PAGE'.

*----------------------------------------------------------------------*
*   I N T E R N A L  T A B L E S     &      W O R K  A R E A S
*----------------------------------------------------------------------*
*structure to hold the quotas
TYPES: BEGIN OF ty_quotas,
         umnetwr   TYPE mc_umnetwr,                  "Net Value
       END OF ty_quotas.

*list of months
DATA: BEGIN OF gs_months,
        field_1      LIKE s600-umnetwr,
        field_2      LIKE s600-umnetwr,
        field_3      LIKE s600-umnetwr,
        field_4      LIKE s600-umnetwr,
        field_5      LIKE s600-umnetwr,
        field_6      LIKE s600-umnetwr,
        field_7      LIKE s600-umnetwr,
        field_8      LIKE s600-umnetwr,
        field_9      LIKE s600-umnetwr,
        field_10     LIKE s600-umnetwr,
        field_11     LIKE s600-umnetwr,
        field_12     LIKE s600-umnetwr,
        total        LIKE s600-umnetwr,
      END OF gs_months.

DATA: gs_quotas TYPE ty_quotas,
      gt_quotas LIKE gs_quotas OCCURS 0 WITH HEADER LINE.
*----------------------------------------------------------------------*
*            S E L E C T I O N - S C R E E N
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK bl1 WITH FRAME TITLE text-013.
PARAMETER: p_lis  TYPE tabname,                "plan input
           p_date LIKE /irm/s_ipcisel-etprd,   "end period
           p_emp  TYPE /irm/ip_ktonr,          "sales participant
           p_terr LIKE ztip_sales-zzterritory, "territory
           p_dis  TYPE zzdistrict,             "district
           p_reg  TYPE zzregion.               "region
SELECTION-SCREEN END OF BLOCK bl1.
*----------------------------------------------------------------------*
*          S T A R T - O F - S E L E C T I O N
*----------------------------------------------------------------------*
START-OF-SELECTION.

*clear all tables, work areas, variables etc
  PERFORM clear_variables.

*get all the months in the period range
  PERFORM get_periods.

*retrieve component quotas
  PERFORM component_quota_amt.

*build field catalogue using internal table
  PERFORM field_catalog_create.

*build dynamic table
  PERFORM build_dynamic_table.

*build layout for the ALV list
  PERFORM build_layout.

*event table to trigger the TOP OF PAGE event to display the heading
  PERFORM eventtab_build USING gt_events[].

*subroutine for displaying the header which contains the number of
*datasets selected or displayed
  PERFORM header_build  USING gt_list_top_of_page[].

*ALV grid display
  PERFORM display_alv_grid.
*&---------------------------------------------------------------------*
*&      Form  clear_variables
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM clear_variables.

  CLEAR:     gs_quotas,
             gv_count,
             gv_beg_period,
             gv_total.

  REFRESH:   gt_quotas.

  MOVE sy-repid TO gv_repid.

ENDFORM.                    " clear_variables
*&---------------------------------------------------------------------*
*&      Form  get_periods
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_periods.

*get the fiscal year
  gv_month = p_date+4(2).
  gv_year  = p_date+0(4).

  MOVE gv_month TO gv_count.

  CONCATENATE gv_year c_01 INTO gv_beg_period.

  r_fiscal_period-low    = gv_beg_period.
  r_fiscal_period-high   = p_date.
  r_fiscal_period-sign   = 'I'.
  r_fiscal_period-option = 'BT'.
  APPEND r_fiscal_period.

ENDFORM.                    " get_periods
*&---------------------------------------------------------------------*
*&      Form  field_catalog_create
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM field_catalog_create.

  DESCRIBE TABLE gt_quotas LINES sy-tabix.

  gv_num_lines = sy-tabix.
  gv_num_lines = gv_num_lines - 1.

  DO gv_num_lines TIMES.
    MOVE sy-index TO gv_line.
    CONCATENATE 'FIELD_' gv_line INTO gs_fldcat-fieldname.
    APPEND gs_fldcat TO gt_fldcat.
  ENDDO.

  MOVE 'TOTAL' TO gs_fldcat-fieldname.
  APPEND gs_fldcat TO gt_fldcat.

*build field catalog and change the field texts of some of the amount
*fields more specific
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
       EXPORTING
            i_program_name         = gv_repid
            i_internal_tabname     = 'GS_MONTHS'
            i_inclname             = gv_repid
            i_bypassing_buffer     = 'X'
       CHANGING
            ct_fieldcat            = gt_fieldcat[]
       EXCEPTIONS
            inconsistent_interface = 1
            program_error          = 2
            OTHERS                 = 3.


  LOOP AT gt_fieldcat INTO gs_fieldcat.
*condition to check for building a dynamic ALV grid
    CHECK sy-tabix LE gv_count.
    CASE gs_fieldcat-fieldname.
      WHEN 'FIELD_1'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-001 TO gs_fcat-seltext_l.
        MOVE text-001 TO gs_fcat-seltext_m.
        MOVE text-001 TO gs_fcat-seltext_s.
        MOVE text-001 TO gs_fcat-reptext_ddic.

      WHEN 'FIELD_2'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-002 TO gs_fcat-seltext_l.
        MOVE text-002 TO gs_fcat-seltext_m.
        MOVE text-002 TO gs_fcat-seltext_s.
        MOVE text-002 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_3'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-003 TO gs_fcat-seltext_l.
        MOVE text-003 TO gs_fcat-seltext_m.
        MOVE text-003 TO gs_fcat-seltext_s.
        MOVE text-003 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_4'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-004 TO gs_fcat-seltext_l.
        MOVE text-004 TO gs_fcat-seltext_m.
        MOVE text-004 TO gs_fcat-seltext_s.
        MOVE text-004 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_5'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-005 TO gs_fcat-seltext_l.
        MOVE text-005 TO gs_fcat-seltext_m.
        MOVE text-005 TO gs_fcat-seltext_s.
        MOVE text-005 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_6'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-006 TO gs_fcat-seltext_l.
        MOVE text-006 TO gs_fcat-seltext_m.
        MOVE text-006 TO gs_fcat-seltext_s.
        MOVE text-006 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_7'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-007 TO gs_fcat-seltext_l.
        MOVE text-007 TO gs_fcat-seltext_m.
        MOVE text-007 TO gs_fcat-seltext_s.
        MOVE text-007 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_8'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-008 TO gs_fcat-seltext_l.
        MOVE text-008 TO gs_fcat-seltext_m.
        MOVE text-008 TO gs_fcat-seltext_s.
        MOVE text-008 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_9'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-009 TO gs_fcat-seltext_l.
        MOVE text-009 TO gs_fcat-seltext_m.
        MOVE text-009 TO gs_fcat-seltext_s.
        MOVE text-009 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_10'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-010 TO gs_fcat-seltext_l.
        MOVE text-010 TO gs_fcat-seltext_m.
        MOVE text-010 TO gs_fcat-seltext_s.
        MOVE text-010 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_11'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-011 TO gs_fcat-seltext_l.
        MOVE text-011 TO gs_fcat-seltext_m.
        MOVE text-011 TO gs_fcat-seltext_s.
        MOVE text-011 TO gs_fcat-reptext_ddic.


      WHEN 'FIELD_12'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-012 TO gs_fcat-seltext_l.
        MOVE text-012 TO gs_fcat-seltext_m.
        MOVE text-012 TO gs_fcat-seltext_s.
        MOVE text-012 TO gs_fcat-reptext_ddic.

    ENDCASE.
    APPEND gs_fcat   TO gt_fcat.
  ENDLOOP.


  LOOP AT gt_fieldcat INTO gs_fieldcat.
    CASE gs_fieldcat-fieldname.
      WHEN 'TOTAL'.
        MOVE-CORRESPONDING gs_fieldcat TO gs_fcat.
        MOVE text-015 TO gs_fcat-seltext_l.
        MOVE text-015 TO gs_fcat-seltext_m.
        MOVE text-015 TO gs_fcat-seltext_s.
        MOVE text-015 TO gs_fcat-reptext_ddic.
        APPEND gs_fcat   TO gt_fcat.
    ENDCASE.
  ENDLOOP.

ENDFORM.                    " field_catalog_create
*&---------------------------------------------------------------------*
*&      Form  component_quota_amt
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM component_quota_amt.

*convert the user to a valid format
  PERFORM partid_input_conversion CHANGING p_emp.

  IF p_lis EQ c_600.

*Employee Monthly Quota Targets
    SELECT   umnetwr
      INTO   TABLE gt_quotas
      FROM   s600                      "Employee Monthly Quota Targets
     WHERE   ssour  EQ space
       AND   vrsio  EQ c_a00
       AND   spmon  IN r_fiscal_period
       AND   pvrtnr EQ p_emp.

  ELSEIF p_lis EQ c_601.

*Territory monthly quota targets
    SELECT   umnetwr
      INTO   TABLE gt_quotas
      FROM   s601                      "Territory monthly quota targets
     WHERE   ssour      EQ space
       AND   vrsio      EQ c_a00
       AND   spmon      IN r_fiscal_period
       AND   zzterritor EQ p_terr.

  ELSEIF p_lis EQ c_602.

*District Monthly Quota Targets
    SELECT   umnetwr
      INTO   TABLE gt_quotas
      FROM   s602                      "District  Monthly Quota Targets
     WHERE   ssour      EQ space
       AND   vrsio      EQ c_a00
       AND   spmon      IN r_fiscal_period
       AND   zzdistrict EQ p_dis.

  ELSEIF p_lis EQ c_603.

*Region Monthly Quota Targets
    SELECT   umnetwr
      INTO   TABLE gt_quotas
      FROM   s603                      "Region Monthly Quota Targets
     WHERE   ssour      EQ space
       AND   vrsio      EQ c_a00
       AND   spmon      IN r_fiscal_period
       AND   zzregion   EQ p_reg.

  ENDIF.

**********************************************************************

  LOOP AT gt_quotas INTO gs_quotas.

    gv_total = gv_total + gs_quotas-umnetwr.

  ENDLOOP.

  MOVE gv_total    TO gs_quotas-umnetwr.
  APPEND gs_quotas TO gt_quotas.

**********************************************************************

ENDFORM.                    " component_quota_amt
*&---------------------------------------------------------------------*
*&      Form  build_dynamic_table
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_dynamic_table.

*Create dynamic internal table
  CALL METHOD cl_alv_table_create=>create_dynamic_table
               EXPORTING
                  it_fieldcatalog = gt_fldcat
               IMPORTING
                  ep_table        = gt_dyn_table.

  ASSIGN gt_dyn_table->* TO <fs_quota>.

  CREATE DATA gs_dyn_line LIKE LINE OF <fs_quota>.
  ASSIGN gs_dyn_line->* TO <fs_quota_wa>.

  LOOP AT gt_quotas INTO gs_quotas.
    ASSIGN COMPONENT sy-tabix OF STRUCTURE <fs_quota_wa> TO <fs_amount>.
    <fs_amount> = gs_quotas-umnetwr.
  ENDLOOP.

  IF sy-subrc = 0.
    APPEND <fs_quota_wa> TO <fs_quota>.
  ENDIF.

ENDFORM.                    " build_dynamic_table
*&---------------------------------------------------------------------*
*&      Form  display_alv_grid
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_alv_grid.

  IF NOT <fs_quota> IS INITIAL.

* ALV grid display
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
         EXPORTING
              i_callback_program = gv_repid
              is_layout          = gt_layout
              it_fieldcat        = gt_fcat
*              it_sort            = gt_sortcat
              i_save             = 'A'
              it_events          = gt_events
         TABLES
              t_outtab           = <fs_quota>
         EXCEPTIONS
              program_error      = 1
              OTHERS             = 2.

    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
  ELSE.
    MESSAGE i013(zip).
  ENDIF.

ENDFORM.                    " display_alv_grid
*&---------------------------------------------------------------------*
*&      Form  build_layout
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_layout.

  gt_layout-zebra             = 'X'.
  gt_layout-no_vline          = ' '.
  gt_layout-reprep            = 'X'.
  gt_layout-detail_popup      = 'X'.
  gt_layout-window_titlebar   = text-014.
  gt_layout-no_scrolling      = ' '.
  gt_layout-detail_titlebar   = text-014.
  gt_layout-numc_sum          = 'X'.
  gt_layout-colwidth_optimize = 'X'.

ENDFORM.                    " build_layout
*&---------------------------------------------------------------------*
*&      Form  partid_input_conversion
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_P_EMP  text
*----------------------------------------------------------------------*
FORM partid_input_conversion CHANGING p_emp.

  DATA: lv_pernr  TYPE pernr_d.

  lv_pernr  = p_emp.
  p_emp     = lv_pernr.

ENDFORM.                    " partid_input_conversion
*&---------------------------------------------------------------------*
*&      Form  eventtab_build
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_EVENTS[]  text
*----------------------------------------------------------------------*
FORM eventtab_build USING p_lt_events TYPE slis_t_event.

  DATA: ls_event TYPE slis_alv_event.

*top of page event is read here to display the heading
  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
       EXPORTING
            i_list_type = 0
       IMPORTING
            et_events   = p_lt_events.

  READ TABLE p_lt_events WITH KEY name =  slis_ev_top_of_page
                         INTO ls_event.
  IF sy-subrc = 0.
    MOVE c_formname_top_of_page TO ls_event-form.
    MODIFY p_lt_events FROM  ls_event TRANSPORTING form
                       WHERE name =  slis_ev_top_of_page.
  ENDIF.

ENDFORM.                    " eventtab_build
*&---------------------------------------------------------------------*
*&      FORM  TOP-OF-PAGE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM top_of_page.

  IF NOT gt_quotas[] IS INITIAL.
* function module which displayes the number of records selected in the
*  header part
    CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
         EXPORTING
              it_list_commentary = gt_list_top_of_page.
  ENDIF.

ENDFORM.                    " TOP-OF-PAGE
*&---------------------------------------------------------------------*
*&      Form  header_build
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_LIST_TOP_OF_PAGE[]  text
*----------------------------------------------------------------------*
FORM header_build USING p_lt_list_top_of_page TYPE slis_t_listheader.

  DATA: ls_line       TYPE slis_listheader,
        lv_amount(20) TYPE c.

  MOVE gv_total TO lv_amount.
  CONCATENATE text-016 lv_amount INTO gv_text1.
  CONCATENATE gv_text1 'USD' INTO gv_text1 SEPARATED BY space.
  CLEAR ls_line.
  ls_line-typ  = 'H'.
  ls_line-info = gv_text1.
  APPEND ls_line TO p_lt_list_top_of_page.

ENDFORM.                    " header_build

Thanks,

former_member188685
Active Contributor
0 Kudos

Hi,

Check this...

REPORT  Z_TEST_ALV_GRID     message-id zz   .

type-pools: slis.
data: x_fieldcat type slis_fieldcat_Alv,
      it_fieldcat type slis_t_fieldcat_alv.

data: v_lines type i.
data: begin of itab occurs 0,
       vbeln like vbak-vbeln,
       posnr like vbap-posnr,
      end of itab.

select vbeln
       posnr
       from vbap
       up to 100 rows
       into table itab.
call function 'REUSE_ALV_FIELDCATALOG_MERGE'
 EXPORTING
   I_PROGRAM_NAME               = sy-repid
   I_INTERNAL_TABNAME           = 'ITAB'
   I_INCLNAME                   = sy-repid
  changing
    ct_fieldcat                  = IT_FIELDCAT
 EXCEPTIONS
   INCONSISTENT_INTERFACE       = 1
   PROGRAM_ERROR                = 2
   OTHERS                       = 3
          .
if sy-subrc <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
endif.

 call function 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
    I_CALLBACK_PROGRAM                = sy-repid
    IT_FIELDCAT                       = IT_FIELDCAT
    tables
     t_outtab                         = ITAB[]
  EXCEPTIONS
    PROGRAM_ERROR                     = 1
    OTHERS                            = 2
           .
 if sy-subrc <> 0.
 MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
 endif.

Regards

Vijay

0 Kudos

vijay your program not work???

0 Kudos

It is working for me. are you facing any problem with the code.

Regards

vijay

Former Member
0 Kudos

A search on the forum should help you. An example:

Hope this helps.

Sudha