Next and Previous button going to a different database

Hello.

I have my Next and Previou s buttons working with code, but I have noticed that when I create a new database and I try to use the same code to have my buttons going to the next item it will take me to a different database.

There is anyway to keep it from doing that? I just want to see the Next and previous items in the same collection.

I have 4 dynamic pages, the first 2 I created a few months ago are working fine. but the 2 I created recently I am having the problem.

The code I am using I made it following this tutorial: Velo Tutorial: Creating Previous and Next Buttons for a Dynamic Item Page with Code | Help Center | Wix.com

PS. “Anterior” = “Previous” and “Siguiente” = “Next”
Code in Item page:

import {local} from 'wix-storage';
import wixLocation from 'wix-location';

$w.onReady(function () {
  $w("#Anterior").disable();
  $w("#Siguiente").disable();
 
 if (local.getItem('dynamicPageURLs')) {
 const dynamicPageURLs = local.getItem('dynamicPageURLs').split(',');

 const currentPage = '/' + wixLocation.prefix + '/' + wixLocation.path.join('/');
 const currentPageIndex = dynamicPageURLs.indexOf(currentPage);

 if (currentPageIndex > 0) {
      $w("#Anterior").link = dynamicPageURLs[currentPageIndex - 1];
      $w("#Anterior").enable();
    }

 if (currentPageIndex < dynamicPageURLs.length - 1) {
      $w("#Siguiente").link = dynamicPageURLs[currentPageIndex + 1];
      $w("#Siguiente").enable();
    }
  }

Code in All page:

import {local} from 'wix-storage';
import wixLocation from 'wix-location';

$w.onReady(function () {
  $w("#Anterior").disable();
  $w("#Siguiente").disable();
 
 if (local.getItem('dynamicPageURLs')) {
 const dynamicPageURLs = local.getItem('dynamicPageURLs').split(',');

 const currentPage = '/' + wixLocation.prefix + '/' + wixLocation.path.join('/');
 const currentPageIndex = dynamicPageURLs.indexOf(currentPage);

 if (currentPageIndex > 0) {
      $w("#Anterior").link = dynamicPageURLs[currentPageIndex - 1];
      $w("#Anterior").enable();
    }

 if (currentPageIndex < dynamicPageURLs.length - 1) {
      $w("#Siguiente").link = dynamicPageURLs[currentPageIndex + 1];
      $w("#Siguiente").enable();
    }
  }
} );

If you are trying to do this for another collection, then you will have to save the URLs associated with that collection using another storage key. For example, something like this:

For the first collection:

local.getItem('dynamicPageURLs')

For the second collection:

local.getItem('dynamicPageURLs2')

Or, maybe I just don’t properly understand your problem.

I tried doing that and then it jus did not work, next and previous button were disable like it could not find any other item

i will try again I guess.

@rhitservicesllc Keep in mind that if you’re using two separate collections, you will need to separate the code accordingly.

You might find the Dynamic Page Loop example useful. It will show you how to handle previous and next without using local storage. And it even throws in a way to loop at the end from the last to first.

Thanks! That kind of helps, but now i am having 2 issues with your example.

1- The Items are not switching in order, if i click Next or Prev, it will go to another item that is not the next in line.

2- See the picture below, for some reason when going to one of the items is giving me that error, is adding “/cerrar/” in the middle, but is not doing it for all the pages.

Any suggestinons?

And thanks again!

PS. I pretty much used the same code, but change the location from /countries/ to /ibkr-mobile/

Nothing?

@rhitservicesllc My example works fine - just tried it (again). You should review that changes that you made.