Numbered index

Hi everyone!
I wanted to know if it was possible to have a numbered index.
More precisely, I have a big collection of movies on which my entire site relies. I have two general index pages where everything is presented, one in alphabetical order, and the other one in chronological order. Plus, I have a page for each studio on which every movie made by a specific studio is listed. But I would want all those lists to be numbered. So if I go on a studio page, the first movie by that studio is identified 1, then the next one is 2… But when I go on the alphabtical page, then the first movie starting with A is number 1, which is not the same as previously. So it has to be done automatically, I could have 50+ index pages, but I obviously can’t have 50+ columns in my collection.

Thanks in advance for your help!

If you are the only one adding movies to the database, then you shouldn’t really have a problem. You can create a database collection that has one row with one value: the serial number. When you need a new serial number, do a query of the serial number collection and add one to the result. Don’t forget to save the new value to your collection.

This method could fall apart if others are able to add movies since there might be a conflict on who updates the serial number first. If it’s just you, then you should be good to go.

I’m sorry I really don’t get how to do that…
So, I created a whole new database with one row titled Serial Number and one value (1). And I’m stuck already.
I suspect I need to code the query of the collection? But how do I save the resulting number then? And what code will allow me to add one to the serial number? Plus, one movie could have up to 6 different serial numbers. How will the site know which one to load for a specific page? Is that method compatible with the repeaters? (What I tried to do first was to simply have a numbered collection from 1 to 999, and put a number in the repeater and connect it to the collection. Turns out one repeater can only be linked to one collection. It can’t have elements in it that are linked to other collections otherwise they just won’t appear. Unless I’m doing something wrong.)
Thanks a lot for your help, I’m really not that skilled, being more of a design person than a code person. I really wanna learn though and your tips are really helping!

EDIT: Oh and no, users won’t be able to add movies, so technically your method should work.

Hi,

Let’s take another, and I hope more straightforward, approach.

The idea is that before you add a record, you will find out what the last serial number in the collection is, add one, and then put this serial number in the record. The record can then be saved.

To do this, we’ll use a data hook. For more information about this, read the article How to Use Data Hooks .

As a very simple example, let’s say here’s our insert of a new record to the collection called Movies .

const record = {
	"title": "",
	"serial": 0
};

wixData.insert("Movies", record)
	// log the error if the insert fails
	.catch((err) => {
		console.log("error", err);
	});

Now we’ll need to add a hook that catches the record before it’s inserted, finds out what the last serial number is, and puts it in the record. The record is then returned so that it can now be added to the collection.

Add the hook to the collection:


And ask for a before insert hook:

Here’s the code for the beforeInsert hook:

import wixData from "wix-data"; // you'll need this too

export function Movies_beforeInsert(item, context) {
	return wixData.query("Movies")
		.limit(1)
		.descending('serial') // sorts the results in descending order by 'serial'
		.find()
		.then((results) => {
			if(results.length === 0) {
				item.serial = 1;	// start at 1 for an empty collection
			}
			else {
				const lastItemInCollection = results.items[0]; // get item with last serial number
				item.serial = lastItemInCollection.serial + 1; // increment last serial number	
			}
			return item; // beforeInsert function returns the modified item (record) to be saved
		});
}

Now, each time you add a record to the Movies collection, the code looks for the last serial number used, adds one to it for a new serial number, and then saves the record with the new serial number.

I need to point out here that this is a very simple example, and even then it requires a good amount of code. You will need to familiarize yourself with basic coding concepts to accomplish what you want. There are a wealth of Javascript coding sites which will help you learn Javascript from basic to advanced - Javascript.info is a good one. The Wix Code Resources page provides tutorials, examples, and articles on getting the most out of Wix Code. We are happy to get you pointed in the right direction, but you’ll need to take it from there. As questions or difficulties arise, we are here to help.

Good luck,

Yisrael

Will this work when inserting multiple entries in a collection. I tried but it just gives me 1 value. Example: If the last serial number was 5 and i inserted 4 more items from the page using the repeater’s forEach function then all 4 items have the serial number 6.

any work around for this?

@shantanukumar847 If you’re doing this in a loop, you need to ensure that you increment the value for each insert.

got it!! thanks

@shantanukumar847 hi Shan, I am trying out something similar but I am horrible with javascript. Would you mind sharing your completed and working code?

@yifanzhou Please make a new post and describe your issue more broadly so that one of the community members can provide a solution for you. Take a look at our Community Guidelines for making a new post.