Select Multi Select

I’m sure I’m missing something simple but I’m stumped.

I have a multi-select field which gets a list of categories from the tbl_categories table. I then have a table tbl_stocks which has a list of stocks and a table tbl_stock_categories which stores the category id’s associated with each stock id.

First of all what I need is to be able to have the multi-select field select the already selected values from the tbl_stock_categories table. Then when I save the form I need those values to update if they’ve been changed.

I suppose I could just put a category_ids field in the tbl_stocks table but if there was a straightforward way to do it with my current database structure I would prefer that.

Thanks.

What database are you using?

MySQL

Is it a multiuser system where each user has their own category selections?

At the moment it’s just one list of categories but in future it could be user specific.

Store the multi-select values as a string array in the user table and retrieve using an IN clause.

I was trying to avoid storing the values as a string array.

Just use a standard junction table then. There’s few other reasonable options

How do I use that with the multi-select?

Use a repeat with the array of categoriy_ids passed in the form value to the server connect, adding each ID to the junction table with the selected stock_id

How do I pre-select the items that are already in tbl_stock_categories (junction table) .

image

So for the category select for stock_id 66 categories 1 and 2 should be selected when the form is opened.

Also - I’m updating so just adding those values to the tbl_stock_categories table won’t work. I guess I could just delete any current records and then add the new ones.

The multi selects expects an array of the values, returned by your data source in the Selected Values option.

Hey @Teodor. I get that, but for some reason I can’t figure out HOW to get that array.

It’s returned in a JSON array naturally - so you’ve got the IDs you need form the SELECT query. When posting the values back, you would first clear all the values then add the select_id items back

My understanding is that the multi select expects something like
“categories”: [1,2]

but the server connect query returns

“categories”: [
{
“category_id”: “1”
},
{
“category_id”: “2”
}
],

If you need a comma separated array or just a single value array you can use the flatten function followed by a split (split if none comma separated)

Single value array
get_all_users.flatten('id').split(',')

Comma separated
get_all_users.flatten('id')

1 Like

Thanks @Sorry_Duh . That was the answer!