Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Fill drop down list with a calculated total

wilsonl28824783
Level 1
Level 1

I have a calculated field that totals the difference between two numbers; is there a way to use that generated total and populate a drop down?

 

example: the calculation is 5; is there a way to populate a drop down list that would show 0,1,2,3,4,5?

I think some combination of a For loop and an array might do it, however, I am no good at script writing and I would really appreciate an example 😌

1 Accepted Solution
Mayank_Gandhi
Correct answer by
Community Advisor
Community Advisor

@wilsonl28824783 

Try adding the calculated element using JS i.e  DropDown.addItem("Last Item," Calculated Value");

View solution in original post

5 Replies
Mayank_Gandhi
Correct answer by
Community Advisor
Community Advisor

@wilsonl28824783 

Try adding the calculated element using JS i.e  DropDown.addItem("Last Item," Calculated Value");

View solution in original post

Nupur_Jain
Community Advisor
Community Advisor

Hi @wilsonl28824783 

 

You can achieve your requirment using Jquery. Find the code snippet to dynamically add values to dropdown.

 

(function ($, $document) {
	"use strict";
	// Add granite:class='custom-dynamic-selection' on selection node on cq:dialog
	const SELECTION_SELECTOR = ".custom-dynamic-selection";

	$document.on("dialog-ready", function () {
		// Change this value after calculating it
		var dynamicLength = 5;
		populateDropdown(dynamicLength);
	});

	function populateDropdown(length) {
		var dynamicSelection = $(SELECTION_SELECTOR);
		if (dynamicSelection.length) {
			dynamicSelection[0].items.clear();
			for (var number = 1; number <= length; number++) {
				var selectItem = {};
				selectItem.content = {};
				selectItem.content.textContent = number;
				selectItem.value = number;
				dynamicSelection[0].items.add(selectItem);
			}
		}
	}

}(jQuery, jQuery(document)));

 

Add this js in "cq.authoring.dialog" clientlibs and add property granite:class on selection like this

Screenshot from 2020-07-02 12-56-12.png

 

Hope it helps!

Thanks,

Nupur