Sorting with any/multiple fields in TABLE MAINTENA...
Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
Then create one custom FM with following logic that will popup the dialog box for the user to select the columns of table control in SM30.
This FM should have view name of type DD02L-TABNAME as importing parameter and exporting parameter of type ABAP_SORTORDER_TAB that contains the field names to be sorted.
Define one global internal table of type DFIES to retrieve the structure of the table and then use the same in the custom screen
Inside this FM use FM 'VIEW_GET_FIELDTAB' to get the list of the columns of table currently edited/viewed in SM30.
Then create one custom screen of type dialog inside this FM.
Create one table control using wizardwith multiple selection on the screen that displays the column names for the user to select. Also add two radio buttons for ascending or descending as shown below.
Below is the code written inside the custom FM
FUNCTION zca_common_tmg_sort.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(I_VAR_VIEW_NAME) TYPE DD02L-TABNAME
*" EXPORTING
*" VALUE(E_TAB_SORTORDER) TYPE ABAP_SORTORDER_TAB
*"----------------------------------------------------------------------
CALL FUNCTION 'VIEW_GET_FIELDTAB'
EXPORTING
view_name = i_var_view_name
TABLES
fieldtab = g_tab_fields
EXCEPTIONS
table_not_found = 1
OTHERS = 2.
IF sy-subrc = 0.
DELETE g_tab_fields WHERE datatype = 'CLNT'.
CALL SCREEN 9000 STARTING AT 5 3.
ENDIF.
e_tab_sortorder = g_tab_sortorder.
Below is the code written inside the PAI module of custom screen.
MODULE user_command_9000 INPUT.
FIELD-SYMBOLS: <l_wa_sortorder> LIKE LINE OF g_tab_sortorder.
CLEAR g_tab_sortorder[].
CASE sy-ucomm.
WHEN 'OK'.
LOOP AT g_tab_fields INTO g_wa_fields
WHERE inttype = 'X'.
APPEND INITIAL LINE TO g_tab_sortorder ASSIGNING <l_wa_sortorder>.
<l_wa_sortorder>-name = g_wa_fields-fieldname.
IF g_var_descend IS NOT INITIAL.
<l_wa_sortorder>-descending = abap_true.
ENDIF.
CLEAR g_wa_fields.
ENDLOOP.
SET SCREEN 0.
WHEN 'CAN'.
SET SCREEN 0.
ENDCASE.
ENDMODULE. " USER_COMMAND_9000 INPUT
After creating the above FM and custom screen, we need to go back to table maintenance generator of respective table and go to Environment->Modification->Maintenance Screens and double on the screen.
Then create one PAI module in the screen layout and call the custom FM in this module.
Note: Only this PAI module statement(Module sort In process after input) in screen layout needs to be re-written once we regenerate the maintenance screen again. Rest everything remains intact on regeneration.
Below is the code to be written in the module
MODULE sort INPUT.
DATA: l_tab_agent TYPE STANDARD TABLE OF zsdc_frght_agent,
l_tab_sortorder TYPE abap_sortorder_tab..
FIELD-SYMBOLS: <ls_xfrom> TYPE x, "Hexadecimal value of from value
<ls_xto> TYPE x. "Hexadecimal value of to value