Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to display a sort indicator title in a column's header
PRODUCT: 4D View | VERSION: 2004 | PLATFORM: Mac & Win
Published On: July 17, 2008

In 4D View it may be desirable to show a sort indicator title in the column header after the header is clicked to sort the column. It would be even better if the title indicated whether the sort were ascending or descending. If the sort is done programmatically, the column title can be set with code as well.

One approach is to use the PV SORT ONE command in a callback method responding to the PV ON CLICKED event. A form checkbox (whose variable will be called check1 here) can be provided for the user to select ascending or descending. Then the SET COLUMN HEADER command can be used to set the title, using the checkbox setting and $4 (column number) event parameter, which were both also used by the PV SORT ONE call.

First make sure On Plug in Area is checked under Events in the Property List for the 4D View area. Then create an object method for the area (called area1 here), and add this line of code to set up a method named SortColumn as the event handler:

PV ON EVENT(area1;pv on clicked;"SortColumn")

And then set up the SortColumn method something like this (a hard-coded 3 column by 6 row data area used here for example purposes):

 `SortColumn method...

 `Event parameters


If($5=0)  `If row 0, i.e. column header, was clicked
$0:=True  `Don't take event into account

C_BOOLEAN(check1`Checkbox on form
If(check1)  `Descending is checked
End if

PV SORT ONE(area1;1;1;3;6;1;$4;$order)
 `Sort area from col 1, row 1 to col 3, row 6
 `Sort columns ascending, keyed to column clicked

 `First erase existing column headings
For ($i;1;3)
    PV SET COLUMN HEADER(area1;$i;" ")
End for

$title:="A -> Z"
If(check1)  `Descending checked
    $title:="Z -> A"
End if
PV SET COLUMN HEADER(area1;$4;$title)

End if

Now look at the running 4D View area on the form:

Let's try a different sort, by clicking on the column B header, without selecting the "Descending" checkbox, to sort the whole set of data by column B, ascending:

Not only did the sort work, but column B has a header title indicating an ascending sort. Let's check the "Descending" checkbox and click on the column C header:

Now the data is sorted based on column C, descending, and the column C header title so indicates. In addition, the code took care of erasing the column headings of the unselected columns. Note that " " (i.e. a space) was used in PV SET COLUMN HEADER to erase titles; "" (NULL, quotes containing nothing) will cause the title to revert to the lettered column heading, i.e. A, B, C, etc.

For more information on responding to events in 4D View, see "PV ON EVENT" in the 4D View Language Reference,