I have a repeater that is connected to a dataset. The dataset is told to sort by a numerical ranking (high to low). When I preview, it works great. When I go live, I can see that it loads correctly, and then reverts to sorting by date (newest to oldest). Any idea why the Sort Function is getting overwritten?
Not sure, but in most cases there are always the same issues/reasons, why the PREVIEW differs from LIVE-VIEW.
- DATABASE-PERMISSIONS
- SYNCING
- DATASET-SETUP (READ-WRITE)
and probably even some more…
Please share your code, nicely formatted in a code block, so that others can assist.
-
Permissions are set to “Anyone”
-
I synced Sandbox to Live
-
The database is set up as “Read & Write”.
Here is the code:
import wixData from 'wix-data';
const collectionName = 'Projects';
const fieldToFilterByInCollection = 'tags';
$w.onReady(function () {
setRepeatedItemsInRepeater(100);
loadDataToRepeater(100);
$w('#selectionTags1').onChange((event) => {
const selectedTags = $w('#selectionTags1').value;
loadDataToRepeater(selectedTags);
})
});
function loadDataToRepeater(selectedCategories = []) {
let dataQuery = wixData.query(collectionName);
if (selectedCategories.length > 0) {
dataQuery = dataQuery.hasSome(fieldToFilterByInCollection, selectedCategories);
}
dataQuery
.find()
.then(results => {
const itemsReadyForRepeater = results.items;
$w('#repeater1').data = itemsReadyForRepeater;
const isRepeaterEmpty = itemsReadyForRepeater.length === 0
if (isRepeaterEmpty) {
$w('#container1').show();
} else {
$w('#container1').show();
}
})
}
function setRepeatedItemsInRepeater() {
$w('#repeater1').onItemReady(($item, itemData) => {
$item('#container1').src = itemData.projectimage;
$item('#container1').tooltip = itemData.projectimage;
})
}
Well, you may have your Repeater connected to a Dataset, but you are doing a query with the wix-data API and then using those results to set the Repeater data.
You need to use only one - dataset or wix-data query - not both.
If you want to sort the results using a wix-data query, then add ascending() to the query.
Thanks for the message. Is there a way to make it so that when you click one of the selection tags buttons, it keeps the sort priority? It is only sorting on the first page load, and then reverts to New->Old once a selection tag is clicked.
Hi Yisrael- I believe it is all working correctly now with the following. Do you see any problems with this code?
import wixData from 'wix-data';
const collectionName = 'Projects';
const fieldToFilterByInCollection = 'tags';
$w.onReady(function () {
$w('#selectionTags1').onChange((event) => {
const selectedTags = $w('#selectionTags1').value;
loadDataToRepeater(selectedTags);
})
});
function loadDataToRepeater(selectedCategories = []) {
let dataQuery = wixData.query(collectionName);
if (selectedCategories.length > 0) {
dataQuery = dataQuery.hasSome(fieldToFilterByInCollection, selectedCategories);
}
dataQuery
.descending("sortOrder")
.find()
.then(results => {
const itemsReadyForRepeater = results.items;
$w('#repeater1').data = itemsReadyForRepeater;
const isRepeaterEmpty = itemsReadyForRepeater.length === 0
if (isRepeaterEmpty) {
$w('#container1').show();
} else {
$w('#container1').show();
}
})
}
function setRepeatedItemsInRepeater() {
$w('#repeater1').onItemReady(($item, itemData) => {
$item('#container1').src = itemData.projectimage;
$item('#container1').tooltip = itemData.projectimage;
})
}
I guess if it works, there isn’t a problem.
However… I don’t see setRepeatedItemsInRepeater() being called from anywhere.