Hi, I am trying sort a table of results and not having much luck.
So, I have a table of results that are created by looking up the records in my collection based on the output of a dropdown. So if I pick ‘New York’ in the drop down, all records with New York as the location are retrieved and displayed in a table. So far, so good.
But I need to also order the results in the table too - and that is where I am struggling.
It is slightly complicated because the ordering needs to differ based on the selection of the dropdown box. So, if the dropdown was ‘New York’, I want to go to a column called z_nyc and sort by that column. If the dropdown was Los Angeles, I want to order the results by the column z_la.
I am struggling to figure out how to do this. I thought maybe I need to query another collection to look up a string of the column name (i.e. z_la), and use that as the ‘sort’. But after a few goes at that approach I’ve realised I really need to ask for help. I have no development background and this is day3 of wix coding for me so feel free to tell me this is totally the wrong approach!
So here goes - can anyone suggest an approach, and, ideally adjust my code to help?
Here is what I’ve done so far. There’s no error in the wix console, but I need to // out the ascending line or I get no table.
export function indexTownDropDown_change(event, $w) {
//Sets up the variable for the later sorting
var mysortstring
//Runs the query to populate the variable with the name of the column
wixData.query(“Town_Distance_Lookup”)
// Query the collection for any items whose “Name” field contains
// the value the user selected in the dropdown
// note there will only ever be one row that matches
.contains(“Title”, $w(“#indexTownDropDown”).value)
.find() // Run the query
.then(res => {
// Create an array object called items to be the results of the query
//tempLookup is the column that holds the string of data that like z_la that needs to become my sort order
let items = res.items
mysortstring = items.tempLookup
})
// Now run my second query on another collection to
// retrieve a subset of records and sort them by the looked up string variable
wixData.query(“Activities”)
// Query the collection for any items whose “Name” field contains
// the value the user selected in the dropdown
.contains(“location”, $w(“#indexTownDropDown”).value)
.ascending(mysortstring)
.find() // Run the query
.then(res => {
// Set the table data to be the results of the query
$w(“#resultsTable1”).rows = res.items;
$w(“#resultsTable1”).expand();
});
}