Linking search results to Dynamic Pages

Hello again :slight_smile:
I am currently trying to get my search results, which are shown in a repeater, to be linked to Dynamic Pages but I am struggling right now :frowning:

As a background it is a railway themed website which details data regarding locomotives. The search result page is connected to a dataset (‘#shunters’) and has multiple fields per item but it is the text box relating to the “number” field that I would want to be able to click on to redirect to a Dynamic Page which offers more detail about that particular locomotive.

In terms of the Dynamic Pages, these are currently split into individual pages separated by the classes of locomotive with each Dynamic Page having it’s own dataset.

I have tried the following code:

function linktolocolog () {
  $w("#repeater1").onItemReady( ($item, itemData, index) => {
 $item("#text95").onClick( (event) => {

   $w('#Shunters').onReady(() => {
 let $item =  $w.at(event.context);
 let currentItem = $item('#Shunters').getCurrentItem();
 let dynamicPageID = `${currentItem.number}`
 let collectionID = `${currentItem.title}`

   console.log('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);

 wixLocation.to('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);
 
 }); 
})
})
}

And this yields a result in that the console log


But doesn’t actually then do anything?!?
The target address is correct (if you type that address into your address bar, you’ll be directed to the page) but no redirect is happening when I click on the relevant text box.

Could anyone point me in the right direction please?

Thanks a lot

I’ve tried the following variations with no joy :frowning:

function linktolocolog () {
$w('#Shunters').onReady(() => {
  $w("#container2").onClick( (event) => {
 
 let $item =  $w.at(event.context);
 let currentItem = $item('#Shunters').getCurrentItem();
 let dynamicPageID = `${currentItem.number}`
 let collectionID = `${currentItem.title}`
   console.log('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);
 wixLocation.to('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);
 }); 
})
}
function linktolocolog () {
  $w("#container2").onClick( (event) => {
     $w('#Shunters').onReady(() => {
 let $item =  $w.at(event.context);
 let currentItem = $item('#Shunters').getCurrentItem();
 let dynamicPageID = `${currentItem.number}`
 let collectionID = `${currentItem.title}`
   console.log('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);
 wixLocation.to('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);
 }); 
})
}

I’ve also changed the container 2 part to the specific text box within the container which relates to the number field.

All of these have resulted in the same outcome - the console log recording the correct url, but there being no actual redirection.

If anyone can give me some pointers as to where I am going wrong with this, it would be massively appreciated :slight_smile:

Thanks

Ok so it appears that I was right with my 2nd attempts.
With this code:

functionlinktolocolog(){
$w('#Shunters').onReady(()=>{
$w("#container2").onClick((event)=>{
let $item =  $w.at(event.context);
let currentItem =$item('#Shunters').getCurrentItem();
let dynamicPageID =`${currentItem.number}`
let collectionID =`${currentItem.title}`    console.log('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);  wixLocation.to('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);});})}

the link from my search result repeater to my dynamic pages works in live mode (but not in preview mode which is where I was trying it previously).

So that solves that issue but leads me to another…
My search results repeater is connected to my (‘#Shunters’) dataset and my Dynamic Pages are connected to my (‘#LocoLogs’) dataset. The Dynamic Pages ID’s are set from the ‘Number’ field which appears in both datasets. However, I have 50 separate numbers in the (‘#Shunters’) database but only 20 of these numbers appear in the (‘#LocoLogs’) database. So with the above code, if I click on a number which doesn’t have a Dynamic Page, it still tries to redirect which obviously leads to a ‘404 There’s Nothing Here’ outcome.

Would it be possible to remove the ability to click on any item where the number doesn’t have a Dynamic Page, leaving just those which have a live Dynamic Page clickable?

Thanks

After my previous post, I have been trying to solve the issue of having results which don’t have a dynamic page not being clickable.

Having browsed the web, I have come up with the below:

function linktolocolog () {
$w('#Shunters').onReady(() => {
  wixData.query('Shunters')
    .eq('number', number1)
    .find()
    .then(res => {
 const number1 = res.number1;
    })
    .catch ((err) => {
 let errorMsg = err;
    });
    wixData.query('LocoData')
    .eq('number', number2)
    .find()
    .then(res => {
 const number2 = res.number2;
    })
    .catch ( (err) => {
 let errorMsg = err;
    });

 if (number1 === number2){
$w("#container2").onClick( (event) => {
 
 

 let $item =  $w.at(event.context);
 let currentItem = $item('#Shunters').getCurrentItem();
 let dynamicPageID = `${currentItem.number}`
 let collectionID = `${currentItem.title}`
   console.log('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);
 wixLocation.to('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);
 })
}
})
}

It doesn’t work but thought I would post in here to see if there is any kind soul out there who could, at the very least, tell me if I am slowly heading in the right direction with the additions I have made in this code?

Thanks

Site URL helps! Can you provide the page url having these codes?

Thanks for your response!

The page url where the above code is placed is:

https://www.ukraillog.co.uk/loco-results2

This results page is accessible via https://www.ukraillog.co.uk/copy-of-home using the search function at the bottom. If you use this page for any reason the best search for you to use is “90” in the class field.

Please note that these pages are a copy of the live pages on my website which I am using as test pages. Therefore, all buttons and menus will redirect you to live pages which currently have no bearing on this code.

Hope this is what you are looking for but if you need any more info then feel free to ask :slight_smile:

Thanks