Handling sort in tableview

I noticed that the onSortCmd can be used by clicking on the column header in a tableview list.

If I’m able to retrieve from which field I clicked,
it’s possible to do custom sorting or other fun stuff with it!

My question therefore is: how to retrieve the field/element name by clicking on the column header?

I tried var vElement = application.getMethodTriggerElementName(),
but that didn’t do the trick…

Not, there is no event for that. Maybe worth a feature request.

This is 3.5 right?

Doesn’t the onsort Cmd method get passed soem arguments on which you can figure out which column (or related dataprovider) is clicked?

Paul

pbakker:
This is 3.5 right?
Doesn’t the onsort Cmd method get passed soem arguments on which you can figure out which column (or related dataprovider) is clicked?

That’s true Paul, thanks!
We’re talking about Servoy 3.5 here, indeed.
The onSortCmd returns in Servoy 3.5 the fieldname that you’ve clicked on AND and the sort order!
For example:

//onSortCmd method
var vFieldName = arguments[0]
var vSort = arguments[1]
plugins.dialogs.showInfoDialog( 'Info', 'Sorted by ' + vFieldName + '.', 'OK')

vSort returns true or false. I guess that stands for ascending or descending.
Very cool! 8)

yes, but can we overrule the sorting?

We like to sort a column in a table-view based on another column!

example:

We have a calculation that translates todays date to: Today
the date of yesterday, to: Yesterday, etc…etc…
but if someone clicks the column we don’t want to sort on that text, but on the REAL date column.

Can this be done in 3.5?

HJK:
yes, but can we overrule the sorting?
We like to sort a column in a table-view based on another column!

Right to the point, Harjo! I tried to overrule the sort by doing the method below, but it doesn’t seem to run the calculated sort afterwards…

var vForm = application.getMethodTriggerFormName()
var vFieldName = arguments[0]
var vSort = arguments[1]

var vSortString = 'asc'
if(vSort == false) {
	var vSortString = 'desc'
}

var vEsc = "'"
var vString = vEsc + vFieldName + " " + vSortString + vEsc
application.output(vString)

forms[vForm].controller.sort(vString)

Suggestions are more then welcome :wink: !

Yes Harjo, with this new functionality you can overrule the standard column sort normally fired by a click on a tableview column header.

Karel, it works fine for me, I think the problem is in your method: Why are you placing vEsc in front and behind the sort criteria?

Paul

pbakker:
Karel, it works fine for me, I think the problem is in your method: Why are you placing vEsc in front and behind the sort criteria?

You’re right, Paul, thanks. The vEsc doesn’t need to be in there. Without the quotes the method runs perfectly :)

So this is the way to run a custom onSortCmd Method inServoy 3.5:

//get form-, field and sort values
var vForm = application.getMethodTriggerFormName()
var vFieldName = arguments[0]
var vSort = arguments[1]

//convert sort boolean to string
var vSortString = 'asc'
if(vSort == false) {
   var vSortString = 'desc'
}

//set your customized field name
if(vFieldName == 'name_display') {
   vFieldName = 'name_last'
}

var vString = vFieldName + " " + vSortString

//sort the foundset
forms[vForm].controller.sort(vString)

BTW, did you notice that it’s now also possible to apply a scripted sort when a column header of an unstored calculation is clicked?

Even the click on a tableheader of a column displaying an unstored calculation is ported via the onSortCmd command.

Paul

pbakker:
BTW, did you notice that it’s now also possible to apply a scripted sort when a column header of an unstored calculation is clicked?

Even the click on a tableheader of a column displaying an unstored calculation is ported via the onSortCmd command.

Paul

Very cool! I also think it’s a great option to PREVENT a user from sorting a column.

Only missing thing (concerning my tableview wishlist :wink: ) is being able to store/restore the colomn location in tableview. That way we could save the location of the field when the user dragges a fieldrow to another location.