Application Development and Automation 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: 
Read only

ALV Subtotal

Former Member
0 Likes
1,242

Hi Experts,

I have to display Subtotal & total in my ALV like this

Item Material

______________________________________

1 100000012

______________________________________

1

______________________________________

1 100000040

1 100000040

______________________________________

2

______________________________________

1 100000050

1 100000050

1 100000050

1 100000050

______________________________________

4

_______________________________________

7(Total)

How can we achive this ? Item field is simply an int field with constant 1. Subtotal of items should be dependent on materials.

I tried below code but no result.

&----


*& Form alv_it_sort_subtotal

&----


  • sort & subtotal

----


FORM alv_it_sort_subtotal .

PERFORM ale_sort

USING 'MATNR' '' 'X'.

ENDFORM. " alv_it_sort_subtotal

&----


*& Form ale_sort

&----


FORM ale_sort USING fieldname

subtotal

up.

CLEAR it_sort_subtotal.

it_sort_subtotal-fieldname = fieldname.

it_sort_subtotal-subtot = subtotal.

it_sort_subtotal-up = up.

APPEND it_sort_subtotal.

ENDFORM. " ale_sort

regards,

Nik

Hi Experts,

I have to display Subtotal & total in my ALV like this

Item Material

______________________________________

1 100000012

______________________________________

1

______________________________________

1 100000040

1 100000040

______________________________________

2

______________________________________

1 100000050

1 100000050

1 100000050

1 100000050

______________________________________

4

_______________________________________

7(Total)

How can we achive this ? Item field is simply an int field with constant 1. Subtotal of items should be dependent on materials.

I tried below code but no result.

&----


*& Form alv_it_sort_subtotal

&----


  • sort & subtotal

----


FORM alv_it_sort_subtotal .

PERFORM ale_sort

USING 'MATNR' '' 'X'.

ENDFORM. " alv_it_sort_subtotal

&----


*& Form ale_sort

&----


FORM ale_sort USING fieldname

subtotal

up.

CLEAR it_sort_subtotal.

it_sort_subtotal-fieldname = fieldname.

it_sort_subtotal-subtot = subtotal.

it_sort_subtotal-up = up.

APPEND it_sort_subtotal.

ENDFORM. " ale_sort

regards,

Nik

10 REPLIES 10
Read only

sreelatha_gullapalli
Active Participant
0 Likes
1,201

hi

check the following link

http://www.sap-img.com/abap/what-is-alv-programming.htm

http://www.sapdevelopment.co.uk/reporting/alvhome.htm

http://www.erpgenie.com/abap/code/abap28.htm

REPORT Z_ALV_SIMPLE_EXAMPLE_WITH_ITAB .

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

*Simple example to use ALV and to define the ALV data in an internal

*table

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

  • Martin Schlegel, BearingPoint, December 2004

*

  • Thanks to Madhusudhan Sonee and Rama Krishna Kommineni for testing

  • and feedback

*

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

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

*For a very long time, people gave me the feeling that ALV is a

*complicated tool that is difficult to understand and to use.

*Lately I had to use it and I discovered that ALV is easy to use and

*saves a lot of work:

*ALV will generate the column headings on its own, so one does not need

*to work on headlines and transalation.

*ALV allows the user to select the columns he wants to see, so the user

*does not need to contact a developer for every change he likes to have.

*ALV allows the user to create his own sums, so …

*ALV has a simple way to work with internal tables.

*If you really want to save time, use ALV instead of write!

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

*

*Please take 30 minutes to explore the following example and see how

*easy it is to use ALV!

*

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

*data definition

tables:

marav. "Table MARA and table MAKT

----


  • Data to be displayed in ALV

  • Using the following syntax, REUSE_ALV_FIELDCATALOG_MERGE can auto-

  • matically determine the fieldstructure from this source program

Data:

begin of imat occurs 100,

matnr like marav-matnr, "Material number

maktx like marav-maktx, "Material short text

matkl like marav-matkl, "Material group (so you can test to make

" intermediate sums)

ntgew like marav-ntgew, "Net weight, numeric field (so you can test to

"make sums)

gewei like marav-gewei, "weight unit (just to be complete)

end of imat.

----


  • Other data needed

  • field to store report name

data i_repid like sy-repid.

  • field to check table length

data i_lines like sy-tabix.

----


  • Data for ALV display

TYPE-POOLS: SLIS.

data int_fcat type SLIS_T_FIELDCAT_ALV.

----


select-options:

s_matnr for marav-matnr matchcode object MAT1.

----


start-of-selection.

  • read data into table imat

select * from marav

into corresponding fields of table imat

where

matnr in s_matnr.

  • Check if material was found

clear i_lines.

describe table imat lines i_lines.

if i_lines lt 1.

  • Using hardcoded write here for easy upload

write: /

'No materials found.'.

exit.

endif.

end-of-selection.

----


*

  • Now, we start with ALV

*

----


*

*

  • To use ALV, we need a DDIC-structure or a thing called Fieldcatalogue.

  • The fieldcatalouge can be generated by FUNCTION

  • 'REUSE_ALV_FIELDCATALOG_MERGE' from an internal table from any

  • report source, including this report.

  • The only problem one might have is that the report and table names

  • need to be in capital letters. (I had it )

*

*

----


  • Store report name

i_repid = sy-repid.

  • Create Fieldcatalogue from internal table

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

I_PROGRAM_NAME = i_repid

I_INTERNAL_TABNAME = 'IMAT' "capital letters!

I_INCLNAME = i_repid

CHANGING

CT_FIELDCAT = int_fcat

EXCEPTIONS

INCONSISTENT_INTERFACE = 1

PROGRAM_ERROR = 2

OTHERS = 3.

*explanations:

  • I_PROGRAM_NAME is the program which calls this function

*

  • I_INTERNAL_TABNAME is the name of the internal table which you want

  • to display in ALV

*

  • I_INCLNAME is the ABAP-source where the internal table is defined

  • (DATA....)

  • CT_FIELDCAT contains the Fieldcatalouge that we need later for

  • ALV display

IF SY-SUBRC <> 0.

write: /

'Returncode',

sy-subrc,

'from FUNCTION REUSE_ALV_FIELDCATALOG_MERGE'.

ENDIF.

*This was the fieldcatlogue

----


*

  • And now, we are ready to display our list

  • Call for ALV list display

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

  • I_CALLBACK_PROGRAM = 'Z_ALV_SIMPLE_EXAMPLE_WITH_ITAB'

I_CALLBACK_PROGRAM = i_repid

IT_FIELDCAT = int_fcat

I_SAVE = 'A'

TABLES

T_OUTTAB = imat

EXCEPTIONS

PROGRAM_ERROR = 1

OTHERS = 2.

*

*explanations:

  • I_CALLBACK_PROGRAM is the program which calls this function

*

  • IT_FIELDCAT (just made by REUSE_ALV_FIELDCATALOG_MERGE) contains

  • now the data definition needed for display

*

  • I_SAVE allows the user to save his own layouts

*

  • T_OUTTAB contains the data to be displayed in ALV

IF SY-SUBRC <> 0.

write: /

'Returncode',

sy-subrc,

'from FUNCTION REUSE_ALV_LIST_DISPLAY'.

ENDIF.

*

----


*

  • yes, it is that simple. Go ahead and try yourself!

*

----


regards

sreelatha gullapalli

Read only

Former Member
0 Likes
1,201

Hi,

DATA : w_fieldcat TYPE slis_fieldcat_alv,

i_fieldcat TYPE slis_t_fieldcat_alv,

gs_layout TYPE slis_layout_alv,

wa_sort1 TYPE slis_t_sortinfo_alv,

wa_sort2 TYPE slis_sortinfo_alv.

wa_sort2-spos = '01'.

wa_sort2-up = 'X'.

wa_sort2-fieldname = 'ITEM'.--> here give field name of item

APPEND wa_sort2 TO wa_sort1.

CLEAR wa_sort2.

wa_sort2-spos = '02'.

wa_sort2-up = 'X'.

wa_sort2-fieldname = 'MATNR'.

APPEND wa_sort2 TO wa_sort1.

CLEAR wa_sort2.

In Field Catelog

-


w_fieldcat-tabname = 'INTERNAL TABLE NAME'.

w_fieldcat-fieldname = 'FIELD NAME'.

w_fieldcat-do_sum = 'X'.

w_fieldcat-outputlen = '10'.

w_fieldcat-col_pos = '1'.

w_fieldcat-row_pos = '1'.

w_fieldcat-seltext_l = c_seltext_l6.

APPEND w_fieldcat TO i_fieldcat.

CLEAR w_fieldcat.

w_fieldcat-tabname = 'INTERNAL TABLE NAME'.

w_fieldcat-fieldname = 'FIELD NAME'.

w_fieldcat-do_sum = 'X'.

w_fieldcat-outputlen = '15'.

w_fieldcat-col_pos = '2'.

w_fieldcat-row_pos = '1'.

w_fieldcat-seltext_l = c_seltext_l7.

APPEND w_fieldcat TO i_fieldcat.

CLEAR w_fieldcat.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = ws_repid

i_callback_pf_status_set = 'GUI_STAT1'

i_callback_user_command = 'STAT'

is_layout = gs_layout

it_fieldcat = i_fieldcat[]

it_sort = wa_sort1

TABLES

t_outtab = i_zaw_pol_plan.

Read only

0 Likes
1,201

Guys read the Question properly.

The subtotal of items should be depends on the material field

The content you guys game i already tried but no result.

regards,

Nik

Read only

0 Likes
1,201

Try this:

CLEAR LS_FIELDCAT.

LS_FIELDCAT-FIELDNAME = 'COUNT'. " say item field

LS_FIELDCAT-TABNAME = 'IT_FINAL'. " final itab

LS_FIELDCAT-DO_SUM = 'X'.

APPEND LS_FIELDCAT TO LT_FIELDCAT. " filed catalog

CLEAR LS_SORT.

LS_SORT-FIELDNAME = 'MATNR'.

LS_SORT-TABNAME = 'IT_FINAL'.

LS_SORT-SUBTOT = 'X'.

APPEND LS_SORT TO GT_SORT.

i think this works out. if any concerns do reply....

Ram

Read only

0 Likes
1,201

Problem Solved.

I forgot to write the it_sort in the AVL GRID Function Module.

Read only

0 Likes
1,201

Problem Solved

Read only

0 Likes
1,201

Thats good. Please close the thread and reward points for helpful answers.

Thanks,

Balaji

Read only

0 Likes
1,201

Hi,

Can you explain me how to get the title for the sub-totals and totals?

Regards,

Raghu

Read only

Former Member
0 Likes
1,201

Try the below code.



TABLES: mara.

SELECT-OPTIONS: s_matnr FOR mara-matnr.

types: begin of ty_marc,
         matnr type marc-matnr,
         werks type marc-werks,
         count type i,
       end of ty_marc.

data: it_marc type standard table of ty_marc with header line.

START-OF-SELECTION.

  SELECT matnr werks FROM marc INTO TABLE it_marc WHERE matnr IN s_matnr and werks in ('0888', '0811').

  loop at it_marc.
    it_marc-count = 1.
    modify it_marc transporting count.
  endloop.

"* Definition for Object Oriented ALV
  DATA: gr_table      TYPE REF TO cl_salv_table.
  DATA: gr_sorts      TYPE REF TO cl_salv_sorts.
  DATA: gr_agg        TYPE REF TO cl_salv_aggregations.
  DATA: gr_agg2       TYPE REF TO cl_salv_aggregation.
  DATA: gr_display    TYPE REF TO cl_salv_display_settings.
  DATA: gr_layout     TYPE REF TO cl_salv_layout.
  DATA: ls_key        TYPE salv_s_layout_key.


"* Display ALV as a Grid
  TRY.
      cl_salv_table=>factory( IMPORTING r_salv_table = gr_table
                              CHANGING  t_table      = it_marc[] ).
    CATCH cx_salv_msg.
  ENDTRY.
    TRY.
        gr_agg = gr_table->get_aggregations( ).
        CALL METHOD gr_agg->add_aggregation
          EXPORTING
            columnname  = 'COUNT'
            aggregation = if_salv_c_aggregation=>total
          RECEIVING
            value       = gr_agg2.
      CATCH cx_salv_data_error .
      CATCH cx_salv_not_found .
      CATCH cx_salv_existing .
    ENDTRY.
"* Set up Sorts
    TRY.
        gr_sorts = gr_table->get_sorts( ).
        CALL METHOD gr_sorts->add_sort
          EXPORTING
            columnname = 'MATNR'
            position   = 1
            sequence   = if_salv_c_sort=>sort_up
            subtotal   = if_salv_c_bool_sap=>true
            group      = if_salv_c_sort=>group_none
            obligatory = if_salv_c_bool_sap=>false.
      CATCH cx_salv_data_error .
      CATCH cx_salv_not_found .
      CATCH cx_salv_existing .
    ENDTRY.

"* Add layout variants in report
    TRY.
      gr_layout = gr_table->get_layout( ).
"*... set the Layout Key
      ls_key-report = sy-repid.
      gr_layout->set_key( ls_key ).
"*... set usage of default Layouts
      gr_layout->set_default( abap_true ).
"*... set Layout save restriction
      gr_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).
    ENDTRY.

    TRY.
"* Display ALV
      gr_table->display( ).
    ENDTRY.

Hope it helps.

Thanks,

Balaji

Read only

Former Member
0 Likes
1,201

solved