1

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.

DiegLuthor
  • 195
  • 1
  • 14
  • So, what your current problem you are facing in your code? Also what does `a, b, c, d, e, f, g, h` has in it ? – Swati May 29 '20 at 11:20
  • The main problem is that the function is not putting in the field the "name" after construction has been made. Letters represents the element of the Array passed as a parameter, as i read in the tutorial that array elements are mapped to the parameters written in the declaration of the function. Is this right or I was wrong since the beginning? – DiegLuthor May 29 '20 at 11:47
  • yes you are right ,what ever you will passed in your function i.e : `a,b,c..` this can used inside your function.So ,you said `function is not putting in the field the "name" after construction has been made..` ? what do you mean by this ? does `nome_batch_act` show any value or not ?Also if you are passing all values of array how do you decide which case to be selected? – Swati May 29 '20 at 11:51
  • the variable doesn't show any value, so the field on the form is not autofilled by the instruction `document.getElementById("nome_batch_act").value = letterValue.concat(sectorValue).concat(" ").concat(yearValue);` – DiegLuthor May 29 '20 at 11:57
  • 1
    I will test this and let you know . – Swati May 29 '20 at 12:56
  • 1
    I tried by passing with `ArrayList` and it worked with `apply` method.But,in your question you are passing `object` not string array thats the reason its not working?Maybe converting your array to string array might worked.This [post](https://stackoverflow.com/questions/14773264/convert-listobject-to-string-in-java) will help you to convert and then passed that string array to js code .Let me know if you get any error after that. – Swati May 29 '20 at 14:21
  • 1
    I was actually thinking it was the object type I was passing the source of the error, by changing directly to a String with `Arrays.toString(suggestionsAct.toArray()) ` the array (inside controller) and then splitting it in the JS function worked perfectly, moreover the JS function is much smaller than before. – DiegLuthor May 29 '20 at 14:34

0 Answers0