Friday, 3 August 2007

ABAP: Gettting the selected column

Every TABLEVIEW control defined in an ABAP program contains a cols field that contains information about selected columns. Whether or not column selection is allowed is set by the tableview object's properties in the screen painter. (see also the relevant image for the posting How to setup a Screen containing a Table a few days back.

One common use for column selection is sorting, so an example sorting function that sorts a global table named tbl_my_data based on the selected column might look like this :

FORM sort_table USING  sort_mode LIKE sy-ucomm.
  FIELD-SYMBOLS
     TYPE cxtab_column.

* find out if a column is selected.
  READ TABLE tc_mytab-cols
    WITH KEY selected = 'X'
    ASSIGNING <selected_col>.

  IF sy-subrc <> 0.
    MESSAGE s888(sabapdocu) WITH 'No column is selected'.
    EXIT.
  ENDIF.

* sort according to the selected column
  CASE sort_mode.
    WHEN 'SORT_ASC'.
      CASE <selected_col>-index.
        WHEN 1.
          SORT tbl_mydata ASCENDING BY field1.
        WHEN 2.
          SORT tbl_mydata ASCENDING BY field2.
        WHEN OTHERS.
          MESSAGE s888(sabapdocu) WITH 'Not yet implemented'.
      ENDCASE.
    WHEN 'SORT_DSC'.
      CASE <selected_col>-index.
        WHEN 1.
          SORT tbl_mydata DESCENDING BY field1.
        WHEN 2.
          SORT tbl_mydata DESCENDING BY field2.
        WHEN OTHERS.
          MESSAGE s888(sabapdocu) WITH 'Not yet implemented'.
      ENDCASE.
  ENDCASE.
ENDFORM.                    " sort_table

No comments :