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 can we sort records in alv, give example

Former Member
0 Kudos

how can we sort records in alv, give example

5 REPLIES 5

Former Member
0 Kudos

Hi

<b>Setting Sort Conditions</b>

It is possible to set sort conditions for the table data. This is achieved by filling an internal table of structure “LVC_T_SORT” which consists of the sort criteria. To have an initial sorting, pass it to the parameter “IT_SORT” of the method “set_table_for_first_display”.


FORM prepare_sort_table CHANGING pt_sort TYPE lvc_t_sort .
 DATA ls_sort TYPE lvc_s_sort .
ls_sort-spos = '1' .
ls_sort-fieldname = 'CARRID' .
ls_sort-up = 'X' . "A to Z
ls_sort-down = space .
APPEND ls_sort TO pt_sort .
ls_sort-spos = '2' .
ls_sort-fieldname = 'SEATSOCC' .
ls_sort-up = space .
ls_sort-down = 'X' . "Z to A
APPEND ls_sort TO pt_sort .
ENDFORM. " prepare_sort_table

<b>Preparing the table for sorting settings</b>

We have two important points to tell about this topic. First one is that, be ready for a short dump if any one of the fields given to be sorted is not in the content of the field catalog. Secondly, when you make ALV Grid to sort data, by default it vertically merges fields having the same content. To avoid from this for all of the columns, you can set “no_merging” field of the layout structure to ‘X’. If you want to disable merging for just some columns, set “no_merging” field of the field catalog row corresponding to that column.

You can get and set sort criteria applied whenever you want by using methods “get_sort_criteria” and “set_sort_criteria”, respectively.

<b>

Sort Using FACTORY CLASSES</b>

<b>Sorts – CL_SALV_SORTS</b>

we can add some sorting to the ALV grid. Create the object reference variable and receive the object using the GET_SORTS method of the GR_TABLE object. Next, add the sort by calling the ADD_SORT method of the GR_SORTS object.


report zalvom_demo1. 
data: ispfli type table of spfli.
 data: gr_table type ref to cl_salv_table.
 data: gr_functions type ref to cl_salv_functions.
 data: gr_display type ref to cl_salv_display_settings. data: gr_columns type ref to cl_salv_columns_table.
 data: gr_column type ref to cl_salv_column_table. 
data: gr_sorts type ref to cl_salv_sorts.
data: color type lvc_s_colo.
start-of-selection. 
select * into table ispfli from spfli. cl_salv_table=>factory( importing r_salv_table = gr_table   changing t_table = ispfli ).
 gr_functions = gr_table->get_functions( ). gr_functions->set_all( abap_true ).
 gr_display = gr_table->get_display_settings( ). gr_display->set_striped_pattern( cl_salv_display_settings=>true ). gr_display->set_list_header( 'This is the heading' ). gr_columns = gr_table->get_columns( ). 
gr_column ?= gr_columns->get_column( 'CITYTO' ). gr_column->set_long_text( 'This is long text' ). gr_column->set_medium_text( 'This is med text' ). gr_column->set_short_text( 'This is sh' ).
 gr_column ?= gr_columns->get_column( 'CITYFROM' ). color-col = '6'.
 color-int = '1'. 
color-inv = '0'.
 gr_column->set_color( color ). 
gr_sorts = gr_table->get_sorts( ). 
gr_sorts->add_sort 'CITYTO' ). 
gr_table->display( ).

Regards

Ravish

<b><i>

Reward if useful to you</i></b>

Message was edited by:

Ravish Garg

Former Member
0 Kudos

Hi

u can sort the records after display output in alv.

or check this extra stmts one

SORT TYPE slis_t_sortinfo_alv WITH HEADER LINE,

FORM FILL_SORT .

SORT-DOWN = 'X'.

SORT-SPOS = 1.

SORT-FIELDNAME = 'MATNR'.

SORT-tabname = 'MARA'.

APPEND SORT.

ENDFORM. " FILL_SORT

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

IT_SORT = SORT[]

reward points to all helpful answers

kiran.M

Former Member
0 Kudos

Hi,

We will specify the sort criteria information while appending into one internal table and the same internal tabel will be used in the ALV FM.

Regards,

Satya

Former Member
0 Kudos

Hi,

When u use <b>REUSE_ALV_GRID_DISPLAY</b> function module, u can an option in importing parameter as <b>it_sort = it_sort, in this u mark it as 'X'</b> in ur coding. U will get the records in sorted order.

Reward points if found useful.

Message was edited by:

Sathya

Former Member
0 Kudos

DATA: isort TYPE slis_t_sortinfo_alv,

isort1 TYPE slis_sortinfo_alv.

PERFORM sort_data_fcat.

FORM sort_data_fcat .

CLEAR isort1.

isort1-spos = '1'.

isort1-fieldname = 'WERKS'.

isort1-up = 'X'.

  • isort1-subtot = 'X'.

APPEND isort1 TO isort.

CLEAR isort1.

isort1-spos = '2'.

isort1-fieldname = 'TAXM1'.

  • isort1-fieldname = 'TAXKM'.

isort1-up = 'X'.

isort1-subtot = 'X'.

APPEND isort1 TO isort.

ENDFORM. " sort_data_fcat

......

FORM z_reuse_alv_grid_display .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

  • I_INTERFACE_CHECK = ' '

  • I_BYPASSING_BUFFER = ' '

  • I_BUFFER_ACTIVE = ' '

i_callback_program = sy-repid

  • I_CALLBACK_PF_STATUS_SET = ' '

.......

........

it_fieldcat = fcat[]

  • IT_EXCLUDING =

  • IT_SPECIAL_GROUPS =

it_sort = <b>isort</b>

  • IT_FILTER =

  • IS_SEL_HIDE =

......

.......

i think this will help u.