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: 

sorting in table control

Former Member
0 Kudos

Hi all,

I want to know whether is there a sorting facility for different columns in a table control of module pool program.

I have 5 columns in table control .Is there any way that i can sort on whatever column i want.

I mean like we have in alv right. Is it possible in table control too.

If yes please let me know

1 ACCEPTED SOLUTION

ferry_lianto
Active Contributor
0 Kudos

Hi Swathi,

Please check demo program <b>DEMO_DYNPRO_TABCONT_LOOP_AT</b>.

Hope this will help.

Regards,

Ferry Lianto

9 REPLIES 9

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Yes, you can, but you need to code for it. You need to find what column the users cursor is in when the user clicks the sort button in the screen.

data: cursorfield(10) type c.
GET cursor field cursorfield.





This the cursor is in the table control column, CURSORFIELD will be populated with the value 'ITAB-FLD1'. Here you need to stripp off the ITAB- and just have the FLD1. Then you can use this in a dynamic sort statement.



  data: sort_table(20) type c.
  data: sort_column(20) type c.

  split cursorfield at '-' into sort_table sort_column.

  if sort_table = 'ITAB'.
    sort itab by (sort_column) ascending.
  endif.

REgards,

Rich Heilman

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

You can also do this when the user highlights a column by clicking the header to find what column to sort by, this is a little more complex. I'm looking for an example.

Regards,

Rich Heilman

ferry_lianto
Active Contributor
0 Kudos

Hi Swathi,

Please check demo program <b>DEMO_DYNPRO_TABCONT_LOOP_AT</b>.

Hope this will help.

Regards,

Ferry Lianto

0 Kudos

Ok, here is the example of the user selecting the column by highlighting the column and clicking a sort button.

In the user command. Here itabcon is the name of your table control.




data:
      cursor_table(5) type c,
      cursor_field(10) type c,
      cursor_column(20) type c  ,
       column_name like line of itabcon-cols.

  clear: column_name, cursor_column, cursor_table, cursor_field.
  loop at itabcon-cols into column_name where not selected is initial.
    cursor_column = column_name.
    split cursor_column at '-' into cursor_table cursor_field.
  endloop.
  if cursor_column is initial.
    get cursor field cursor_column.
  endif.

case ok_code.

    when 'ASORT'.

      clear ok_code.
      sort itab stable by (cursor_field) ascending.

endcase.


REgards,

Rich Heilman

0 Kudos

Hi Rich Heilman.,

     It's Working Fine For Single Column Sorting. But I Need To Sort The Table By Multiple Column As The User Selected Order. Please Guide Me How To Do?

Former Member
0 Kudos

Hi Rich,

This my screEn PBO

Process before output.

module status_100.

module get_data.

loop at itab with control

cnt_abc cursor cnt_abc-top_line.

module display_data.

endloop.

I DECLARED IN module get_data.

DATA : CURSORFIELD(10) TYPE C.

GET cursor field cursorfield.

Iam retrieving data from ztable into itab

if sy-ucomm = 'SORT'.

sort itab by cursorfield. "sorting

else.

sort itab by erdat ertime.

endif.

IT GIVES ME AN ERROR SAYING

no component exists with name cursor field.

Please let me know iam wrong anywhere in the process.

0 Kudos

Hi,

check the syntax:

GET CURSOR FIELD fldname VALUE fldval.

Ex:

DATA: CURSORFIELD(20) TYPE C,CURSOR(10) TYPE C.

GET CURSOR FIELD CURSORFIELD VALUE CURSOR.

Regards

Appana

0 Kudos

Please check the samples above closer, the CUSORFIELD needs to be wrapped with parenthesis and this should be done in the PAI when the user clicks the sort button.

  sort itab stable by <b>(cursor_field)</b> ascending.

REgards,

Rich Heilman

0 Kudos

Hi Rich,

I have fixed the problem with the help of your code.

I have awarded full points.

Have a nice day