I’m using a repeater on a page to display various businesses depending upon certain criteria keyed in by the user.
I’m doing this by using the following command;
$w(‘#repeater3’).data = displayArray;
The page starts out with the repeater showing the data from the collection called Local_Businesses, using Dataset1.
How do I get the repeater source data back to the original data source of the actual collection, Local_Businesses??
Thanks!
You can get all your data using:
import wixData from 'wix-data';
$w.onReady( async() => {
let data = await wixData.query("myCollection").find()
$w("#repeater3").data = data
}
Thank you Bruno - I found this on the Velo site just after as you posted the answer too! lol
I saw that I could also assign the data as data.items which is exactly what I needed so thank you for your answer!
I used this code in the end and also found a code example to make sure the array items were sorted by the field “title” - this worked brilliantly for me…
wixData . query ( “LocalBusinesses” )
. find ()
. then (( results ) => {
// Create the originalArray results of items
originalArray = results . items ;
// Sort the originalArray by title field
originalArray . sort ( function ( a , b ) {
var x = a . title . toLowerCase (); // ignore upper and lowercase
var y = b . title . toLowerCase (); // ignore upper and lowercase
if ( x < y ) {
return - 1 ;
}
if ( x > y ) {
return 1 ;
}
// names must be equal
return 0 ;
});
Just a related question please…
Do you use any particular editor to create your Javascript or is there any editor that you could recommend?
I use VS Code with a bunch of extensions, including one Velo extension. When the code is ready, I copy and paste to the Wix Editor X.
@paulgoodchild nice, just a small tip: you can make this compare function WAY shorter using arrow functions and .localeCompare() method. Try this:
originalArray.sort((a, b) => a.title.localeCompare(b.title))