I'm trying to create a dynamic choice filter, whose number of statements changes based on the number of choices selected in the previous question.
To do this, I've written a script in Javascript that builds the correct expression. I've tested the script, and it works as intended, however I can't seem to get the output to be used as the choice filter. I have tried both in the choice filter, and having it calculate in another question and referencing that question.
Does anyone have any idea of how to do this?
Thanks!
In case someone wants to see the code:
function choicenum(N) {
var index_range = [];
//Create an index range based on the count-selected in the question.
for (var i = 1; i <= N; i++) {
index_range.push(i);
}
// Create your filter statements
var filterstr = []
for (var y in index_range) {
filterstr.push("contains(Animal, selected-at(${Animal}" + ", '" + (y) + "')) or ");
}
// Concatenates into a string, joined by a dummy character
var split_char= "?"
var filterstr = filterstr.join(split_char);
// splits along the dummy character and stitches back together
filterstr= filterstr.split(split_char).join('');
// Removes the last "or" from the string
filterstr = filterstr.substring(0, filterstr.length -4)
return filterstr
}
And the output for choicenum(3) will return as
contains(Animal, selected-at(${Animal}, '0')) or contains(Animal, selected-at(${Animal}, '1')) or contains(Animal, selected-at(${Animal}, '2'))