Dynamic column sorting for reports

Allow users to sort the report by columns after display without having to regather data.

Allow users to sort the report by columns after display without having to regather data. User just clicks on column

heading and columns are resorted, like the sort feature in Excel.


REPORT YSMSORT NO STANDARD PAGE HEADING.
* Dynamic Report Sort
* just select a column heading to sort by that column
*
* Sean Morrow  seanmorrow@hotmail.com
*
TABLES VBUK.

* itab for column headings
DATA: BEGIN OF IT_RPT_HEADINGS OCCURS 0,
        NAME(20) TYPE C,
        AGE(10) TYPE C,
        JOB(20) TYPE C,
        ORDER(20) TYPE C.
DATA: END OF IT_RPT_HEADINGS.

*itab for report lines
DATA: BEGIN OF IT_RPT_LINES OCCURS 0,
        NAME(20) TYPE C,
        AGE(10) TYPE C,
        JOB(20) TYPE C,
        ORDER   LIKE VBUK-VBELN.
DATA: END OF IT_RPT_LINES.

* variable to hold the field name
DATA: FIELD_NAME(35) TYPE C.

* This is just a demonstration, create the data for headings and lines
INITIALIZATION.
  IT_RPT_HEADINGS-NAME = 'NAME'.
  IT_RPT_HEADINGS-AGE  = 'AGE'.
  IT_RPT_HEADINGS-JOB  = 'JOB TITLE'.
  IT_RPT_HEADINGS-ORDER  = 'ORDER'.
  APPEND IT_RPT_HEADINGS.

  IT_RPT_LINES-NAME = 'Clark, Bob'.
  IT_RPT_LINES-AGE  = '47'.
  IT_RPT_LINES-JOB  = 'General Manager'.
  IT_RPT_LINES-ORDER  = '0000001022'.
  APPEND IT_RPT_LINES.

  IT_RPT_LINES-NAME = 'Ron, Ellis'.
  IT_RPT_LINES-AGE  = '54'.
  IT_RPT_LINES-JOB  = 'Saleman'.
  IT_RPT_LINES-ORDER  = '0000001023'.
  APPEND IT_RPT_LINES.

  IT_RPT_LINES-NAME = 'Adems, Austin'.
  IT_RPT_LINES-AGE  = '44'.
  IT_RPT_LINES-JOB  = 'Cook'.
  IT_RPT_LINES-ORDER  = '0000001024'.
  APPEND IT_RPT_LINES.

  IT_RPT_LINES-NAME = 'King, Stephen'.
  IT_RPT_LINES-AGE  = '16'.
  IT_RPT_LINES-JOB  = 'Writer'.
  IT_RPT_LINES-ORDER  = '0000001025'.
  APPEND IT_RPT_LINES.

  IT_RPT_LINES-NAME = 'Davis, Miles'.
  IT_RPT_LINES-AGE  = '66'.
  IT_RPT_LINES-JOB  = 'Writer'.
  IT_RPT_LINES-ORDER  = '0000001026'.
  APPEND IT_RPT_LINES.

START-OF-SELECTION.

  PERFORM WRITE_HEADINGS.
  PERFORM WRITE_LINES.


AT LINE-SELECTION.
* get the field name user has selected
  GET CURSOR FIELD FIELD_NAME.

* sort the lines based on user selection
  CASE FIELD_NAME.
    WHEN 'IT_RPT_HEADINGS-NAME'.
      SORT IT_RPT_LINES BY NAME AGE JOB ORDER.
    WHEN 'IT_RPT_HEADINGS-AGE'.
      SORT IT_RPT_LINES BY AGE NAME JOB ORDER.
    WHEN 'IT_RPT_HEADINGS-JOB'.
      SORT IT_RPT_LINES BY JOB NAME AGE ORDER.
    WHEN 'IT_RPT_HEADINGS-ORDER'.
      SORT IT_RPT_LINES BY  ORDER NAME AGE JOB.
    WHEN 'IT_RPT_LINES-ORDER'.  "if the user selected an order, show it
      PERFORM DISPLAY_DOCUMENT USING 'VA03'
                                     IT_RPT_LINES-ORDER
                                     ' '.  " VBTYP -
  ENDCASE.

*rewrite the report
  PERFORM WRITE_HEADINGS.
  PERFORM WRITE_LINES.
*&---------------------------------------------------------------------*
*&      Form  WRITE_LINES
*&---------------------------------------------------------------------*
FORM WRITE_LINES.
  FORMAT COLOR COL_BACKGROUND.
  LOOP AT IT_RPT_LINES.
    WRITE: / IT_RPT_LINES-NAME,
             IT_RPT_LINES-AGE,
             IT_RPT_LINES-JOB.
    FORMAT COLOR COL_KEY.
    WRITE: IT_RPT_LINES-ORDER HOTSPOT.
    FORMAT COLOR COL_BACKGROUND.
    HIDE: IT_RPT_LINES-ORDER.          "keep track of the order
  ENDLOOP.

ENDFORM.                               " WRITE_LINES

*&---------------------------------------------------------------------*
*&      Form  WRITE_HEADINGS
*&---------------------------------------------------------------------*
FORM WRITE_HEADINGS.
  FORMAT COLOR COL_HEADING.
  LOOP AT IT_RPT_HEADINGS.
    WRITE: / IT_RPT_HEADINGS-NAME HOTSPOT,
             IT_RPT_HEADINGS-AGE  HOTSPOT,
             IT_RPT_HEADINGS-JOB  HOTSPOT.
    WRITE: IT_RPT_HEADINGS-ORDER  HOTSPOT.

* keep track of the column headings for user selection
    HIDE: IT_RPT_HEADINGS-NAME,
          IT_RPT_HEADINGS-AGE,
          IT_RPT_HEADINGS-JOB,
          IT_RPT_HEADINGS-ORDER.
  ENDLOOP.

ENDFORM.                               " WRITE_HEADINGS

*---------------------------------------------------------------------*
*       FORM DISPLAY_DOCUMENT                                         *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
*  -->  P_FCODE   -- Transaction code
*  -->  P_DOCNUM  -- SD document number
*  -->  P_VBTYP   -- SD document category
*---------------------------------------------------------------------*
FORM DISPLAY_DOCUMENT USING P_FCODE
                            P_DOCNUM LIKE VBUK-VBELN
                            P_VBTYP  LIKE VBUK-VBTYP.

  CALL FUNCTION 'RV_CALL_DISPLAY_TRANSACTION'
       EXPORTING
*         BUKRS   = '    '
            FCODE   = P_FCODE
*         GJAHR   = '0000'
*         LGNUM   = '   '
*         POSNR   = '000000'
            VBELN   = P_DOCNUM
            VBTYP   = P_VBTYP
       EXCEPTIONS
            OTHERS  = 1.

ENDFORM.                               " DISPLAY_DOCUMENT
This was first published in March 2002

Dig deeper on SAP ABAP

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close