0

I'm trying to understand this but for some reason I can't. I'm fairly new to Javascript....

Here is what I want to do.

var companyList = {};
companyList.comanyZero = {
  fldStreet: "That street 0",
  fldPostcode: "0000 AA",
  fldCity: "Amsterdam",
  fldCountry: "The Netherlands"
};
companyList.companyOne = {
  fldStreet: "Street name 1",
  fldPostcode: "1234 BA",
  fldCity: "Amsterdam",
  fldCountry: "The Netherlands"
};

if (!event.willCommit){ 

if (event.changeEx === "companyZero"){
    this.getField("fldStreet").value = companyList.companyZero.fldStraat;
    this.getField("fldPostcode").value = companyList.companyZero.fldPostcode;
    this.getField("fldCity").value = companyList.companyZero.fldCity;
    this.getField("fldCountry").value = companyList.companyZero.fldCountry;
}

if (event.changeEx === "companyOne"){
    this.getField("fldStreet").value = companyList.companyOne.fldStreet;
    this.getField("fldPostcode").value = companyList.companyOne.fldPostcode;
    this.getField("fldCity").value = companyList.companyOne.fldCity;
    this.getField("fldCountry").value = companyList.companyOne.fldCountry;
}
}

The idea is to create a loop that gives back the values according to what is selected in my combobox.

Please note that this is a PDF moderation so users can change a combobox in a PDF and the fields will be filled with correct data.

Interactive
  • 1,474
  • 5
  • 25
  • 57

1 Answers1

1

I think you're just looking for brackets notation:

if (!event.willCommit) {
    var currentCo = companyList[event.changeEx];
    if (currentCo) {
        this.getField("fldStreet").value = currentCo.fldStraat;
        this.getField("fldPostcode").value = currentCo.fldPostcode;
        this.getField("fldCity").value = currentCo.fldCity;
        this.getField("fldCountry").value = currentCo.fldCountry;
    }
}

If event.changeEx contains "companyOne", then companyList[event.changeEx] will reference companyList.companyOne.


Since your property name and field names are same, you can even maintain a list of keys to be fetched and update their values using a loop:

var fieldNames = ["fldStreet", "fldPostcode", "fldCity", "fldCountry"]
if (!event.willCommit) {
  var currentCo = companyList[event.changeEx];
  if (currentCo) {
    fieldNames.forEach((name) => {
      this.getField("").value = currentCo[name];
    })
  }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Well I'm trying to understand how this works. As far as I know I thought objectlist was my "array" and I could get my data from there. Based upon that I should be able to select data based upon a combobox selection! – Interactive May 12 '17 at 12:06
  • @Interactive: Your `companyList` is a non-array object, not an array. The above does select information from it based on the combobox selection (assuming that's what `event.changeEx` is). – T.J. Crowder May 12 '17 at 12:07
  • Oh crap I just read your "non-code" part. It's brilliantly simple . Thank you – Interactive May 12 '17 at 12:08
  • Yes the value of `event.changeEx` is indeed the combobox! – Interactive May 12 '17 at 12:10