top of page
Anchor 1
Last Updated on :
Friday, 28 January 2022
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).
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;
}
bottom of page