Sign In Register

How can we help you today?

Start a new topic

Dynamic Form dropdown menu with arrays

Hello,
I'm a hobbyist developer, and I'm pretty inexperienced with server/html coding. I'm currently having trouble creating a dropdown menu for an array in dynamic forms.

I have a meta collection "Cities" and each city object contains an array of any size, along with some other properties. The elements of this array are the $oids of objects from another meta collection, "Regions"

Essentially, I'm trying to query the "Regions" meta collection for all objects in it, and use those as options. I've currently got all the options showing up correctly, but I can't get it to display the selected object correctly.

Here is the relevant Javascript:
function view(data){
        if(data.id !== undefined){
            form.city = Spark.metaCollection("cities").findOne({"_id":{"$oid":data.id}});
            form.allRegions = Spark.metaCollection("regions").find();
        }
        return form;
    }

 Here is the relevant HTML:

{{#each form.city.regions}}
            <gs-row>
                <gs-col width=11>
                    <select name="region[{{@index}}]" class="input-block-level" style="margin-bottom:5px">
                        {{#each ../form.allRegions}}
                            <option value="{{_id.$oid}}" {{#compare _id.$oid "===" ../form.city.regions[@index]}}selected{{/compare}}>{{name}}</option>
                        {{/each}}
                    </select>
                </gs-col>
                <gs-col width=1>
                    <gs-link snippet="edit_city?action=removeRegion&id={{form.city._id.$oid}}&region="><i data-toggle="tooltip" data-placement="top" title="Remove Region" class="icon-trash"/></gs-link>
                </gs-col>
            </gs-row>
            {{/each}}

 

And here is the json data that I'm trying to get this to work with.

"Cities" meta collection object:

"_id": {
   "$oid": "5a78605dcc302a04c40dcf5c"
},
"name": "City 03",
"locID": "city03_title",
"version": 1,
"regions": [
   "5a8d92a557203b04c3f63baa",
   "5a7863b82f481f04c7f04cc2"
]

 "Regions" meta collection objects:

"_id": {
   "$oid": "5a7863b82f481f04c7f04cc2"
},
"name": "Test Region",
"locID": "testRegion",
"version": 1
"_id": {
   "$oid": "5a8d92a557203b04c3f63baa"
},
"name": "Test Region 2",
"locID": "testRegion2_loc",
"version": 1
Currently, with this data and code, I get an error:
Parse error on line 26: ... "===" ../form.city.regions[@index]}}sel -----------------------^ Expecting 'ID', got 'INVALID'

Which is being caused by this section here:
{{#compare _id.$oid "===" ../form.city.regions[@index]}}

What do I set for the compare function here to compare the value of the array vs. the id of the region object?

Thanks for any help. As I mentioned, I'm just a hobbyist, so please let me know if I haven't explained something well.


I discovered that Handlebars aren't something from Gamesparks, but a separate library. I managed to figure it out just by looking up Handlebars stuff, and it's a pretty simple fix.


Just had to change the one line in the HTML to be:

<option value="{{_id.$oid}}" {{#compare this._id.$oid "===" ../this}}selected{{/compare}}>{{name}}</option>

 


1 person likes this

This needs to be added to documentation somewhere, I looked forever for this. Thankyou.

Login to post a comment