Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Use Array of names to set Items in combobox

Explorer ,
Feb 25, 2021 Feb 25, 2021

I am trying to loop an array to set Items in a long list of comboboxes. I keep getting not a function as a return.

 

var myArray=new Array ("combobox1","combobox2",etc);
for (i=0; i < myArray.length; i++);
{myArray[i].setvalue(["1","2"],["3","4"]);}

I keep getting myArray[i].setvalue is not a function. What am I doing wrong?  I am new to this so anyadvice is beneficial. Thanks for reading!

TOPICS
How to , JavaScript
1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Feb 27, 2021 Feb 27, 2021

Use:

this.getField(myArray[i]).setItems( ... )

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 25, 2021 Feb 25, 2021

You must use setItems on the field objects. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 27, 2021 Feb 27, 2021

That was definitely my issue. would calling  getField("myArray[i]").setItems() not work then? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 27, 2021 Feb 27, 2021

Use:

this.getField(myArray[i]).setItems( ... )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 27, 2021 Feb 27, 2021

Yes, the quotes would look for a field with that name not the array itself. Thank you very much for the help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 15, 2021 Mar 15, 2021

Hi. How would you set combobox items one at a time as if reading from records from a table?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2021 Mar 15, 2021

Put the entries in a array and use the method setItems.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 15, 2021 Mar 15, 2021

Thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 15, 2021 Mar 15, 2021
LATEST

Bernd is correct, that is how you can add one item at a time by calling (myArray[x]) x being the number of the array entry you want to add and then using setItems. 

this.getField("combobox").setItems(myArray[x]);

I am a little new but I think this will work. It sounds though like you want to automate the process?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 25, 2021 Feb 25, 2021

Also, myArray[i] returns a string, not a field. You need to use the getField method to access a field object.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 27, 2021 Feb 27, 2021

Ah I see. Would there be any way to use getField on multiple fields? like getField ("1","2","3") etc? Or would I have to input each getField individually? 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 27, 2021 Feb 27, 2021

You have to CALL getField for each field. You don't have to type it, you loop over your array and call it for each. But in general you can't take something made for one item and make it take a list, that's what loops are for.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 27, 2021 Feb 27, 2021

By the way this line

for (i=0; i < myArray.length; i++);

does nothing at all. The following line

{myArray[i].setvalue(["1","2"],["3","4"]);}

will be run exactly once. This is because of the semi-colon on the for line, which runs everything BEFORE the semi-colon in a loop (so in this case runs nothing).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 27, 2021 Feb 27, 2021

That is very good to know! Thank you for the claridication. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines