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: 

ALV - Sort

Former Member
0 Kudos

HI All,

I want to do Total sum and Sub total ZRATE(CURR) with respect to ZCLIENT(Char10).

Can you please what is wrong in my code.

Correct me if i am wrong.

Even DOSUM is also not working....

DEFINE m_fieldcat.

add 1 to ls_fieldcat-col_pos.

ls_fieldcat-fieldname = &1.

ls_fieldcat-tabname = 'IT_PIPE'.

ls_fieldcat-seltext_l = &2.

ls_fieldcat-do_sum = &3.

append ls_fieldcat to lt_fieldcat.

END-OF-DEFINITION.

m_fieldcat 'ZCLIENT' 'Client' ' '.

m_fieldcat 'ZRATE' 'Rate' 'X'.

DEFINE m_sort.

add 1 to ls_sort-spos.

ls_sort-fieldname = &1.

ls_sort-up = 'X'.

ls_sort-subtot = &2.

append ls_sort to lt_sort.

END-OF-DEFINITION.

m_sort 'ZCLIENT ' 'X' .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = sy-repid

it_fieldcat = lt_fieldcat

it_sort = lt_sort

TABLES

t_outtab = it_pipe.

Thanks,

Ashok...

5 REPLIES 5

Former Member
0 Kudos

hi,

can you try like this.

DEFINE m_fieldcat.

add 1 to ls_fieldcat-col_pos.

ls_fieldcat-fieldname = &1.

ls_fieldcat-tabname = 'IT_PIPE'.

ls_fieldcat-seltext_l = &2.

ls_fieldcat-do_sum = &3.

append ls_fieldcat to lt_fieldcat.

END-OF-DEFINITION.

m_fieldcat 'ZCLIENT' 'Client' ' '.

m_fieldcat 'ZRATE' 'Rate' 'X'.

DEFINE m_sort.

add 1 to ls_sort-spos.

ls_sort-fieldname = &1.

ls_sort-up = 'X'.

ls_sort-subtot = &2.

append ls_sort to lt_sort.

END-OF-DEFINITION.

m_sort 'ZCLIENT ' ' ' .

m_sort 'ZRATE' 'X'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = sy-repid

it_fieldcat = lt_fieldcat

it_sort = lt_sort

TABLES

t_outtab = it_pipe.

Former Member
0 Kudos

Hi Ashok,

The macro is fine. check the code below.

SORT-SPOS = &1.

SORT-FIELDNAME = &2.

SORT-TABNAME = 'TT_ITAB'.

SORT-UP = 'X'.

APPEND SORT.

CLEAR SORT.

END-OF-DEFINITION.

MACROS1 '1' 'EBELN'.

MACROS1 '2' 'AEDAT'.

MACROS1 '3' 'LIFNR'.

MACROS1 '4' 'EKGRP'.

This code is to sort 4 fields.

cheers,

Hema.

Former Member
0 Kudos

Hi Ashok , I am giving u a similar sorting problem and solution.pls go through it so that u will get some idea.

Q: I need to sort the alv after displaying the output.The requirement is the user will select any column in the output and when he presses the sort(Custom Button) Button the entire output has to be displayed in the sort order of that column.Here I am using OOPs Concept.

Sol: problem solved using the methods set_sort_criteria and get_selected_columns.

Please find the related code.

any way thank you for all u r replys.

METHOD handle_user_command.

  • define local data

data:i_col type standard table of LVC_S_COL,

wa_col type LVC_S_COL,

wa_sort TYPE LVC_S_SORT.

CASE e_ucomm.

WHEN 'SORT_COL'.

REFRESH: mt_sort.

CALL METHOD sender->get_selected_columns

IMPORTING

ET_INDEX_COLUMNS = i_COL.

loop at i_col into wa_col .

wa_sort-spos = sy-tabix.

wa_sort-fieldname = wa_col-fieldname.

wa_sort-up = 'X'.

append wa_sort to mt_sort.

endloop.

CALL METHOD sender->set_sort_criteria

EXPORTING

it_sort = mt_sort

  • EXCEPTIONS

  • NO_FIELDCATALOG_AVAILABLE = 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.

*

CALL METHOD go_grid->set_table_for_first_display

EXPORTING

i_structure_name = 'VBAK'

is_layout = g_layout

CHANGING

it_outtab = g_outtab

it_sort = mt_sort

EXCEPTIONS

OTHERS = 4.

IF sy-subrc 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

WHEN OTHERS.

ENDCASE.

cheers,

Hema.

Former Member
0 Kudos

Hi

In order to display an ALV report with specific columns already sorted by default you will need to build a

sort catalogue. This is fairly straight forward and is done in the following way:

Step 1. Add data declaration for sort catalogue

Step 2. Add code to build sort catalogue table

Step 3. Update 'REUSE_ALV_GRID_DISPLAY' FM call to include parameter 'it_sort'

ALV data declarations

data: it_sortcat type slis_sortinfo_alv occurs 1,

wa_sort like line of it_sortcat.

perform build_sortcat.

&----


*& Form build_sortcat

&----


Build Sort catalog

-


FORM build_sortcat .

wa_sort-spos = 1.

wa_sort-fieldname = 'EBELN'.

wa_sort-SUBTOT = 'X'. "subtotals any totals column by this field

gd_sortcat-tabname

APPEND wa_sort TO it_sortcat.

wa_sort-spos = 2.

wa_sort-fieldname = 'EBELP'.

gd_sortcat-tabname

APPEND wa_sort TO it_sortcat.

ENDFORM. " build_sortcat

call function 'REUSE_ALV_GRID_DISPLAY'

exporting

i_callback_program = gd_repid

i_callback_top_of_page = 'TOP-OF-PAGE'

is_layout = gd_layout

it_fieldcat = fieldcatalog[]

it_sort = it_sortcat

i_save = 'X'

tables

t_outtab = it_ekko

exceptions

program_error = 1

others = 2.

Former Member
0 Kudos

You have to use ls_fieldcat-do_sum statement in your coding

Check this sample coding

code&----


*& Report ZALVTOTAL *

*& *

&----


*& *

*& *

&----


REPORT ZALVTOTAL .

TYPES :

BEGIN OF ty_vbak,

vkorg TYPE vbak-vkorg, " Sales organization

kunnr TYPE vbak-kunnr, " Sold-to party

vbeln TYPE vbak-vbeln, " Sales document

netwr TYPE vbak-netwr, " Net Value of the Sales Order

waerk TYPE vbak-waerk, " Document currency

END OF ty_vbak.

DATA:

vbak TYPE vbak,

gt_vbak TYPE TABLE OF ty_vbak.

SELECT-OPTIONS :

s_vkorg FOR vbak-vkorg, " Sales organization

s_kunnr FOR vbak-kunnr, " Sold-to party

s_vbeln FOR vbak-vbeln. " Sales document

SELECTION-SCREEN :

SKIP, BEGIN OF LINE,COMMENT 5(27) v_1 FOR FIELD p_max.

PARAMETERS p_max(2) TYPE n DEFAULT '20' OBLIGATORY.

SELECTION-SCREEN END OF LINE.

-


INITIALIZATION.

v_1 = 'Maximum of records to read'.

-


START-OF-SELECTION.

PERFORM f_read_data.

PERFORM f_display_data.

-


Form f_read_data

-


FORM f_read_data.

SELECT vkorg kunnr vbeln netwr waerk

UP TO p_max ROWS

INTO TABLE gt_vbak

FROM vbak

WHERE kunnr IN s_kunnr

AND vbeln IN s_vbeln

AND vkorg IN s_vkorg.

ENDFORM. " F_READ_DATA

-


Form f_display_data

-


FORM f_display_data.

TYPE-POOLS: slis. " ALV Global types

DEFINE m_fieldcat.

add 1 to ls_fieldcat-col_pos.

ls_fieldcat-fieldname = &1.

ls_fieldcat-ref_tabname = 'VBAK'.

ls_fieldcat-do_sum = &2.

ls_fieldcat-cfieldname = &3.

append ls_fieldcat to lt_fieldcat.

END-OF-DEFINITION.

DEFINE m_sort.

add 1 to ls_sort-spos.

ls_sort-fieldname = &1.

ls_sort-up = 'X'.

ls_sort-subtot = &2.

append ls_sort to lt_sort.

END-OF-DEFINITION.

DATA:

ls_fieldcat TYPE slis_fieldcat_alv,

lt_fieldcat TYPE slis_t_fieldcat_alv,

lt_sort TYPE slis_t_sortinfo_alv,

ls_sort TYPE slis_sortinfo_alv,

ls_layout TYPE slis_layout_alv.

m_fieldcat 'VKORG' '' ''.

m_fieldcat 'KUNNR' '' ''.

m_fieldcat 'VBELN' '' ''.

m_fieldcat 'NETWR' 'X' 'WAERK'.

m_fieldcat 'WAERK' '' ''.

m_sort 'VKORG' 'X'. " Sort by vkorg and subtotal

m_sort 'KUNNR' 'X'. " Sort by kunnr and subtotal

m_sort 'VBELN' ''. " Sort by vbeln

ls_layout-cell_merge = 'X'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

is_layout = ls_layout

it_fieldcat = lt_fieldcat

it_sort = lt_sort

TABLES

t_outtab = gt_vbak.

ENDFORM. " F_DISPLAY_DATA[/code]