Hi, I’m a bloody beginner with JavaScript and I just wrote my first working snippet, but it seems to run quite slow. The mouseIn function takes 1-2 sec. to show the text in the text box. The mouseOut function works without a delay.
What I want to do:
I have a map with around 30 buttons (circles). Each button indicates a certain type of terrain. When the user hovers a cercle, it gets coloured and the title and description for this point will be loaded and displayed in two text elements on the page. When the user leaves the circle, the information will be cleared.
Hi Urs,
I like your thoughts,
I would approach like this:
import wixData from 'wix-data';
let mapInfos;
$w.onReady(function () {
getMapsInfos();
});
export function getMapsInfos() {
wixData.query("Terrain")
.find()
.then((resultMapInfos) => {
mapInfos = resultMapInfos;
})
.catch((err) => {
let errorMsg = err;
})
}
And for the mouseIn:
export fuction button2_mouseIn(event, $w) {
const buttonID = event.target.id; // don't forget to give the buttons the same name stating from zero (button0, button1 etc).
const buttonNumber = buttonID.slice(6);
$w('#title').text = mapInfos[buttonNumber].title;
$w('#description').text = mapInfos[buttonNumber].description;
}
Hi Roi,
Thank you for your speedy answer!
I’d like to understand what you did. You placed the query to be executed as soon as the page is loaded and the query loads the whole table “Terrain” (I think like a tmp-table in SQL?). This information keeps available until the user changes the page.
Doing this prevent us of querying the DataCollection on each mouse-over. Right?
I implemented the code, renamed the test-buttons to button0 and button1 and did the same within the button-field in the DataCollection. I also renamed the text fields on the page to #title and #description.
I checked the content of mapInfos on the console before I moved the mouse to button0. It was loaded correctly.
On mouseover at button0, I got the following error msg: Uncaught TypeError: Cannot read property ‘title’ of undefined
Could you imagine the reason for that?
Urs
The current code is: import wixData from ‘wix-data’; let mapInfos; $w.onReady(function () { getMapsInfos(); });
export function button0_mouseIn(event, $w) { const buttonID = event.target.id; // don’t forget to give the buttons the same name stating from zero (button0, button1 etc). console.log(buttonID); const buttonNumber = buttonID.slice(6); console.log(buttonNumber); $w(‘#title’).text = mapInfos[buttonNumber].title; $w(‘#description’).text = mapInfos[buttonNumber].description; }