Code Queen

Sep 14, 20201 min

Random Order for Repeater Items on Page Ready

About

The following code assumes your repeater is hidden on load and that you are filtering your dataset when the page loads.  Adjust the code as needed (including the element ID names).

You can see this code being used on this live page:  https://www.totallycodable.com/featured-designers


Good to Know #1

The Code


 
import wixData from 'wix-data';
 

 
$w.onReady(async function () {
 
$w("#dataset1").onReady(async () => {
 

 
$w("#dataset1").setFilter(wixData.filter()
 
.eq("designer", "designer") //Our example sets a filter before running the randomization code
 
)
 
.then((results) => {
 
$w('#repeater1').show();
 
let repeaterItems = $w('#repeater1').data;
 
$w('#repeater1').data = shuffle(repeaterItems);
 
})
 
.catch((err) => {
 
let errorMsg = err;
 
});
 

 
});
 
});
 

 
function shuffle(items) {
 
var currentIndex = items.length,
 
temporaryValue, randomIndex;
 
while (0 !== currentIndex) {
 
randomIndex = Math.floor(Math.random() * currentIndex);
 
currentIndex -= 1;
 
temporaryValue = items[currentIndex];
 
items[currentIndex] = items[randomIndex];
 
items[randomIndex] = temporaryValue;
 
}
 
return items;
 
}
 


Author

by Code Queen

Stuck on a project? Hire Code Queen, LLC!

Schedule a phone call or video call directly online. In a different time zone? No problem! Code Queen currently has clients around the world.

Online Booking: Discovery Session

Contact Form: Send project details

    2220
    7