06-13-2011 2:58 PM
Hi,
I have a requirement as follows:
In ALV Grid:
I have to display subtotal for a field(Currency) , based on two fields (document and material). When the user executes, he should be able to see the subtotals based on these two fields.
And I have to diaply grand total, based on document number.
Could you please help me out for the same.
Thanks,
Sandeep
06-13-2011 3:50 PM
Hi Sandeep;
You have 2 way for this question.
First way as Rob say. But this way has one problem. if somebody has authority for change alv layout, He/She can change your layout.
Second way You can change/write your code. If This case is better for you. Please apply below code.
Best regards.
FORM ALV_FCAT .
"write DO_SUM property
endform.
FORM ALV_SORT.
CLEAR : SSORT, TSORT.
REFRESH : TSORT.
SSORT-SPOS = '1' .
SSORT-FIELDNAME = 'xxx' .
SSORT-UP = 'X' . "A to Z
APPEND SSORT TO TSORT .
SSORT-SPOS = '2' .
SSORT-FIELDNAME = 'yyy' .
SSORT-UP = 'X' . "A to Z
SSORT-SUBTOT = 'X'.
APPEND SSORT TO TSORT .
ENDFORM.
FORM DISPLAY_ALV .
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY' " REUSE_ALV_GRID_DISPLAY
Exporting
...
IT_FIELDCAT = TFCAT[]
IT_SORT = TSORT[]
...
endform.
06-13-2011 3:05 PM
06-13-2011 3:45 PM
Hi Rob,
Could you please help me, how can I do that..
My display should be like below:
Document Material Amount
1 A 10
1 A 10
1 A 10
Toatl Amount -
30
-
1 B 10
1 B 10
1 B 10
Subto total 30
Total 60
like that to all....other documents....
Thanks,
Sandeep
06-13-2011 3:50 PM
Hi Sandeep;
You have 2 way for this question.
First way as Rob say. But this way has one problem. if somebody has authority for change alv layout, He/She can change your layout.
Second way You can change/write your code. If This case is better for you. Please apply below code.
Best regards.
FORM ALV_FCAT .
"write DO_SUM property
endform.
FORM ALV_SORT.
CLEAR : SSORT, TSORT.
REFRESH : TSORT.
SSORT-SPOS = '1' .
SSORT-FIELDNAME = 'xxx' .
SSORT-UP = 'X' . "A to Z
APPEND SSORT TO TSORT .
SSORT-SPOS = '2' .
SSORT-FIELDNAME = 'yyy' .
SSORT-UP = 'X' . "A to Z
SSORT-SUBTOT = 'X'.
APPEND SSORT TO TSORT .
ENDFORM.
FORM DISPLAY_ALV .
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY' " REUSE_ALV_GRID_DISPLAY
Exporting
...
IT_FIELDCAT = TFCAT[]
IT_SORT = TSORT[]
...
endform.
06-13-2011 3:59 PM
Hi Sandeep,
Rob is correct: The best way is to set the sorts and subtotal flag on the grid and save it as a default layout.
If you want to do it programmatically, you will probably use the only officially released objects, the SALV classes. Then the subtotal coding will look like this:
...
DATA:
lo_sort TYPE REF TO cl_salv_sort,
lo_sorts TYPE REF TO cl_salv_sorts,
...
lo_sorts = mo_salv->get_sorts( ).
...
* subtotal compressed
TRY.
lo_sort = lo_sorts->add_sort( <column_ref>-columnname ).
lo_sort->set_sequence( )." IF_SALV_C_SORT=>SORT_UP
lo_sort->set_subtotal( )." IF_SALV_C_BOOL_SAP=>TRUE
lo_sorts->set_compressed_subtotal( <column_ref>-columnname ).
CATCH cx_root INTO mo_exception.
ENDTRY.
For more advice on SALV you may search the SCN blogs and wikis and the forum.
Regards,
Clemens