Adobe PDF JavaScript form: If...else synthesis



  • There is a PDF form document, the user input is checked with JavaScript. The user selects different options in forms, and depending on the choices, different labels appear on the outside. Example of the code:

    var countryofgrowth = this.getField("countryofgrowth").value;
    var countryofgrowth = this.getField("sugarquality").value;
    var countryofgrowth = this.getField("sugarQualitylevelhigh").value;
    var countryofgrowth = this.getField("cornquantitymax").value;
    var countryofgrowth = this.getField("cornquantity").value;
    var type = this.getField("type").value;
    

    if ((type == "sugar") && (countryofgrowth == "Afghanistan [AF]" || countryofgrowth == "Albania [AL]" || countryofgrowth == "Algeria [DZ]" || countryofgrowth == "Angola [AO]" || countryofgrowth == "Argentina [AR]" || ……) )
    {
    getField("sugarlabel").display=display.visible;
    getField("goodsugar").display=display.hidden;
    getField("sugarQualitylevellow").display=display.hidden;
    getField("sugarQualitylevelhigh").display=display.hidden;
    getField("sugarQualitylevelhighmorethan75").display=display.hidden;
    getField("sugarQualitylevelhighlessthan75").display=display.hidden;
    getField("cornquantity").display=display.hidden;
    getField("cornquantitymorethan500").display=display.hidden;
    getField("cornquantitymax").display=display.hidden;
    getField("cornquantitymaxyes").display=display.hidden;
    getField("cornquantitymaxno").display=display.hidden;
    }
    else if ((type == "sugar") && (countryofgrowth == "Australia [AU]" || countryofgrowth == "Belgium [BE]" || countryofgrowth == "Canada [CA]" || countryofgrowth == "Czech Republic [CZ]" || countryofgrowth == "Denmark [DK]" || …..))
    {
    getField("sugarlabel").display=display.hidden;
    getField("sugarquality").display=display.visible;
    getField("sugarQualitylevellow").display=display.hidden;
    getField("sugarQualitylevelhigh").display=display.hidden;
    getField("sugarQualitylevelhighmorethan75").display=display.hidden;
    getField("sugarQualitylevelhighlessthan75").display=display.hidden;
    getField("cornquantity").display=display.hidden;
    getField("cornquantitymorethan500").display=display.hidden;
    getField("cornquantitymax").display=display.hidden;
    getField("cornquantitymaxyes").display=display.hidden;
    getField("cornquantitymaxno").display=display.hidden;

    if (sugarquality == "Low Quality")
    {
       getField("sugarlabel").display=display.hidden;
       getField("sugarquality").display=display.visible;
       getField("sugarQualitylevellow").display=display.visible;
       getField("sugarQualitylevelhigh").display=display.hidden;
       getField("sugarQualitylevelhighmorethan75").display=display.hidden;
       getField("sugarQualitylevelhighlessthan75").display=display.hidden;
       getField("cornquantity").display=display.hidden;
       getField("cornquantitymorethan500").display=display.hidden;
       getField("cornquantitymax").display=display.hidden;
       getField("cornquantitymaxyes").display=display.hidden;
       getField("cornquantitymaxno").display=display.hidden;
    }
    else if (sugarquality == "High Quality")
    {
       getField("sugarlabel").display=display.hidden;
       getField("sugarquality").display=display.visible;
       getField("sugarQualitylevellow").display=display.hidden;
       getField("sugarQualitylevelhigh").display=display.visible;
       getField("sugarQualitylevelhighmorethan75").display=display.hidden;
       getField("sugarQualitylevelhighlessthan75").display=display.hidden;
       getField("cornquantity").display=display.hidden;
       getField("cornquantitymorethan500").display=display.hidden;
       getField("cornquantitymax").display=display.hidden;
       getField("cornquantitymaxyes").display=display.hidden;
       getField("cornquantitymaxno").display=display.hidden;
    
       if (sugarQualitylevelhigh == " More than 75%")
        {
         getField("sugarlabel").display=display.hidden;
            getField("sugarquality").display=display.visible;
            getField("sugarQualitylevellow").display=display.hidden;
            getField("sugarQualitylevelhigh").display=display.visible;
            getField("sugarQualitylevelhighmorethan75").display=display.visible;
            getField("sugarQualitylevelhighlessthan75").display=display.hidden;
            getField("cornquantity").display=display.hidden;
            getField("cornquantitymorethan500").display=display.hidden;
            getField("cornquantitymax").display=display.hidden;
            getField("cornquantitymaxyes").display=display.hidden;
            getField("cornquantitymaxno").display=display.hidden;
        }
        else if (sugarQualitylevelhigh == "Less than 75%")
        {
            getField("sugarlabel").display=display.hidden;
            getField("sugarquality").display=display.visible;
            getField("sugarQualitylevellow").display=display.hidden;
            getField("sugarQualitylevelhigh").display=display.visible;
            getField("sugarQualitylevelhighmorethan75").display=display.hidden;
            getField("sugarQualitylevelhighlessthan75").display=display.visible;
            getField("cornquantity").display=display.hidden;
            getField("cornquantitymorethan500").display=display.hidden;
            getField("cornquantitymax").display=display.hidden;
            getField("cornquantitymaxyes").display=display.hidden;
            getField("cornquantitymaxno").display=display.hidden;
        }
    }
    

    }
    else if ((type == "Corn") && (countryofgrowth == "Aland Islands [AX]" || countryofgrowth == "Alderney" || countryofgrowth == "American Samoa [AS]" || countryofgrowth == "Andorra [AD]" || countryofgrowth == "Anguilla [AI]" || countryofgrowth == "Antarctica [AQ]" || ……))
    {
    getField("sugarlabel").display=display.hidden;
    getField("sugarquality").display=display.hidden;
    getField("sugarQualitylevellow").display=display.hidden;
    getField("sugarQualitylevelhigh").display=display.hidden;
    getField("sugarQualitylevelhighmorethan75").display=display.hidden;
    getField("sugarQualitylevelhighlessthan75").display=display.hidden;
    getField("cornquantity").display=display.visible;
    getField("cornquantitymorethan500").display=display.hidden;
    getField("cornquantitymax").display=display.hidden;
    getField("cornquantitymaxyes").display=display.hidden;
    getField("cornquantitymaxno").display=display.hidden;

    if (cornquantity == "More than 500t")
    {
       getField("sugarlabel").display=display.hidden;
       getField("sugarquality").display=display.hidden;
       getField("sugarQualitylevellow").display=display.hidden;
       getField("sugarQualitylevelhigh").display=display.hidden;
       getField("sugarQualitylevelhighmorethan75").display=display.hidden;
       getField("sugarQualitylevelhighlessthan75").display=display.hidden;
       getField("cornquantity").display=display.visible;
       getField("cornquantitymorethan500").display=display.visible;
       getField("cornquantitymax").display=display.hidden;
       getField("cornquantitymaxyes").display=display.hidden;
       getField("cornquantitymaxno").display=display.hidden;
    }
    else if (cornquantity == "More than 1000t")
    {
       getField("sugarlabel").display=display.hidden;
       getField("sugarquality").display=display.hidden;
       getField("sugarQualitylevellow").display=display.hidden;
       getField("sugarQualitylevelhigh").display=display.hidden;
       getField("sugarQualitylevelhighmorethan75").display=display.hidden;
       getField("sugarQualitylevelhighlessthan75").display=display.hidden;
       getField("cornquantity").display=display.visible;
       getField("cornquantitymorethan500").display=display.hidden;
       getField("cornquantitymax").display=display.visible;
       getField("cornquantitymaxyes").display=display.hidden;
       getField("cornquantitymaxno").display=display.hidden;
    
        if (cornquantitymax == "Yes")
        {
         getField("sugarlabel").display=display.hidden;
            getField("sugarquality").display=display.hidden;
            getField("sugarQualitylevellow").display=display.hidden;
            getField("sugarQualitylevelhigh").display=display.hidden;
            getField("sugarQualitylevelhighmorethan75").display=display.hidden;
            getField("sugarQualitylevelhighlessthan75").display=display.hidden;
            getField("cornquantity").display=display.visible;
            getField("cornquantitymorethan500").display=display.hidden;
            getField("cornquantitymax").display=display.visible;
            getField("cornquantitymaxyes").display=display.visible;
            getField("cornquantitymaxno").display=display.hidden;
        }
        else if (cornquantitymax == "No")
        {
            getField("sugarlabel").display=display.hidden;
            getField("sugarquality").display=display.hidden;
            getField("sugarQualitylevellow").display=display.hidden;
            getField("sugarQualitylevelhigh").display=display.hidden;
            getField("sugarQualitylevelhighmorethan75").display=display.hidden;
            getField("sugarQualitylevelhighlessthan75").display=display.hidden;
            getField("cornquantity").display=display.visible;
            getField("cornquantitymorethan500").display=display.hidden;
            getField("cornquantitymax").display=display.visible;
            getField("cornquantitymaxyes").display=display.hidden;
            getField("cornquantitymaxno").display=display.visible;
        }
    }
    

    }

    To date, my problem is that in syntax with countries, operator. || - has 30 to 90 countries, depending on the synticsis. I now have four different syntaxes with different countries in each. How can four groups be grouped under four different variables?

    Ideally would like to take the example of the following: The code presented below does not work, provided only for example:

    var countryofgrowth = this.getField("countryofgrowth").value;
    var countryofgrowth = this.getField("sugarquality").value;
    var countryofgrowth = this.getField("sugarQualitylevelhigh").value;
    var countryofgrowth = this.getField("cornquantitymax").value;
    var countryofgrowth = this.getField("cornquantity").value;
    var type = this.getField("type").value;

    var Highriskcountries = ["Afghanistan [AF]", "Albania [AL]", "Algeria [DZ]", "Angola [AO]", "Argentina [AR]", .....];

    var Lowriskcountries = ["Australia [AU]", "Belgium [BE]", "Canada [CA]", "Czech Republic [CZ]", "Denmark [DK]", .....];

    var Mediumriskcountries = ["Aland Islands [AX]", "Alderney", "American Samoa [AS]", "Andorra [AD]", "Anguilla [AI]", "Antarctica [AQ]", .....];

    var Acceptablecountries = ["Aland Islands [AX]", "Alderney", "American Samoa [AS]", "Andorra [AD]", "Anguilla [AI]", "Antarctica [AQ]", "Australia [AU]", "Belgium [BE]", "Canada [CA]", "Czech Republic [CZ]", "Denmark [DK]", .....];

    if ((type == "sugar") && (countryofgrowth == Highriskcountries))
    {
    getField("sugarlabel").display=display.visible;
    getField("goodsugar").display=display.hidden;
    getField("sugarQualitylevellow").display=display.hidden;
    getField("sugarQualitylevelhigh").display=display.hidden;
    getField("sugarQualitylevelhighmorethan75").display=display.hidden;
    getField("sugarQualitylevelhighlessthan75").display=display.hidden;
    getField("cornquantity").display=display.hidden;
    getField("cornquantitymorethan500").display=display.hidden;
    getField("cornquantitymax").display=display.hidden;
    getField("cornquantitymaxyes").display=display.hidden;
    getField("cornquantitymaxno").display=display.hidden;
    }
    else if ((type == "sugar") && (countryofgrowth == Lowriskcountries))
    {
    getField("sugarlabel").display=display.hidden;
    getField("sugarquality").display=display.visible;
    getField("sugarQualitylevellow").display=display.hidden;
    getField("sugarQualitylevelhigh").display=display.hidden;
    getField("sugarQualitylevelhighmorethan75").display=display.hidden;
    getField("sugarQualitylevelhighlessthan75").display=display.hidden;
    getField("cornquantity").display=display.hidden;
    getField("cornquantitymorethan500").display=display.hidden;
    getField("cornquantitymax").display=display.hidden;
    getField("cornquantitymaxyes").display=display.hidden;
    getField("cornquantitymaxno").display=display.hidden;
    }

    That's what my form looks like:

    ![enter image description here

    I presented the example of text fields where my code is stored, so far my code is working, the problem is syntaxis with If... ♪ Else and all countries have a lot of lines of code and he doesn't fit into one text field, so we have to roll into a few, but unfortunately, because some of my ifs. ♪ ♪ Else syntaxis have a large number of syntaxes, I can't separate them between the text fields, because I was hoping to solve the problem with a group of countries to save the number of lines of the code plus the code to be clean. Thank you so much for any help.

    enter image description here



  • Like one way to put them in the mass and check through. https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

    var AcceptableCountries = ["Aland Islands [AX]", "Alderney", "American Samoa [AS]", "Andorra [AD]", "Anguilla [AI]", "Antarctica [AQ]", "Australia [AU]", "Belgium [BE]", "Canada [CA]", "Czech Republic [CZ]", "Denmark [DK]"];
    

    var countryofgrowth = "Czech Republic [CZ]";
    if(AcceptableCountries.indexOf(countryofgrowth) != -1){
    console.log("okay");
    }

    You can also reduce your code by putting the fields in a function.

    var setDisplay = function(display){
    var fields = ["sugarlabel", "goodsugar", "sugarQualitylevellow" /,.../];
    for(var i=0; i< fields.length; i++){
    if(fields[i] == display) {
    getField(fields[i]).display=display.visible;
    } else {
    getField(fields[i]).display=display.hidden;
    }
    }
    }

    setDisplay("goodsugar")




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2