I am using a dynamic page with datasets to create a custom services page using the Wix bookings app. The only thing that is not working is the order the services appear in preview. I would like to sort the service items in the repeater by the Service Name (serviceName). I tried using the dataset sort feature but that did not work. This is the code I have…It seems like it should be a fairly straight forward simple solution, but I am new to Wix and I have searched extensively for an example and nothing I have found works. Any help would be greatly appreciated.
import wixData from ‘wix-data’ ;
$w.onReady( function () {
wixData.query( “Bookings/Services” )
.ascending( “serviceName” )
.limit( 100 )
.find() //TODO: write your page related code here…
});
I would expect the result to be Service 1, Service 2, Service 3, but that is not the order on the preview.
Hello Jessica,
i think it might have to do because of the same name + number text.
if you use different text it should work. (I think)
what i did (becous i needed another sorting way) is add a new row to the database named “Nr” and added 1,2,3 depending on the order i want
and then i sort on those.
greets
Kristof.
Thank you for taking a look and your response.
That makes sense and was thinking that would be a good solution, but the database I am working with is from the Wix Booking App and I am not able to add any fields to it. When I tried the sort feature, to sort the dataset, I only had the option to sort by slug. I have tried to rename to get a slug that I could sort alphabetically, but that did nothing.
Then i can sadly enough not help any more.
i never worked with Wix Booking App so i have no knowledge of how it works.
Grtz
Kristof.
I’m having the exact same issue. Have you found a solution yet?
I have the same issue here. The Bookings/Services dataset is only sorted by _id. Any updates?
I have found a solution.
Create a function
function dynamicSort(property) {
var sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a,b) {
/* next line works with strings and numbers,
* and you may want to customize it to your needs
*/var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
return result * sortOrder;
}
}
Now to use this function
var People = [
{Name: "Name", Surname: "Surname"},
{Name:"AAA", Surname:"ZZZ"},
{Name: "Name", Surname: "AAA"}
];
People.sort(dynamicSort("Name"));
People.sort(dynamicSort("Surname"));
People.sort(dynamicSort("-Surname"));