|
| 1 | +--- |
| 2 | +rows: |
| 3 | + - component: form |
| 4 | + action: examples/show_variables.sql |
| 5 | + reset: Reset |
| 6 | + - name: component |
| 7 | + type: select |
| 8 | + value: form |
| 9 | + options: |
| 10 | + - label: Form |
| 11 | + value: form |
| 12 | + options_source: examples/from_component_options_source.sql?category=component |
| 13 | + description: Start typing the name of a component like 'map' or 'form'... |
| 14 | +--- |
| 15 | + |
| 16 | +### Multi-select |
| 17 | +You can authorize the user to select multiple options by setting the `multiple` property to `true`. |
| 18 | +This creates a more compact (but arguably less user-friendly) alternative to a series of checkboxes. |
| 19 | +In this case, you should add square brackets to the name of the field (e.g. `'my_field[]' as name`). |
| 20 | +The target page will then receive the value as a JSON array of strings, which you can iterate over using |
| 21 | + - the `json_each` function [in SQLite](https://www.sqlite.org/json1.html) and [Postgres](https://www.postgresql.org/docs/9.3/functions-json.html), |
| 22 | + - the [`OPENJSON`](https://learn.microsoft.com/fr-fr/sql/t-sql/functions/openjson-transact-sql?view=sql-server-ver16) function in Microsoft SQL Server. |
| 23 | + - in MySQL, json manipulation is less straightforward: see [the SQLPage MySQL json example](https://github.com/sqlpage/SQLPage/tree/main/examples/mysql%20json%20handling) |
| 24 | + |
| 25 | +[More information on how to handle JSON in SQL](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide). |
| 26 | + |
| 27 | +The target page could then look like this: |
| 28 | + |
| 29 | +```sql |
| 30 | +insert into best_fruits(id) -- INSERT INTO ... SELECT ... runs the SELECT query and inserts the results into the table |
| 31 | +select CAST(value AS integer) as id -- all values are transmitted by the browser as strings |
| 32 | +from json_each($my_field); -- in SQLite, json_each returns a table with a "value" column for each element in the JSON array |
| 33 | +``` |
| 34 | + |
| 35 | +### Example multiselect generated from a database table |
| 36 | + |
| 37 | +If you have a table of all possible options (`my_options(id int, label text)`), |
| 38 | +and want to generate a multi-select field from it, you have two options: |
| 39 | +- if the number of options is not too large, you can use the `options` parameter to return them all as a JSON array in the SQL query |
| 40 | +- if the number of options is large (e.g. more than 1000), you can use `options_source` to load options dynamically from a different SQL query as the user types |
| 41 | + |
| 42 | +#### Embedding all options in the SQL query |
| 43 | + |
| 44 | +Let's say you have a table that contains the selected options per user (`my_user_options(user_id int, option_id int)`). |
| 45 | +You can use a query like this to generate the multi-select field: |
| 46 | + |
| 47 | +```sql |
| 48 | +select 'select' as type, true as multiple, json_group_array(json_object( |
| 49 | + 'label', my_options.label, |
| 50 | + 'value', my_options.id, |
| 51 | + 'selected', my_user_options.option_id is not null |
| 52 | +)) as options |
| 53 | +from my_options |
| 54 | +left join my_user_options |
| 55 | + on my_options.id = my_user_options.option_id |
| 56 | + and my_user_options.user_id = $user_id |
| 57 | +``` |
| 58 | + |
| 59 | +This will generate a json array of objects, each containing the label, value and selected status of each option. |
| 60 | + |
| 61 | +#### Loading options dynamically from a different SQL query with `options_source` |
| 62 | + |
| 63 | +If the `my_options` table has a large number of rows, you can use the `options_source` parameter to load options dynamically from a different SQL query as the user types. |
| 64 | + |
| 65 | +We'll write a second SQL file, `options_source.sql`, that will receive the user's search string as a parameter named `$search`, |
| 66 | + and return a json array of objects, each containing the label and value of each option. |
| 67 | + |
| 68 | +When both `options` and `options_source` are set, the local `options` are loaded first. Search results from `options_source` are loaded into the same option list; a result with the same `value` updates the existing option. This is useful to set a default preselected value with `options_source`. |
| 69 | + |
| 70 | +`options_source` may already contain query parameters. SQLPage preserves them and adds or replaces the `search` parameter when loading options. |
| 71 | + |
| 72 | +##### `options_source.sql` |
| 73 | + |
| 74 | +```sql |
| 75 | +select 'json' as component; |
| 76 | + |
| 77 | +select id as value, label as label |
| 78 | +from my_options |
| 79 | +where label like $search || '%'; |
| 80 | +``` |
| 81 | + |
| 82 | +##### `form` |
0 commit comments