Randomize items in repeater

Hey,
I’ve seen a couple of threads sort of showing how this is done. e.g. https://www.wix.com/corvid/forum/community-discussion/randomizing-my-repeater

For whatever reason I cannot seem to get this to work. I sort of new to Wix and never used corvid before. Can anyone give me any pointers on how to get to this work please.

I have a DB called Testimonials. This has a title and description fields in. I have 6 testimonials in the DB. I have a repeater on my page which is connected to this DB and set to pull in 2 items. But none of this is using any code. I want to be able to randomize these two items from the 6 testimonials I have.

Please could someone help with a detailed implementation of how do get this working. It’s probably really simple but I cannot for the life of me get it to work.

Thanks in advance

1 Like

Just use the full code example that Yisrael has given for you and change the number from 3 to 2, then just change the repeater and dataset names to match your own used elements and dataset etc.

Hey. Thanks very much for the response. I tried this. It didn’t work.

I have the dataset connected to my repeater using the WIX editor. Should I do this?

Sorry, but it appears I need a step by step approach as I cannot seem to get it working.

@info44722
https://support.wix.com/en/article/about-datasets-6368396

So you have created your dataset and added it to your page.
https://support.wix.com/en/article/adding-and-setting-up-a-dataset

Then you need to connect it to your repeater.
https://support.wix.com/en/article/connecting-page-elements-to-a-dataset

As you don’t mention which block of code that you are actually using, then note that on Yisrael’s later post about shuffling the repeater, that he does state that you need to disconnect the dataset from the repeater.

I didn’t understand that you were trying to shuffle the results - I thought you were just trying to randomize what would be displayed.

To shuffle, you will need to disconnect the Repeater from the dataset, and add some code which will query the collection and shuffle the returned array of items.

@givemeawhisky Thanks again. Appreciate your desire to help.
I tried the following. But it doesn’t like the numberRecipes bit. I assumed that was for something else but whatever I change this too it doesn’t work.

I’ve amended the names of the repeater and data collection to my ones. Still doesn’t pull anything in.

import wixData from 'wix-data';

$w.onReady(function () {

 // 1) get a random skip number
 let max = numberRecipes - 3;
 let skip = Math.floor(Math.random() * max) + 1;

 // 2) query the database and get 3 random itmes
 wixData.query("collection")
  .ascending("title")
  .skip(skip)
  .limit(3)
  .find()
  .then( (results) => {
 let items = results.items;
 // 3) create a repeater data object from our retrieved data
 let itemData = [
      {
         "_id":items[0]._id,
         "text1":items[0].text
      },
      {
         "_id":items[1]._id,
         "text1":items[1].text
      },
      {
         "_id":items[2]._id,
         "text1":items[2].text
      }
    ];

 // 4) set the repeater data property with our data object
 $w("#repeater1").data = itemData; // add data to our repeater 
  } )
  .catch( (error) => {
 let errorMsg = error.message;
 let code = error.code;
  } );
 
 // 5) method to handle each repeated item
 $w("#repeater1").onItemReady( ($w, itemData, index) => {
 // all we do here is set the repeated image from our data
 const repeatedText = $w("#text1");
 repeatedText.text = itemData.text1;

 // you can do a whole lot more in this routine:
 // - handle other fields or elements
 // - add an onClick() handler
 // - handle selected repeat items
    });
});