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

SORT IN ALV with Objects

Former Member
0 Likes
3,849

Hi , I want to build a sort table for my ALV output to show duplicate values only once. Currently I have three fields : material number , description and quantity , that are being repeated for all entries. How to use a sort and how to pass it to the field catalog .

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,473

if you want to display repeated data only once but not delete the records,

then the solution is to use the "sort table catalog" , a kind of input table for function REUSE_ALV* . Modify the "sort catalog" by adding those fields and sort order ,

then pass the "sort catalog" to the function along with other input ( e.g. field catalog ).

if you want to display repeated data only once but not delete the records,

then the solution is to use the "sort table catalog" , a kind of input table for function REUSE_ALV* . Modify the "sort catalog" by adding those fields and sort order ,

then pass the "sort catalog" to the function along with other input ( e.g. field catalog ).

13 REPLIES 13
Read only

Former Member
0 Likes
2,473

you can write simple logic :

DELETE ADJACENT DUPLICATES FROM itab comparing matnr.

thanks

Seshu

Read only

Former Member
0 Likes
2,473

I forgot to tell you before deleting the duplicates

just use sort itab by matnr and delete it.

nothing with fieldcatlog,you just need add the code after select query.

Thanks

Seshu

Read only

0 Likes
2,473

Right now my reprt is like this :

Matnr total quant kunnr quant

-


mat1 500 kun1 100

mat1 500 kun2 400

mat2 300 kun3 150

mat2 300 kun1 150

My output should look like.

Matnr total quant kunnr quant

-


mat1 500 kun1 100

kun2 400

mat2 300 kun3 150

kun1 150

I am using ALV grid in ABAp objects. There is only one internal table that is passed to the ALV.

Read only

0 Likes
2,473

Please see this revised post , this is more clear.

This is the display I am getting right now.

Matnr total quant kunnr quant

-


mat1 500 kun1 100

mat1 500 kun2 400

mat2 300 kun3 150

mat2 300 kun1 150

My output should look like.

Matnr- total quant- kunnr--- quant

-


mat1--


500
kun1
--- 100

-


kun2 -


400

mat2--


300
kun3
--


150

-


kun1----


150

Read only

Former Member
0 Likes
2,474

if you want to display repeated data only once but not delete the records,

then the solution is to use the "sort table catalog" , a kind of input table for function REUSE_ALV* . Modify the "sort catalog" by adding those fields and sort order ,

then pass the "sort catalog" to the function along with other input ( e.g. field catalog ).

Read only

0 Likes
2,473

I am using ALV in abap Objects. If there is a sample code for achieving the output , I will really appreciate it.

thanks.

Read only

0 Likes
2,473

Check the below code ,bold one is important..

Just declare one more internal table and use control break statements ..,use total qty field as charcter for ALV Internal table

REPORT ZTEST_ALV1 no standard page heading.

  • Good Example for Object Oriented ALV

  • Handles Double click

class cl_event_receiver definition.

public section.

methods handle_double_click

for event double_click of cl_gui_alv_grid

importing e_row e_column.

private section.

endclass.

class cl_event_receiver implementation.

method handle_double_click.

perform drill_down using e_row-index.

endmethod.

endclass.

  • Data Declaration Part

tables : makt.

  • Internal table

types : begin of ty_itab ,

matnr(18) type c,

tqty like vbap-kwmeng,

kunnr(10) type c,

qty like vbap-kwmeng,

end of ty_itab.

data itab type standard table of ty_itab.

data wa_itab like line of itab.

data wa_itab2 like line of itab.

  • ALV Internal table

  • Internal table

types : begin of ty_itab1 ,

matnr(18) type c,

tqty(15) type c,

kunnr(10) type c,

qty like vbap-kwmeng,

end of ty_itab1.

data : itab1 type standard table of ty_itab1.

data : wa_itab1 like line of itab1.

  • ALV Variable

data : v_repid like sy-repid,

v_dynnr like sy-dynnr,

ok_code like sy-ucomm,

cl_gui_custom_container type ref to cl_gui_custom_container,

cl_gui_alv_grid type ref to cl_gui_alv_grid,

cl_event_reciver type ref to cl_event_receiver,

layout type lvc_s_layo,

fieldcat type lvc_t_fcat.

data : flag type c.

  • Fill the default values

initialization.

v_repid = sy-repid.

v_dynnr = sy-dynnr.

start-of-selection.

  • Get the data from MAKT Table

perform get_data_makt.

  • Call Screen

perform call_screen.

&----


*& Form get_data_makt

&----


  • Get the data from makt table

----


FORM get_data_makt.

wa_itab-matnr = 'Mat1'.

wa_itab-tqty = '500'.

wa_itab-kunnr = 'Kun1'.

wa_itab-qty = '100'.

append wa_itab to itab.

wa_itab-matnr = 'Mat1'.

wa_itab-tqty = '500'.

wa_itab-kunnr = 'Kun2'.

wa_itab-qty = '400'.

append wa_itab to itab.

wa_itab-matnr = 'Mat2'.

wa_itab-tqty = '300'.

wa_itab-kunnr = 'Kun3'.

wa_itab-qty = '150'.

append wa_itab to itab.

wa_itab-matnr = 'Mat2'.

wa_itab-tqty = '300'.

wa_itab-kunnr = 'Kun1'.

wa_itab-qty = '150'.

append wa_itab to itab.

sort itab by matnr.

<b>* Use internal table control break statements

loop at itab into wa_itab.

move wa_itab to wa_itab2.

at new tqty.

move wa_itab2-matnr to wa_itab1-matnr.

move wa_itab2-tqty to wa_itab1-tqty.

move wa_itab2-kunnr to wa_itab1-kunnr.

move wa_itab2-qty to wa_itab1-qty.

flag = 'X'.

endat.

if flag ne 'X'.

move space to wa_itab1-tqty.

move wa_itab2-kunnr to wa_itab1-kunnr.

move wa_itab2-qty to wa_itab1-qty.

endif.

append wa_itab1 to itab1.

clear : wa_itab,

flag,

wa_itab1,

wa_itab2.

endloop.</b>

ENDFORM. " get_data_makt

&----


*& Form call_screen

&----


  • Screen 1000

----


FORM call_screen.

call screen 1000.

ENDFORM. " call_screen

&----


*& Module USER_COMMAND_1000 INPUT

&----


  • text

----


MODULE USER_COMMAND_1000 INPUT.

case ok_code.

when 'EXIT'.

leave to screen 0.

when 'BACK'.

leave to screen 0.

when 'CANC'.

leave to screen 0.

endcase.

ENDMODULE. " USER_COMMAND_1000 INPUT

&----


*& Module STATUS_1000 OUTPUT

&----


  • text

----


MODULE STATUS_1000 OUTPUT.

SET PF-STATUS '1000'.

  • SET TITLEBAR 'xxx'.

CREATE OBJECT CL_GUI_CUSTOM_CONTAINER

EXPORTING

  • PARENT =

CONTAINER_NAME = 'GRID1'

  • STYLE =

  • LIFETIME = lifetime_default

REPID = v_repid

DYNNR = v_dynnr

  • NO_AUTODEF_PROGID_DYNNR =

EXCEPTIONS

CNTL_ERROR = 1

CNTL_SYSTEM_ERROR = 2

CREATE_ERROR = 3

LIFETIME_ERROR = 4

LIFETIME_DYNPRO_DYNPRO_LINK = 5

others = 6

.

IF SY-SUBRC <> 0.

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

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

ENDIF.

CREATE OBJECT CL_GUI_ALV_GRID

EXPORTING

  • I_SHELLSTYLE = 0

  • I_LIFETIME =

I_PARENT = cl_gui_custom_container

  • I_APPL_EVENTS = space

  • I_PARENTDBG =

  • I_APPLOGPARENT =

  • I_GRAPHICSPARENT =

  • I_USE_VARIANT_CLASS = SPACE

  • I_NAME =

EXCEPTIONS

ERROR_CNTL_CREATE = 1

ERROR_CNTL_INIT = 2

ERROR_CNTL_LINK = 3

ERROR_DP_CREATE = 4

others = 5

.

IF SY-SUBRC <> 0.

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

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

ENDIF.

  • Create Event Receiver

create object cl_event_reciver.

  • ALV Specific .Data Selection

  • Populate field catalog

perform get_fieldcatlog.

CALL METHOD cl_gui_alv_grid->SET_TABLE_FOR_FIRST_DISPLAY

EXPORTING

  • I_BYPASSING_BUFFER =

  • I_BUFFER_ACTIVE =

  • I_CONSISTENCY_CHECK =

  • I_STRUCTURE_NAME = 'I_MAKT'

  • IS_VARIANT =

I_SAVE = 'U'

  • I_DEFAULT = 'X'

  • IS_LAYOUT =

  • IS_PRINT =

  • IT_SPECIAL_GROUPS =

  • IT_TOOLBAR_EXCLUDING =

  • IT_HYPERLINK =

  • IT_ALV_GRAPHICS =

  • IT_EXCEPT_QINFO =

CHANGING

IT_OUTTAB = itab1

IT_FIELDCATALOG = fieldcat

  • IT_SORT =

  • IT_FILTER =

EXCEPTIONS

INVALID_PARAMETER_COMBINATION = 1

PROGRAM_ERROR = 2

TOO_MANY_LINES = 3

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.

set handler cl_event_reciver->handle_double_click for cl_gui_alv_grid.

ENDMODULE. " STATUS_1000 OUTPUT

&----


*& Form get_fieldcatlog

&----


  • text

----


FORM get_fieldcatlog.

data ls_fcat type lvc_s_fcat.

refresh fieldcat.

  • Material #

clear ls_fcat.

ls_fcat-reptext = 'Material Number'.

ls_fcat-coltext = 'Material Number'.

ls_fcat-fieldname = 'MATNR'.

ls_fcat-tabname = 'ITAB1'.

ls_fcat-outputlen = '18'.

ls_fcat-col_pos = 1.

append ls_fcat to fieldcat.

  • Material #

clear ls_fcat.

ls_fcat-reptext = 'Total Qty'.

ls_fcat-coltext = 'Total Qty'.

ls_fcat-fieldname = 'TQTY'.

ls_fcat-tabname = 'ITAB1'.

ls_fcat-outputlen = '15'.

ls_fcat-col_pos = 2.

append ls_fcat to fieldcat.

  • Material #

clear ls_fcat.

ls_fcat-reptext = 'Customer Number'.

ls_fcat-coltext = 'Customer Number'.

ls_fcat-fieldname = 'KUNNR'.

ls_fcat-tabname = 'ITAB1'.

ls_fcat-outputlen = '18'.

ls_fcat-col_pos = 3.

append ls_fcat to fieldcat.

  • Material #

clear ls_fcat.

ls_fcat-reptext = 'QTY'.

ls_fcat-coltext = 'QTY'.

ls_fcat-fieldname = 'QTY'.

ls_fcat-tabname = 'ITAB1'.

ls_fcat-outputlen = '18'.

ls_fcat-col_pos = 4.

append ls_fcat to fieldcat.

ENDFORM. " get_fieldcatlog

&----


*& Form drill_down

&----


  • text

----


  • -->P_E_ROW_INDEX text

----


FORM drill_down USING P_E_ROW_INDEX.

*read table i_makt into wa_makt index p_e_row_index.

*

*if sy-subrc eq 0.

*

*set parameter id 'MAT' field wa_makt-matnr.

*call transaction 'MM02' and skip first screen.

*endif.

ENDFORM. " drill_down

Thanks

Seshu

Read only

0 Likes
2,473

The problem that I am facing does not get solved by using these events. I have to actually use event handler to input a value , this should be subtracted from the total quantity for each block of code for matnr.

Thank you Seshu for your help. ,

Read only

0 Likes
2,473

Which version of ALV objects are you using?

CL_GUI_ALV_GRID and related clases or CL_SALV_TABLE and related classes?

They're not the same; always please identify the actual class name since there is more than one way to do this in objects.

-


For CL_GUI_ALV_GRID, you have to set values in the field catalog and sort table:

DATA: gt_sort TYPE lvc_t_sort,
      gs_sort TYPE lvc_t_sort.

To set a field as a sort field, you create a sort table:


refresh gt_sort. 

clear gs_sort. 
gs_sort-fieldname = 'FIELD1'. 
gs_sort-spos = 1. 
gs_sort-up = 'X'. 
*gs_sort-down = 'X'.   " only do one of up or down, depending on desired sort order
gs_sort-subtot = 'X'.   "subtotal is optional
append gs_sort to gt_sort.

clear gs_sort. 
gs_sort-fieldname = 'FIELD2'. 
gs_sort-spos = 2. 
gs_sort-up = 'X'. 
*gs_sort-down = 'X'.   " only do one of up or down, depending on desired sort order
gs_sort-subtot = 'X'.   "optional
append gs_sort to gt_sort.

etc for as many sort fields as needed

Then to identify the fields to be subtotaled, set the DO_SUM attribute for each field in the field catalog that should be subtotaled:

gt_fieldcat-do_sum = 'X'.

You would add the sort table to you method call for SET_TABLE_FOR_FIRST_DISPLAY

    CALL METHOD g_alv->set_table_for_first_display
      EXPORTING
        is_variant           = gs_variant   
        i_save               = 'A'            
        is_layout            = gs_layout
        it_toolbar_excluding = gt_toolbar_excl
      CHANGING
        it_outtab            = gt_alv_grid_tab
        it_fieldcatalog      = gt_fieldcat
        it_sort              = gt_sort.

If you wanted to change the sort later under program control, you would call the method SET_SORT_CRITERIA

CALL METHOD g_alv->set_sort_criteria
  EXPORTING
    it_sort                   = gt_sort
*  EXCEPTIONS
*    NO_FIELDCATALOG_AVAILABLE = 1
*    others                    = 2

-


For SALV

  DATA: lr_sorts            TYPE REF TO cl_salv_sorts.
  DATA: lr_agg              TYPE REF TO cl_salv_aggregations.

  TRY.
      lr_sorts = gr_table->get_sorts( ).
      lr_sorts->add_sort( columnname = 'FIELD1'     subtotal = gc_true ).  
      lr_sorts->add_sort( columnname = 'FIELD2'    subtotal = gc_true ).
      lr_sorts->add_sort( columnname = 'FIELD3'    subtotal = gc_true ).  "subtotal is optional for each field

etc

    CATCH cx_salv_existing.                             "#EC NO_HANDLER
    CATCH cx_salv_not_found.                            "#EC NO_HANDLER
    CATCH cx_salv_data_error.                           "#EC NO_HANDLER
  ENDTRY.

  TRY.
      lr_agg = gr_table->get_aggregations( ).
      lr_agg->add_aggregation( 'FIELD4' ).
      lr_agg->add_aggregation( 'FIELD5' ).

etc

    CATCH cx_salv_existing.                             "#EC NO_HANDLER
    CATCH cx_salv_not_found.                            "#EC NO_HANDLER
    CATCH cx_salv_data_error.                           "#EC NO_HANDLER
  ENDTRY.

-


good luck

Brian

Read only

0 Likes
2,473

Hi , thank you for the code , however the output remains unchanged.

I am using CL_GUI_ALV_GRID , had created the sort table the way you showed , and also passed it to the set_table_for_first_display ,, but there is no change inthe out put I also put the method go_grid->set_sort_criteria right after set_table_for_first _diplay , There is still no change. I am able to see the material numbers and the qunatity fields repeated.

Read only

0 Likes
2,473

hmmm

i clear the no_merging in gs_layout and fieldcat in my program , but it does not work either .

Read only

0 Likes
2,473

Krish, i have run into this problem with sorting on CL_GUI_ALV_GRID, where the merging feature does not work, regardless of the setting of the no_merging flag, and I do not have a solution for that.

In such cases, I have generally converted to SALV, unless I need input/edit features that SALV does not have, and in that case I just live with the unmerged cells.

While the answer that I gave above is the "textbook" correct answer for sorting with CL_GUI_ALV_GRID, it appears that problems with the merge feature in CL_GUI_ALV_GRID still exist.

There appear to be some bugs in particular releases related to ALV and cell merging.

For example, see

http://www.elec.ucl.ac.be/logistique/informatique/SAP/Manual-6.4r4/relnotes/new.htm

which highlight when bug fixes were released.

In particular note the following (for example):

Release 6.40 rev 2 (June 7, 2005)
Bugfixes

     * Grid control: 
          * The titlebar has not been displayed when the toolbar did not have any
             buttons. 
          * The methods SelectedRowsType2() and  SelectedRows2() were missing, 
             causing an ABAP dump in case they were called by the application. 
          * The wrong column was sorted after pressing the sort button in the toolbar. 
          * Rows can now be (de)selected by ctrl-click. 
          * Row selection buttons have been missing in some cases. 
          * Merging cells of several columns is now also supported for complete
             columns, not only for particular cells. 
          * In some cases, the background color of non-changeable cells has been
            wrong.
          * The new data of a currently edited cell now is sent to the application
             server. 
          * Reordering columns by Drag & Drop has not been reported correctly.

If you are on a release prior to the one specified, you may still have problems.

-


However, if you are doing output only, it should be relatively easy to convert to SALV. SALV is nice in that you don't have to handcode the field catalog.

Rich Heilman, who is a regular on SDN, did a nice write up on SALV, which is available on-line

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/webcontent/uuid/207dc670-6e8e-2910-3297-d1a1a... [original link is broken]

You can also use SE84, Info system and search for program examples starting SALV*

Good luck

Brian

Read only

Former Member
0 Likes
2,473

Check Std progarams BCALV * OR RSDEMO * in SE38 for ALV OOPs

Rewards if useful...................

Minal