Uncategorized

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...

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).

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

Corvid , Wix Design, Project Planning, Private Tutoring

Services world wide

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.

Share this post

Loading...