Multi-column Dynamic Sort

  • Last update:  2024-02-24
  • Overview

    iconNote:
    This document is a reference for users with FineReport versions earlier than 11.0.3. Users with FineReport 11.0.3 and later versions can refer to Sort After Expansion.

    Expected Effect

    When designing a report, you may hope to sort data of multiple columns in ascending/descending order by clicking the corresponding column header. For example, you hope to sort data in the first column (ID) in ascending/descending order by clicking the column header ID and sort data in the second column (Name) in ascending/descending order by clicking the column header Name, as shown in the following figure.

     1.gif

    Implementation Method

    You can add dynamic parameters in Hyperlink and add a formula in Sort After Expansion to dynamically sort data in multiple columns. That is, when you click a data column header, the cell (where the data column is located) is passed as a parameter. Then the parameter is received by the formula in Sort After Expansion to achieve different sorts based on different parameters.

    iconNote:

    1. If data to be sorted includes character-string fields, the formula in Sort After Expansion will be different. You can refer to the two examples in this document.

    2. The implementation method is also applicable to dashboards.

    3. If too many pages or multiple tab pages (in the Tab block of a dashboard) exist in data to be dynamically sorted, the system will refresh the page and jump to the first page after you click the column header to sort data in another page, because the dynamic parameters will refresh the entire page.

    Example One

    Data Preparation

    Create a dataset named ds1 and enter the SQL query statement SELECT EMPID, EMPNAME, BIRTHDATE, HEIGHT FROM EMPLOYEE limit 10.

     2.png

    Report Design

    Drag the fields EMPID, EMPNAME, BIRTHDATE, and HEIGHT into cells (from cell A3 to cell D3), enter the corresponding column headers, and set the table style, as shown in the following figure.

     3.png

    Dynamic Parameter Adding in Hyperlink

    Select cell A2, cell C2, and cell D2 respectively, click the Hyperlink icon on the right, and add dynamic parameters. The following table describes the parameter setting of each cell.

    iconNote:
    In this example, character-string data cannot be sorted.

    Cell
    ParameterValue TypeValueDescription

    A2

    a

    Formula

    if($a==-1,1,-1)

    if($a==-1,1,-1) represents that the value of a is replaced every time you click the hyperlink. 1 represents the ascending order. -1 represents the descending order.

    The value of the parameter asc represents the cell to be sorted. Date and time data need to be set separately.

    asc

    Character String

    A3

    C2

    a

    Formula

    if($a==1,-1,1)

    asc

    Character String

    tointeger(format(C3,'yyyyMMddHHmmss'))

    Note: If time data only includes year, month, and day, the parameter will be tointeger(replace(C3,"-","")).

    D2

    a

    Formula

    if($a==1,-1,1)

    asc

    Character String

    D3

     4.png

    Sort After Expansion Setting

    Select cell A3, click the Cell Attribute icon, click Expansion, set Sort Basis to Formula and Sort Rule to Ascend, and enter the formula if($a==1,EVAL($asc),eval($asc)*-1).

    The following table describes the formula.

    Formula
    Description

    EVAL(exp)

    Returns the calculated result of the expression exp. exp: a character string in the expression form.

    $asc

    Returns the value of parameter $asc.

    if($a==1,eval($asc),eval($asc)*-1)

    If parameter a is 1, data will be sorted in ascending order based on the value of asc. If not, data will be sorted in descending order based on the value of asc.

     5.png

    iconNote:
    The subsequent cells are expanded based on the first column. Therefore, the setting of Sort After Expansion is only effective in the top-level left parent cell.

    Effect Display

    On PC

    Save the template and click Pagination Preview. The effect is shown in section "Expected Effect."

    On Mobile Terminals

    The template can be previewed on both the DataAnalyst and HTML5 apps. The effect is shown in the following figure.

     6.gif

    Example Two

    Character-string data cannot be sorted in example one. Therefore, you need to set date and time data separately. This example mainly introduces how to sort value, date, and character string data together.

    Taking the template in example one as an example, this section introduces a different formula in Sort After Expansion.

    Dynamic Parameter Adding in Hyperlink

    Add dynamic parameters in Hyperlink for cell A2, cell B2, cell C2, and cell D2. The parameter setting is as follows.

    Cell
    ParameterValue TypeValueDescription

    A2

    a

    Formula

    if($a=-1,1,-1)

    if($a=1,-1,1) represents that the value of a is replaced every time you click the hyperlink. 1 represents the ascending order. -1 represents the descending order.

    The value of the parameter asc represents the cell to be sorted.

    asc

    Character String

    A3

    B2

    a

    Formula

    if($a=1,-1,1)

    asc

    Character String

    B3

    C2

    a

    Formula

    if($a=1,-1,1)

    asc

    Character String

    C3

    D2

    a

    Formula

    if($a=1,-1,1)

    asc

    Character String

    D3

    Sort After Expansion Setting

    Select cell A3 and modify the formula in Sort After Expansion into INARRAY(EVAL($asc),SORTARRAY(EVAL($asc+"[!0]")))*$a.

    The following table describes the formula.

    Formula
    Description

    EVAL(exp)

    Returns the calculated result of the expression exp. exp: a character string in the expression form.

    $asc+"[!0]"

    Returns the concatenated characters. If the value of the parameter $asc is A3, A3[!0] will be returned. That is, all the values expanded from cell A3 are returned.

    SORTARRAY(array)

    Returns the sorted array array.

    Note: If the type of array elements is inconsistent or array elements cannot be compared, the original array will be returned.

    INARRAY(co,array)

    Returns the position of co in the array array, if co is not in array, 0 will be returned.

    INARRAY(EVAL($asc),SORTARRAY(EVAL($asc+"[!0]")))*$a

    Sorts all the obtained values expanded from cells through SORTARRAY, returns the position of the value of the current cell in all sorted values through INARRAY, and multiplies the result (obtained from the above two formulas) by the parameter $a to sort data in ascending/descending order.

    iconNote:
    If empty values exist in the data column, the sort of decimal/integer data may fail. You need to perform filtering before sorting.

     7.png

    Effect Display

    On PC

    Save the template and click Pagination Preview. The preview effect is shown in the following figure.

     8.gif

    On Mobile Terminals

    The template can be previewed on both the DataAnalyst and HTML5 apps. The effect is shown in the following figure.

     9.gif

    Template Download

    You can download the example templates Multi-column Dynamic Sort 1.cptMulti-column Dynamic Sort 2.cpt.


    Attachment List


    Theme: Report Application
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy