Skip to content

Commit b32ad90

Browse files
committed
better docs for multi-select
1 parent ea5f741 commit b32ad90

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

examples/official-site/sqlpage/migrations/01_documentation.sql

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
247247
('min', 'The minimum value to accept for an input of type number', 'NUMBER', FALSE, TRUE),
248248
('max', 'The minimum value to accept for an input of type number', 'NUMBER', FALSE, TRUE),
249249
('checked', 'Used only for checkboxes and radio buttons. Indicates whether the checkbox should appear as already checked.', 'BOOL', FALSE, TRUE),
250-
('multiple', 'Used only for select elements. Indicates that multiple elements can be selected simultaneously.', 'BOOL', FALSE, TRUE),
250+
('multiple', 'Used only for select elements. Indicates that multiple elements can be selected simultaneously. When using multiple, you should add square brackets after the variable name: ''my_variable[]'' as name', 'BOOL', FALSE, TRUE),
251251
('step', 'The increment of values in an input of type number. Set to 1 to allow only integers.', 'NUMBER', FALSE, TRUE),
252252
('description', 'A helper text to display near the input field.', 'TEXT', FALSE, TRUE),
253253
('pattern', 'A regular expression that the value must match. For instance, [0-9]{3} will only accept 3 digits.', 'TEXT', FALSE, TRUE),
@@ -316,6 +316,26 @@ FROM fruits
316316
'{"name": "Fruit", "type": "select", "value": 1, "options": '||
317317
'"[{\"label\": \"Orange\", \"value\": 0}, {\"label\": \"Apple\", \"value\": 1}, {\"label\": \"Banana\", \"value\": 3}]"}
318318
]')),
319+
('form', '### Multi-select
320+
You can authorize the user to select multiple options by setting the `multiple` property to `true`.
321+
This creates a more compact (but arguably less user-friendly) alternative to a series of checkboxes.
322+
In this case, you should add square brackets to the name of the field.
323+
The target page will then receive the value as a JSON array of strings, which you can iterate over using
324+
- the `json_each` function [in SQLite](https://www.sqlite.org/json1.html) and [Postgres](https://www.postgresql.org/docs/9.3/functions-json.html),
325+
- the [`JSON_TABLE`](https://dev.mysql.com/doc/refman/8.0/en/json-table-functions.html) function in MySQL (which you''ll need to wrap in a function, because SQLPage cannot parse the non-standard syntax of this function)
326+
- the [`OPENJSON`](https://learn.microsoft.com/fr-fr/sql/t-sql/functions/openjson-transact-sql?view=sql-server-ver16) function in Microsoft SQL Server.
327+
328+
The target page could then look like this:
329+
330+
```sql
331+
insert into best_fruits(id) -- INSERT INTO ... SELECT ... runs the SELECT query and inserts the results into the table
332+
select CAST(value AS integer) as id -- all values are transmitted by the browser as strings
333+
from json_each($preferred_fruits); -- json_each returns a table with a "value" column for each element in the JSON array
334+
```
335+
', json('[{"component":"form"},
336+
{"name": "Fruit", "type": "select", "multiple": true, "description": "press ctrl to select multiple values", "options":
337+
"[{\"label\": \"Orange\", \"value\": 0}, {\"label\": \"Apple\", \"value\": 1}, {\"label\": \"Banana\", \"value\": 3}]"}
338+
]')),
319339
('form', 'This example illustrates the use of the `radio` type.
320340
The `name` parameter is used to group the radio buttons together.
321341
The `value` parameter is used to set the value that will be submitted when the user selects the radio button.

0 commit comments

Comments
 (0)