I have a perspective table which populates data via a binding with a transform script. When I set up the sorting parameters, I can manipulate the table into sorting itself into any order I wish, and it works in the designer in preview mode. However in a web browser session, when the table loads it loads with no sorting, and just has the rows populated in the order they appear in the data array. What could be the cause?
Check that you have all the settings correct as shown in sortOrder in Perspective - Table | Ignition User Manual.
The default weighted order in which columns and their contents are sorted relative to other columns and their contents. Used when the component loads. For sortOrder to be applied, the table must meet the following requirements:
- Objects under the columns array must be defined for each column in the table's underlying data property you wish to display and sort on.
- In addition, each object under columns must have the field setting set to the data item under the data property (for example, "population" in the table's default data set).
- sortable must be enabled.
- sort must be set to something other than none.
Once all columns have been configured, the sortOrder can be configured.Each element in the sortOrder array is expected to be a string value representing the name of the column (as determined by field value in the columns array). For example, sorting by population first, city second, and country last, would look like the following:
Go through the four bullet points carefully.
Thanks, I’ve gone through those four points carefully and can confirm that it behaves as expected when previewing in the designer. If I set the sortOrder to 0: name, 1: code
then hit preview, it sorts the data that way. If I stop preview, and change the sortOrder to 0: code, 1: name
and then click preview again, it re-sorts in the expected order. If I change the sort setting on the top priority sort column from ascending to descending and click preview, the data sorts itself in descending order instead of ascending. It all works exactly as expected when I preview in the designer, but in a session, it just loads them in the order the data appears in the table data binding. I can use the sort controls to sort afterward, but when it first loads, it does not sort.
Could this maybe be something to do with the fact that in the designer, the binding has already executed and loaded the data into the table when I click preview, so the sorting is acting on data that’s already there? Whereas at runtime, perhaps it tries to sort the data before the binding is finalised, so then the “new” data is just added in unsorted?
Yes, that sounds likely. I wonder can you set the sortOrder by script after the binding has loaded the data? I think I've seen post about detecting the end of the data binding update but can't find them. Perhaps an onChange event on data
?
The other option, of course, is to sort in your SQL query.
That’s an idea. In this case my data comes not from a SQL query but from a script transform that is reading data from multiple places and assembling it into a table (yes, I know that a database is the nice way to do this, but this is just an edge project and I can’t guarantee connectivity to the database).
But that being the case, it makes your suggestion even easier - just set the sortOrder at the end of the script. Let me give that a try…
Unfortunately that doesn’t seem to work either.
Looks like sorting the data before I load it into the table is my best option. In consultation with my friendly AI assistant, I’ve found that I can add the following line to my script just before I return data
to sort it into the desired order:
data.sort(key=lambda x: str(x["product"]).lower())
In this case the column I want to sort by is product
and I added the lower()
to make the sort case insensitive.