Dynamic Sort Options - doesn't seem to work

It may well be that I don’t understand how this is supposed to work - or it may be a bug.

On the page concerned, I’ve added a select list with sort options. I would typically need to add quite a number of options, but I’ve kept this very simple and am just sorting by a single field, with options to sort ascending or descending. In the query builder I’ve added the two conditions:

However the query ignores these conditions and sorts by both criteria (which of course is not very useful). Here’s the query which is executed:
SELECT subject FROM subjectlist WHERE (subject LIKE CONCAT(’%’, ‘’, ‘%’) ESCAPE ‘!’) ORDER BY subject ASC, subject DESC LIMIT 10

The action file includes these two sort criteria in the ORDER BY clause (which seems strange), but also includes the conditions:

I may not be using this feature correctly, but this seems odd. It required a lot of fiddling around to get this functionality in the pre-Wappler version of Server Connect. This new version looks like a great improvement - if it works as I think it’s meant to.

You conditions are wrong - the whole expressions should be enclosed with {{ }} not just the first part.
So like {{$_GET.sort == 'asc'}}

That said you don’t need conditions to do sorting. Just supply the sorting parameter as option in the main query builder options:

image

That should do it

One more tip - the columns you are sorting on should be really present in the SELECT as this is used for validation.

Thanks George. I see my expression was wrong and I’ve corrected this. Now no ORDER BY clause is included when the query is executed. I’ve probably done something wrong in App Connect - I’ll investigate.

I noticed the query parameters appear by default now - as in your screenshot. Do you mean I could just pass the ORDER BY field names and asc/desc directly? If so, I can see that would be useful. However I would usually include the options in a single select list like this:

image

I imagine I could do this using the Query Builder sort conditions. Would that be the best approach?

well it seems we have a tutorial for this :slight_smile:

https://wappler.io/docs/#Dynamic-Sortable-Table

Yes, I saw this. The new table generator looks very useful, but it doesn’t cover the new Query Builder Options (using sort conditions). Also, I’m not using a table and want a single select for sorting. Basically I want something like this:


(from a site I did without DMX/Wappler)

It’s all working with Wappler, except the sort options. The sort conditions look as if they would do what I want. Is this correct?

aha with a single dropdown field, normally you have separated field s but ok - it should work but then you need indeed to pass both values the sort field and the order at the same time. Or use a single field and then conditions indeed as you started - I wasn’t aware that you wanted to achieve this.

Great - it looked as if sort conditions is what I wanted (and what I had wished for in the past).

Using the old extensions, I used a single field and appended, -asc / -desc, then split the sort field and direction parameters:
dmx-bind:sortdir=“query.sortorder.split(’-’)[1]”

I imagine I could do something similar again.

If you want to use the query properties, then maybe somthing like

Sort: {{$_GET.sortorder.split('-')[0]}}
Direction: {{$_GET.sortorder.split('-')[1]}}

If using the sorting tab something like

Column Order Condition
Author ASC {{$_GET.sortorder == ‘subject-asc’}}
Author DESC {{$_GET.sortorder == ‘author-desc’}}
Price ASC {{$_GET.sortorder == ‘price-asc’}}
Price DESC {{$_GET.sortorder == ‘price-desc’}}
Date ASC {{$_GET.sortorder == ‘date-asc’}}
Date DESC {{$_GET.sortorder == ‘date-desc’}}
Listed DESC {{$_GET.sortorder == ‘recent’}}
1 Like

Great. That’s just how I hoped the condition feature would work. I’ll try it out.

I am hoping to add toggle buttons to my app that when clicked will sort the data from a custom query in different ways. This data is being used in a graph though. If I try to follow the tutorial for dynamic sortable tables will this work does anyone know?

I don’t think it’s possible to sort the data in custom queries dynamically, in the same way as you can using the query builder. This is not apparent when you set up custom queries, but using variables for the ORDER BY clause won’t work. I think this is to do with PDO and prepared statements rather than Wappler.

I hope I’m wrong about this, and there is some way round the issue, as I shall need to find a solution for this too.

1 Like