4 Replies Latest reply on Jan 14, 2015 4:50 AM by JRez7884

    JavaScript for Inventory

    JRez7884 Level 1

      Hi, I have a form for users to enter into serial number or barcodes in a text field ("text_field_input_1"), what I'm trying to do is have a javascript look up those identical values and populate the item name into a text field ("text_field_output_1"). The problem is more of applying this javascipt to multiple fields of inputs and outputs, the form can run about 400 fields ("text_field_input_400") to ("text_field_output_400"). The best script I have is base on a switch, I did use a if statement but due to the high inventory and (input & output) fields, yet it was going to be tiresome.

       

      var POPGrid = "#POP.P"

      var InvGrid = event.value;

      switch (InvGrid) {

      case "0000001":

      this.getField(POPGrid).value = "Part Item A";

      break;

      case "0000002":

      this.getField(POPGrid).value = "Part Item B";

      break;

      .....

      case "000000N":

      this.getField(POPGrid).value = "Part Item Z";

      break;

      default:

      this.getField(POPGrid).value = "Invalid HPID Item";

      break;

      }

       

      It seems this script is just as long, is there better way to have done this?

       

      Thanks,

      JR

        • 1. Re: JavaScript for Inventory
          try67 MVP & Adobe Community Professional

          I would create a data-model in the form of two matching arrays and then just look up the value in the first one and use the matching index to retrieve the part-name. It's easier shown than explained, I think:

           

          var values = ["0000001", "0000002", "0000003"]; // etc.
          var partNames = ["Part Item A", "Part Item B", "Part Item Z"]; // etc.
          var i = values.indexOf(event.value);
          if (i!=-1) this.getField("#POP.P").value = partNames[i];
          else this.getField("#POP.P").value = "";
          

           

          This is basically all the code you need. You just fill in both arrays with the values, and you're done.

          • 2. Re: JavaScript for Inventory
            JRez7884 Level 1

            Thank you so much, I just test out the script you wrote and it works perfect. The one you provided is more organize and looks completely simple. I just need one more value input, or how to rewrite the following from the switch script:

            default:

            this.getField(POPGrid).value = "Invalid HPID Item";

            break;

             

            Basically, if the user enter the values wrong, it needs to output that it is a "invalid item number".

            Thanks

            • 3. Re: JavaScript for Inventory
              try67 MVP & Adobe Community Professional

              That's what the last line of my code does. I used a blank string, but you can replace it with an error message, if you wish.

              • 4. Re: JavaScript for Inventory
                JRez7884 Level 1

                I understand now, thanks again!