Hi,
I have been trying to solve this for the past few hours but have come to a point where i seem to be going around in circles, I have tried a variety of different options but this seems to be the cleanest code without any errors.
I'm trying to take the id of the course from the dynamic page, use this on the lightbox to populate the relevant text and image for that particular course.
There are no errors on the console log, but the fields don't seem to populate.
Dynamic page code:
export function gradesButton_click(event) {
let courseId = $w('#dynamicDataset').getCurrentItem()._id;
wixWindow.openLightbox('Grades', {id: courseId});
local.setItem("courseId", courseId);
console.log(courseId)
}
Lightbox code:
$w.onReady(function () {
let courseId = local.getItem('courseId');
console.log(courseId)
wixData.query("Courses")
.eq("_id", courseId)
.find()
.then(results => {
$w("#gradesImage").src = results.grade();
$w("#gradesText").text = results.pageTitle();
$w("#gradesDescription").text = results.gradeDescrition()
});
});
Thank you once again!
The results of a query (in your Lightbox) are returned as an array in the results. Like this:
let items = results.items; //see item below let firstItem = items[0];
Also, the collection fields are not functions, you should access your fields like this:
$w("#gradesImage").src = firstItem.grade; $w("#gradesText").text = firstItem.pageTitle; $w("#gradesDescription").text = firstItem.gradeDescrition;
Hey guys, I have researched similar function for some while but cant get an answer. See i want to make similar thing, but with a REPEATER. so i have a repeater with image and tittle, seted to a dataset, all working great. the data contains "title", "image" and "text" fields. what i want to do is, when i click the image in one of the repeaters cell, a lightbox opens with the same image, title and load the text field that is a description of the image. I can't get this to work, however I try. I'm quite new in coding wix.
note: my repeater is populated via wix system, not code. do i need to popilate it via code in order to the lightbox work? I will be most grateful if someone could help me, deadline is verry near D='
@Yisrael (Wix) @Andreas Kviby
Thank you, working great! The code for anyone else if it helps.
import wixData from 'wix-data'; import {lightbox} from "wix-window"; import {local} from 'wix-storage'; $w.onReady(function () { let courseId = local.getItem('courseId'); console.log(courseId) wixData.query("Courses") .eq("_id", courseId) .find() .then(results => { let items = results.items; let firstItem = items[0]; $w("#gradesImage").src = firstItem.grade; $w("#gradesText").text = firstItem.pageTitle; $w("#gradesDescription").html = firstItem.gradeDescrition; }); });
When you use let courseId = $w('#dynamicDataset').getCurrentItem()._id; you must wrap that inside an onReady function for the dataset itself. I guess your dataset is not ready so the id is not available because you are not waiting for the getCurrentItem() to return stuff and if the dataset is not ready that will return nothing.
https://www.wix.com/code/reference/wix-dataset.Dataset.html#onReady