Saturday, 24 December 2016

Sorting Columns Dynamically in a Table Control

This is about dynamically sorting the contents of a column in a table maintenance generator and in turn within a table control.

1 : Consider a table ZTESTL with columns for Material , Language & Material Description.

Sorting Columns Dynamically in a Table Control

Generate a TMG for this table. Now go to to SM30 and maintain some entries for this table .
Next go To  SYSTEM-> STATUS .

Sorting Columns Dynamically in a Table Control

Note down the the program(GUI) and GUI status .

Sorting Columns Dynamically in a Table Control

2: Maintain the event ‘ST’  for this TMG. FORM routine will accept only the Program GUI as seen above. Utilities-> Table Maintenance Generator -> Environment-> Modification-> Events

Sorting Columns Dynamically in a Table Control


Double click on the editor and this will take you to SE41 screen of MENU painter. Enter the GUI status as EULG and edit the program .

Sorting Columns Dynamically in a Table Control

Maintain the SORT push button and activate the function code.This will define that you need a SORT push button on the screen with function code ‘SORT’.

Sorting Columns Dynamically in a Table Control

3: Go to the table control to define column select. Click on the overview screen ‘2’ from the TMG .

Sorting Columns Dynamically in a Table Control

Click on layout and go to change mode and maintain the below settings in ‘Table Control ‘ settings.

Line Sel: Multiple : Means – We can select multiple lines
Column Sel: Single : Means – We can select only one column at a time.
w/SelColumn : Check this field and define a value. This will tell us if a column is selected or not.

Sorting Columns Dynamically in a Table Control

4: Now that we have the push button ‘SORT’ defined and also have the column selection enabled in the table control, lets define what should happen once the push button is clicked.In the PAI of the screen add a module SORT.

Sorting Columns Dynamically in a Table Control

Add code to SORT Module based on column names.

MODULE sort INPUT.
DATA : gt_ztest TYPE STANDARD TABLE OF ztestl.
DATA : table_control_col TYPE LINE OF  scxtab_column_it.
DATA : col_sel    TYPE scxtab_column, lv_col_pos TYPE int4.

CASE function.
WHEN ‘SORT’. ”  Function Code
READ TABLE tctrl_ztestl-cols INTO table_control_col WITH KEY selected = ‘X’.
IF sy-subrc = 0.
CASE table_control_col-index .
WHEN ‘1’.
gt_ztest[] = total[].
SORT gt_ztest[] ASCENDING BY matnr.
total[] = gt_ztest[].
extract[] = total[].
WHEN ‘2’.
gt_ztest[] = total[].
SORT gt_ztest[] ASCENDING BY spras.
total[] = gt_ztest[].
extract[] = total[].
WHEN ‘3’.
gt_ztest[] = total[].
SORT gt_ztest[] ASCENDING BY maktx.
total[] = gt_ztest[].
extract[] = total[].
ENDCASE.
ELSE.
” When no column selected for SORT, pop up displays a message to select a column to sort
CALL FUNCTION ‘POPUP_TO_INFORM’
EXPORTING
titel =  ‘Information’
txt1  = ‘Please Select a Column to Sort!’
txt2  = mark_extract.
ENDIF.
ENDCASE.
ENDMODULE.

5: Now table is available for Sort operation. Just click on any column and press the SORT push button . You can select any column dynamically for SORT operation.

Sorting Columns Dynamically in a Table Control

1 comment:

  1. This code doesn't work.We can not map like gt_ztest[] = total[].
    Any other solution?
    Thank you!

    ReplyDelete