The CreatorCon Call for Content is officially open! Get started here.

Adding list items with commas (via script)

Joe Stoner
Kilo Contributor

I have am working on an application that includes a list field (from a set of strings in the choice table), and there is an "All" list item that customers want to be able to use as a sort of shortcut - if they choose "all" from the dropdown, they'd like the list to just be populated with all items (except "all"). I've got a client script that looks at the list field when it changes, and if it finds the value "All", then it runs my code to change the values in the list.

I've run into two issues here. First, I would like the code to iterate through the options and populate the list, rather than hard-code a string of comma separated values to assign to the field. I haven't found a way to navigate to those options via g_form.

Second, one of the items in the list has commas, and the only way I have found to populate the list field is by setting it to a string of comma-separated values. As you might imagine, this means that instead of adding "apple, banana, carrot" as one list item, I'm adding three items to the list. 

I don't want to overly complicate this by changing the list to use a reference to a different table, because the users want to have their simple drop-down list, and for performance/speed reasons I don't want to make an ajax call or set up a scratchpad variable. 

Has anybody else encountered, and hopefully solved, this problem?

3 REPLIES 3

Yash Agrawal1
Tera Guru

Hello joe,

If you have a list collector type of field/variable and you want to fetch its value ,then use.

var listarr=g_form.getValue('name of list collector variable').split(',');

alert(listarr);

Will give you values in comma separated form.

Regards

Yash Agrawal

I don't have trouble getting the values already in the list. My issue is populating the list field with values that include a comma.

johansec
Tera Guru

I know you dont want to but I think AJAX would be the way to do this. If I am not misunderstanding the requirements here is what I would do and why. 

  • Create a client script to check for 'All' value. You already do this but you can get the gform value of the field and use .indexOf if you want to find out.
  • Create a script include
    • Create an array variable to store options
    • run a glidequery and populate the array with the ids
    • make sure to exclude 'all' in this glidequery
    • return the array with .toString() this will make it comma delimited
  • If All is found, the client script call the script include.
  • Then the client script will receive the value from the script include and populate the field.

 

Why I would do it this way is because I don't know of another way to get every single value from the client side without hitting the server. I also would not hardcode these values because if there are more added later maintenance will start to get very annoying for you and your team. 

 

If you are avoiding ajax for this solution because of concerns of performance, I hope it comforts you to know we used this many places and the user experience is seamless. 

If AJAX is outside of your comfort zone there is plenty of documentation that can assist you. 

https://snprotips.com/blog/2016/2/6/gliderecord-client-side-vs-server-side