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: 

How to do dynamic subtotal and total texts in ALV list...

aris_hidalgo
Contributor
0 Kudos

Hello Experts,

I am currently using the old ALV version to display my report. Currently, I need

to write the subtotal and total texts but depending on the a given value in one of the

fields in my internal table. So for example, that field is for plant and the value is 1000

so I need to write 'Subtotal for Plant 1000:'. My initial code below is not working:

CLEAR gs_layout.

gs_layout-colwidth_optimize = 'X'.

gs_layout-zebra = 'X'.

gs_layout-subtotals_text = 'Subtotal'.

gs_layout-totals_text = 'Total'.

Hope you can help me guys. Thank you and take care!

1 ACCEPTED SOLUTION

former_member188685
Active Contributor
0 Kudos

It will not work. Just check this code...

Don't cross post.

3 REPLIES 3

former_member188685
Active Contributor
0 Kudos

It will not work. Just check this code...

Don't cross post.

0 Kudos

Hi Vijay,

I tried following your code and the subtotal and totals text still does now show. Below is my code:


REFRESH lt_itab_name.
    DEFINE m_fill_itab_name.
      wa_itab_name-itab_name  = &1.
      wa_itab_name-itab_name2 = &2.
      append wa_itab_name to lt_itab_name.
      clear wa_itab_name.
    END-OF-DEFINITION.

    m_fill_itab_name 'ZSD_OUTPUT_NET' 'LT_OUTPUT_FIN'.

    lt_output_fin[] = lcl_process_data=>gt_output_fin[].
    SORT lt_output_fin BY counter tab_counter.

    LOOP AT lt_itab_name INTO wa_itab_name.
      REFRESH gt_fieldcat.
*     Build fieldcatalog
      me->build_fieldcat( ).
    ENDLOOP.

    CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
      EXPORTING
*       I_INTERFACE_CHECK                 = ' '
*       I_BYPASSING_BUFFER                = ' '
*       I_BUFFER_ACTIVE                   = ' '
        i_callback_program                = sy-repid
*       I_CALLBACK_PF_STATUS_SET          = ' '
*       I_CALLBACK_USER_COMMAND           = ' '
*       I_CALLBACK_TOP_OF_PAGE            = ' '
*       I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*       I_CALLBACK_HTML_END_OF_LIST       = ' '
*       I_STRUCTURE_NAME                  =
*       I_BACKGROUND_ID                   = ' '
*       I_GRID_TITLE                      =
*       I_GRID_SETTINGS                   =
        is_layout                         = gs_layout
        it_fieldcat                       = gt_fieldcat
*       IT_EXCLUDING                      =
*       IT_SPECIAL_GROUPS                 =
        it_sort                           = gt_sort
*       IT_FILTER                         =
*       IS_SEL_HIDE                       =
*       I_DEFAULT                         = 'X'
*       I_SAVE                            = ' '
*       IS_VARIANT                        =
        it_events                         = gt_events
*       IT_EVENT_EXIT                     =
        is_print                          = gs_print
*       IS_REPREP_ID                      =
*       I_SCREEN_START_COLUMN             = 0
*       I_SCREEN_START_LINE               = 0
*       I_SCREEN_END_COLUMN               = 0
*       I_SCREEN_END_LINE                 = 0
*       I_HTML_HEIGHT_TOP                 = 0
*       I_HTML_HEIGHT_END                 = 0
*       IT_ALV_GRAPHICS                   =
*       IT_HYPERLINK                      =
*       IT_ADD_FIELDCAT                   =
*       IT_EXCEPT_QINFO                   =
*       IR_SALV_FULLSCREEN_ADAPTER        =
*     IMPORTING
*       E_EXIT_CAUSED_BY_CALLER           =
*       ES_EXIT_CAUSED_BY_USER            =
      TABLES
        t_outtab                          = lt_output_fin
     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.

* METHOD build_fieldcat
  METHOD build_fieldcat.
    me->set_layout( ).
    me->set_events( ).
    me->set_sort_criteria(
          EXPORTING
            im_tabname = wa_itab_name-itab_name2 ).

    REFRESH gt_fieldcat.
    CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
     EXPORTING
       i_program_name               = sy-repid
       i_internal_tabname           = 'LT_OUTPUT_FIN'
       i_structure_name             = 'ZSD_OUTPUT_NET'
*       I_CLIENT_NEVER_DISPLAY       = 'X'
*       I_INCLNAME                   =
*       I_BYPASSING_BUFFER           =
*       I_BUFFER_ACTIVE              =
      CHANGING
        ct_fieldcat                  = gt_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.

    IF gt_fieldcat[] IS NOT INITIAL.
      me->prepare_components( ).
    ENDIF.
  ENDMETHOD.                    "build_fieldcat

* METHOD set_layout
  METHOD set_layout.
    CLEAR gs_layout.
    gs_layout-colwidth_optimize = 'X'.
    gs_layout-zebra             = 'X'.
    gs_layout-subtotals_text    = 'Subtotal'.
    gs_layout-totals_text       = 'Total'.
  ENDMETHOD.                    "set_layout

* METHOD set_print_criteria
  METHOD set_print_criteria.
    CLEAR gs_print.
    gs_print-no_print_listinfos = 'X'.
    gs_print-no_print_selinfos  = 'X'.
  ENDMETHOD.                    "set_print_criteria

* METHOD set_events
  METHOD set_events.
    CLEAR wa_events.
    REFRESH gt_events.

    wa_events-name = 'TOP_OF_PAGE'.
    wa_events-form = 'HANDLE_EVENT_TOP_OF_PAGE_1'.
    APPEND wa_events TO gt_events.

    wa_events-name = 'USER_COMMAND'.
    wa_events-form = 'HANDLE_EVENT_USER_COMMAND'.
    APPEND wa_events TO gt_events.

    wa_events-name = 'SUBTOTAL_TEXT'.
    wa_events-form = 'SUBTOTAL_TEXT'.
    APPEND wa_events TO gt_events.
  ENDMETHOD.                    "set_events

* METHOD set_sort_criteria
  METHOD set_sort_criteria.
    CLEAR wa_sort.
    REFRESH gt_sort.

    CASE 'X'.
      WHEN rb_anlkl.
        wa_sort-fieldname = 'ANLKL'.
        wa_sort-up        = 'X'.
*        wa_sort-group     = '*'.
*        wa_sort-subtot    = 'X'.
        wa_sort-tabname   = im_tabname.
        APPEND wa_sort TO gt_sort.
        CLEAR wa_sort.
      WHEN rb_kostl.
        wa_sort-fieldname = 'KOSTL'.
        wa_sort-up        = 'X'.
*        wa_sort-group     = '*'.
*        wa_sort-subtot    = 'X'.
        wa_sort-tabname   = im_tabname.
        APPEND wa_sort TO gt_sort.
        CLEAR wa_sort.
    ENDCASE.

    wa_sort-fieldname = 'COUNTER'.
    wa_sort-up        = 'X'.
    wa_sort-group     = '*'.
    wa_sort-subtot    = 'X'.
    wa_sort-tabname   = im_tabname.
    APPEND wa_sort TO gt_sort.
    CLEAR wa_sort.

    wa_sort-fieldname = 'TAB_COUNTER'.
    wa_sort-up        = 'X'.
    wa_sort-group     = '*'.
    wa_sort-subtot    = 'X'.
    wa_sort-tabname   = im_tabname.
    APPEND wa_sort TO gt_sort.
    CLEAR wa_sort.
  ENDMETHOD.                    "set_sort_criteria

*&---------------------------------------------------------------------*
*&      Form  subtotal_text
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->IM_OUTPUT_FIN     text
*      -->IM_SUBTOTAL_TEXT  text
*----------------------------------------------------------------------*
FORM subtotal_text USING im_output_fin STRUCTURE zsd_output_net
                         im_subtotal_text TYPE slis_subtot_text.

  IF im_subtotal_text-criteria = 'TAB_COUNTER'.
    im_subtotal_text-display_text_for_subtotal = 'Subtotal'.
  ENDIF.
ENDFORM.                    "subtotal_text

0 Kudos

you just have one Dummy Field, if you clearly observe the code .That Dummy field required for Triggering the SUBTOTAL_TEXT event. and see the sort option population also carefully....