Hi All,
Was hoping someone could help me out with a piece of code that works on one page but doesn’t work on the other.
I have a repeater on a search page (below) that randomizes data from my dataset. It works fine. However, I’m also trying to do this on my homepage (second image) but I can’t get it to randomize and for the life of me can’t figure out why. Thank you very much for the help!
search page, randomized repeater works
import wixLocation from 'wix-location';
import { local } from 'wix-storage';
import wixData from 'wix-data';
$w.onReady(function () {
var sameWord = local.getItem("searchWord");
$w("#searchBar").value = sameWord;
$w("#searchBar").placeholder = sameWord;
$w('#menuSearch').onReady(function () {
search();
//repeater with randomized data, works fine.
let data = $w("#repeater2").data;
data = shuffle(data);
$w("#repeater2").data = data;
});
});
export function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
export function searchButton_click() {
search();
}
export function searchBar_keyPress(event) {
if (event.key === "Enter") {
searchButton_click();
} else {
//
}
}
function search() {
wixData.query('Items')
.contains('rName', $w("#searchBar").value)
.or(wixData.query('Items').contains('description', $w("#searchBar").value))
.find()
.then(res => {
$w('#repeater1').data = res.items;
});
}
Home page, same code, doesn’t work.
import wixUsers from 'wix-users';
import { local } from 'wix-storage';
import wixLocation from 'wix-location';
import wixData from 'wix-data';
$w.onReady(function () {
if (wixUsers.currentUser.loggedIn) {
$w("#menuButtonLoggedIn").show();
$w("#signUpNowText").hide();
} else {
$w("#menuButtonLoggedIn").hide();
$w("#signUpNowText").show();
}
// **
//can't get this to work
let data = $w("#repeater1").data;
data = shuffle(data);
$w("#repeater1").data = data;
});
export function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
//**
export function searchButton_click(event) {
let word = $w("#searchBar").value;
local.setItem("searchWord", word);
wixLocation.to('/search-results')
}
export function searchBar_keyPress(event) {
if (event.key === "Enter") {
searchButton_click();
} else {
//
}
}