I have a bootstrap table with a column (called: prio) where I can enter a number in a text input.
Like this:
This column should be sorted from low to high numbers, so first 1 then 2 then 3 etc. But if I sort that way, it first shows the empty cells, and at the bottom it shows 1,2,3 etc.
How can I make sure the empty cells are ignored in the sort?
I am afraid that wonât be possible.
nulls are considered lower than the rest of the numbers.
Maybe a workaround could be setting a default value for empty fields, like 999 so that they always stay at the end.
Is this not possible because of Wappler itself or does all coding with databases have the same problem?
Giving it a 999 is not ideal. My end goal is to sort the orders (=rows) that we need to process, so a human always has to give the priority to an order so that the staff knows which order to process first. Is there another way to achieve this?
When there is a new order, there is no priority assigned to the order. So that is why the fields have a null value. Because my staff should manually set the priority of that order, after itâs created.
So yes the rows with null values should be visible, otherwise no priority can be assigned.
When you have NULL values in the column and a sort is applied, the decision whether the NULLs should come first or last varies per database type.
MARIADB / MYSQL / SQLSERVER
If you specify the ORDER BY clause, NULL values by default are ordered as less than values that are not NULL. Using the ASC order, a NULL value comes first before any non-NULL value; using DESC order, the NULL comes last.
DB2 / ORACLE / POSTGRESQL
If you specify the ORDER BY clause, NULL values by default are ordered as more than values that are not NULL. Using the ASC order, a NULL value comes after any non-NULL value; using DESC order, the NULL comes first.
And yes, there are some âhacksâ for this using hand coding but, thatâs not currently possible in Wappler as it just uses what the database returns.
How about have a filter on the page greater than or equal to 1? Then have a separate view for records that do not fit that criteria? ie View âAwaiting Priorityâ for example?
No problem Nevil. You could have two separate queries with two tables on the page. One for those with a Priority value and one for those without, allowing for both circumstances. This way attention can be drawn to those records, specifically, without a Priority, and those with a Priority set. Hide either or table based on if Results exist for instance? This way if there are no records without a Priority set the table wonât be displayed, and if there are results it will be displayedâŚ
In this circumstance I usually go in to File Manager find the original query (Action file) and save it as a copy (rename it accordingly), then go in to Server Connect and adjust the filter (on the copy of the original Action), save and upload. Then use this copy with the slight variation to produce the second set of results. That way you can copy the table and paste it, change itâs Action, and everything works as it should without recreating everything again without too much time wasted.
I frequently need to manipulate the sort order - eg where certain characters should be excluded from the beginning of field. Before Wappler, I would do this by using trim and other functions in the query. This is not possible in Wappler - so I would say it is a limitation of Wappler. Using a generated field to sort on (at least in MySQL) is a good workaround.
@Dave has given you a good alternative, but if you did want a single list with NULL values coming last, this would also be possible.
There is an existing feature request which would address issues like this.
Has nothing to do with the topic and the fact how MySQL sorting handles null values, which I already explained in my last post
Not sure why you even mention something like this in this topic...
Quite often people ask a specfic question which has a more general solution or application (for this reason, you frequently change the topic title). While this question was specific, @nevil asked if a limitation in Wappler was the issue - it is, but there is a workaround which can be used to address his issue and any other issues people might have related to sorting. I considered this a potentially useful contribution.
@Dave offered an alternative approach. You explained how Nulls are treated in various databases. I gave a solution to the question. That's why I mentioned it.