Search code examples
google-apps-scriptgoogle-app-maker

How to manually set a value in MultiSelect widget in AppMaker?


I'm using REST API as a datasource and not sure how to manually set a value in the MultiSelect widget

The widget datasource is named Service and has an id and name

What I tried is widget.values = [...IDs of the values...] but it doesn't work


Solution

  • Two things:

    1. The binding in your follow-up comment -- @datasources.Services.items -- won't work for a multiselect, because it represents the entire set of records in your Services datasource. The binding that Markus suggests is what projects to grab just the names of each of the items. If you are trying to present the name as the human-readable choice but use the id as the value you're going to do something with, then you'll want this:
    widget.names = @datasources.Service.items..name;
    widget.options = @datasources.Service.items..id;
    
    1. The multiselect options property represents the possibilities for the values property, and the values property represents the options that have been selected. They are each set as an array. The stuff in 1. above gets you the right set of options but doesn't deal with values. If you want to present particular values as selected or not, you'll either need to:
      • Pass in values from another datasource, turned into an array. This would be if you're reading the values from a specific field in another record. So if you had a User datasource, and the User had a Services field, you might get the existing selections from the User record like this: @datasources.User.item.Services#strToArray(); or
      • Set the values programmatically. This would be if you're doing something else behind the scenes to generate the pre-selected values. So if your Service datasource has six things in it that are potential options, but only some of them should be selected, your script would contain something like this: widget.values = [selection1, selection5, selection6];