Good morning,
I am developing an application in which "Batch" object names have a pre-defined structure which is:
progressiveLetter + number + ' ' (space) + year
For example A01 2020
and B03 2020
. The number depends on the value chosen in a select field:
<form:select class="form-control" id="numero_settore_act" path="numero_settore_act.id_settori" onchange="buildNameAct.apply(this, ${nextLettersAct})">
<form:option value="" />
<form:options items="${settoriAct}" itemValue="id_settori" itemLabel="numero_settore" />
</form:select>
On change the Javascript function buildNameAct
(code at the end of question) place inside a text box the constructed name.
More in general:
- year is taken correctly from another field
- number is taken correctly from the select field
- Now it's time to set the letter, here comes the pain.
Letters comes in a String objects array from the back-end (Spring MVC), so it is in the ModelMap.
Searching the net I found that the way to pass an Array to a javascript function is to use .apply()
method as i did with buildNameAct.apply(this, ${nextLettersAct})
. The problem is that it do not work at all, the only information given is an exception in the console:
Uncaught SyntaxError: Unexpected token ';'
although I didn't know why.
Is there a better way to pass the array I create in the back-end than this? or where is the error in my code? I tried several hours without resolving anything.
Taken into account that the function compile the name correctly if I pass a single suggestion, the error may be in how I pass the array to the function.
Js function code
function buildNameAct(a, b, c, d, e, f, g, h)
{
let sectorValue = document.getElementById("numero_settore_act").value;
let sectorNumber = +sectorValue;
let letterValue = '';
switch (sectorNumber)
{
case 1:
letterValue = a;
break;
case 2:
letterValue = b;
break;
case 3:
letterValue = c;
break;
case 4:
letterValue = d;
break;
case 5:
letterValue = e;
break;
case 6:
letterValue = f;
break;
case 7:
letterValue = g;
break;
case 8:
letterValue = h;
break;
}
if(sectorNumber < 10)
{
sectorValue = '0'.concat(sectorValue);
}
let yearValue = document.getElementById("anno_lotto").value;
document.getElementById("nome_batch_act").value = letterValue.concat(sectorValue).concat(" ").concat(yearValue);
}
EDIT
Further investigations shows that the line
<form:select class="form-control" id="numero_settore_act" path="numero_settore_act.id_settori" onchange="buildNameAct.apply(this, ${nextLettersAct})">
is translated as
onchange="buildNameAct.apply(this, [Ljava.lang.Object;@3528ec13)"
and that may lead to the aformentioned Unexpected token ';'
error, now I do not know how can I pass the array in a way that it is translated without the ;
which is causing the error.