I wish to have a button connect to a data set and pull a line of text or a hyper link on click.
To ensure that the user experience is unique, is it possible to have the button randomly select from a large data set of info? All the tutorials I have looked at so far have been helpful, but seem to only have the button select the next or the previous item as it was input into the data base.
Is there a math function that could take the assigned numbers for each entry in the database and randomize which entry is selected?
Thanks in advance!
Dan
Hi Dan,
Welcome to the Wix Code forums.
You can do something like this:
import wixData from 'wix-data';
// clear any filters in the dataset
$w("#dataset1").setFilter( wixData.filter() );
// get size of collection that is connected to the dataset
let count = $w("#dataset1").getTotalCount();
// get random number using the size as the maximum
let idx = Math.floor(Math.random() * count);
// set the current item index of the dataset
$w("#dataset1").setCurrentItemIndex(idx);
I hope this helps. It should at least get you started in the right direction.
Good luck,
Yisrael
Thank you so much! I will try this out and see how I might extrapolate on this in the future
Is there something that needs to go after this code? I am linking a button (on a page) to a Lightbox which is supposed to show a different message every time (from one of my dataset columns). I have this (see photo) for Lightbox code and it is still just showing one item every time I click open the Lightbox. I can only post one pic but I can show you base page code too if needed.
I gave up. Lightbox won’t work. Neither will dynamic page with single item. Button won’t randomize item showing in text field. Nothing will. Hate Wix now. Building my own site on GoDaddy like I used to do where html-css-java are straightforward and simple.
Your page onReady() function really doesn’t do anything. Just referencing a component such as $w(“#page1”) does not execute an code.
Did you set the dataset’s onReady property, for example:
has anyone had any luck with this code? It isn’t randomising the items in my dataset display.
Not sure what code I am missing as each step is here, setting filter > getting the number of items in database > get a random item from the dataset > set that item on load from the dataset
export function dataset1_ready() {
$w("#dataset1").setFilter(wixData.filter() );
let count = $w("#dataset1").getTotalCount();
let idx = Math.floor(Math.random() * count);
$w("#dataset1").setCurrentItemIndex(idx);
}
This post is from 2018 and being closed.
Please add a new post with a link to refer back to this post if more help is needed.
Search the forum as there has also been more posts on this before and since 2018.
https://www.wix.com/corvid/forum/community-discussion/can-we-randomize-database-info-in-a-repeating-layout
https://www.wix.com/corvid/forum/community-discussion/how-to-make-repeater-show-randomized-items-from-dataset
https://www.wix.com/corvid/forum/community-discussion/random-sorting-of-filtered-gallery
https://www.wix.com/corvid/forum/community-discussion/how-to-pull-random-item-from-database
https://www.wix.com/corvid/forum/community-discussion/choose-random-data-from-database