Velo Tutorial: Creating Previous and Next Buttons for a Dynamic Item Page with Code
To understand why it doesn't work, we need to understand a little bit about how dynamic item pages work. A dynamic item page decides which item to display using the dynamic sections of the URL that was used to reach the page. When the page loads, the proper item is then fetched from the database and is accessible to the page through the page's dynamic dataset. Since it is an item page, there will only be one item in the dataset. Therefore, there are no "previous" or "next" items to navigate to using the Previous and Next actions of the dataset.
This article will demonstrate how you can use code to create previous and next buttons for a dynamic item page. We will do so by adding code to two pages. First, in an index page that your users use to reach your dynamic item pages, we will add code to store all the relevant item page links. Then, in the dynamic item page, we will add code to retrieve the links and use them in previous and next buttons that we will add to the page.
Index Page
To create the previous and next buttons on the dynamic item page we don't need to change the index page at all. We just need to add some code that will work behind the scenes.
- Retrieves the collection items from the dataset
- Gets the relative URLs for the dynamic item page for each of the items
- Stores the URLs in the user's local browser storage for us to use later
1import {local} from 'wix-storage';
2
3const linkField = "link-to-item"; // replace this value
4
5$w.onReady(function () {
6 $w("#myDataset").onReady(() => {
7 const numberOfItems = $w("#myDataset").getTotalCount();
8
9 $w("#myDataset").getItems(0, numberOfItems)
10 .then( (result) => {
11 const dynamicPageURLs = result.items.map(item => item[linkField]);
12 local.setItem('dynamicPageURLs', dynamicPageURLs);
13 } )
14 .catch( (err) => {
15 console.log(err.code, err.message);
16 } );
17 } );
18} );
First, this code assumes your page's dataset has the ID myDataset. If it does not have that ID, you need to either change the dataset's ID to myDataset or change the code wherever it says "#myDataset"
to reflect your dataset's actual ID. You can change an element's ID by selecting the element and editing the ID field in the Properties & Events panel.
On this line
const linkField = "link-to-item"; // replace this value
change the value inside double quotes to the correct field key.
Item Page
- Disables the previous and next buttons
- Retrieves the URLs stored in the user's local browser storage
- Retrieves the URL of the current page
- Gets the location of the current page's URL within the list of all the URLs
- If there is a URL to a "previous" page, sets the previous button's link to that URL and enables the previous button
- If there is a URL to a "next" page, sets the next button's link to that URL and enables the next button
1import {local} from 'wix-storage';
2import wixLocation from 'wix-location';
3
4
5$w.onReady(function () {
6 $w("#previous").disable();
7 $w("#next").disable();
8
9 if (local.getItem('dynamicPageURLs')) {
10 const dynamicPageURLs = local.getItem('dynamicPageURLs').split(',');
11
12 const currentPage = '/' + wixLocation.prefix + '/' + wixLocation.path.join('/');
13 const currentPageIndex = dynamicPageURLs.indexOf(currentPage);
14
15 if (currentPageIndex > 0) {
16 $w("#previous").link = dynamicPageURLs[currentPageIndex - 1];
17 $w("#previous").enable();
18 }
19
20 if (currentPageIndex < dynamicPageURLs.length - 1) {
21 $w("#next").link = dynamicPageURLs[currentPageIndex + 1];
22 $w("#next").enable();
23 }
24 }
25} );
This code assumes that your buttons have the IDs previous and next. If your buttons do not have those IDs, you need to either change the IDs of the buttons to previous and next or change the code wherever it says "#previous"
and "#next"
to reflect actual IDs of your buttons. You can change an element's ID by selecting the element and editing the ID field in the Properties & Events panel.
API List
- $w.onReady( ) - Sets the function that runs when all a page's elements have finished loading.
- $w.Button.disable( ) - Disables an element.
- $w.Button.enable( ) - Enables an element.
- $w.Button.link - Sets or gets the element's link.
- wix-dataset.getItems( ) - Returns the selected items from a dataset.
- wix-dataset.getTotalCount( ) - Returns the number of items in a dataset that match its filter criteria.
- wix-dataset.onReady( ) - Adds an event handler that runs when a dataset is ready.
- wix-location.prefix - Gets the prefix of the page's URL.
- wix-location.path - Gets the path of the page's URL.
- wix-location.to( ) - Directs the browser to navigate to the specified URL.
- wix-storage.getItem( ) - Gets an item from local or session storage.
- wix-storage.setItem( ) - Stores an item in local or session storage.
- wix-window.rendering.env - Gets the rendering environment.