The CreatorCon Call for Content is officially open! Get started here.

How to select only years on my Catalog Item view?

Olga -Maria
Tera Contributor

Hello, i have the requirement for a new variable called 'calendar' on my Catalog Item, to only show years from now on. 

You should be able to select only one year, it can be this actual year or an upcoming one.

How can i achieve this? 

Thank you.

1 ACCEPTED SOLUTION

This should work well for you.  The 'calendar' variable will be a Select Box type.  Choice table = -- None -- and the Include none box checked (if that's what you prefer).  That's it for the variable setup - you will not create any Question Choices.  Next create an onLoad Catalog Client Script that Applies on a Catalog Item view, Requested Items, and/or Catalog Tasks - where ever this variable will be populated/updated.  Your script would look something like this to populate the Calendar select box with choices for the current year and the next 24 years:

function onLoad() {
	var today_date = formatDate(new Date(), 'yyyymmdd').substring(0,4);
	for(var i=0; i<25; i++) {
		g_form.addOption('calendar', parseInt(today_date) + i, parseInt(today_date) + i);
	}
}

 

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Hi Olga!

One way to do this is with a Lookup Select Box type variable.  Reference the sys_choice table, or anywhere else that you have or want to create a record for each year as far out as you want to be selected.  If you're not keen to update this table at the beginning of each year to make the previous year record inactive, you can call a Script Include in the Reference qualifier of the Lookup Select Box.  The Script Include will get the current year, then only return records from the choice table that are this number or higher.

Olga -Maria
Tera Contributor

Hi Brad, thank you for your response.

I should create the variable so that i can only choose upcoming years and not see the past ones, it should be dynamic so that you should not have to update the years. Mentioning that i do not have years as choices to refere to. 

 

This should work well for you.  The 'calendar' variable will be a Select Box type.  Choice table = -- None -- and the Include none box checked (if that's what you prefer).  That's it for the variable setup - you will not create any Question Choices.  Next create an onLoad Catalog Client Script that Applies on a Catalog Item view, Requested Items, and/or Catalog Tasks - where ever this variable will be populated/updated.  Your script would look something like this to populate the Calendar select box with choices for the current year and the next 24 years:

function onLoad() {
	var today_date = formatDate(new Date(), 'yyyymmdd').substring(0,4);
	for(var i=0; i<25; i++) {
		g_form.addOption('calendar', parseInt(today_date) + i, parseInt(today_date) + i);
	}
}