You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,8 @@
13
13
- Update ApexCharts to [v4.4.0](https://github.com/apexcharts/apexcharts.js/releases/tag/v4.4.0): fixes multiple small bugs in the chart component.
14
14
- Add a new `auto_submit` parameter to the form component. When set to true, the form will be automatically submitted when the user changes any of its fields, and the page will be reloaded with the new value. The validation button is removed.
15
15
- This is useful to quickly create filters at the top of a dashboard or report page, that will be automatically applied when the user changes them.
16
+
- New `options_source` parameter in the form component. This allows to dynamically load options for dropdowns from a different SQL file.
17
+
- This allows easily implementing autocomplete for form fields with a large number of possible options.
Copy file name to clipboardExpand all lines: examples/official-site/sqlpage/migrations/01_documentation.sql
+37-8Lines changed: 37 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -375,7 +375,7 @@ FROM fruits
375
375
('form', '### Multi-select
376
376
You can authorize the user to select multiple options by setting the `multiple` property to `true`.
377
377
This creates a more compact (but arguably less user-friendly) alternative to a series of checkboxes.
378
-
In this case, you should add square brackets to the name of the field.
378
+
In this case, you should add square brackets to the name of the field (e.g. `''my_field[]'' as name`).
379
379
The target page will then receive the value as a JSON array of strings, which you can iterate over using
380
380
- the `json_each` function [in SQLite](https://www.sqlite.org/json1.html) and [Postgres](https://www.postgresql.org/docs/9.3/functions-json.html),
381
381
- the [`OPENJSON`](https://learn.microsoft.com/fr-fr/sql/t-sql/functions/openjson-transact-sql?view=sql-server-ver16) function in Microsoft SQL Server.
@@ -388,14 +388,20 @@ The target page could then look like this:
388
388
```sql
389
389
insert into best_fruits(id) -- INSERT INTO ... SELECT ... runs the SELECT query and inserts the results into the table
390
390
select CAST(value AS integer) as id -- all values are transmitted by the browser as strings
391
-
from json_each($preferred_fruits); -- json_each returns a table with a "value" column for each element in the JSON array
391
+
from json_each($my_field); -- in SQLite, json_each returns a table with a "value" column for each element in the JSON array
392
392
```
393
393
394
394
### Example multiselect generated from a database table
395
395
396
-
As an example, if you have a table of all possible options (`my_options(id int, label text)`),
397
-
and another table that contains the selected options per user (`my_user_options(user_id int, option_id int)`),
398
-
you can use a query like this to generate the multi-select field:
396
+
If you have a table of all possible options (`my_options(id int, label text)`),
397
+
and want to generate a multi-select field from it, you have two options:
398
+
- 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
399
+
- 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
400
+
401
+
#### Embedding all options in the SQL query
402
+
403
+
Let''s say you have a table that contains the selected options per user (`my_user_options(user_id int, option_id int)`).
404
+
You can use a query like this to generate the multi-select field:
399
405
400
406
```sql
401
407
select ''select'' as type, true as multiple, json_group_array(json_object(
@@ -408,10 +414,33 @@ left join my_user_options
408
414
on my_options.id = my_user_options.option_id
409
415
and my_user_options.user_id = $user_id
410
416
```
417
+
418
+
This will generate a json array of objects, each containing the label, value and selected status of each option.
419
+
420
+
#### Loading options dynamically from a different SQL query with `options_source`
421
+
422
+
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.
423
+
424
+
We''ll write a second SQL file, `options_source.sql`, that will receive the user''s search string as a parameter named `$search`,
425
+
and return a json array of objects, each containing the label and value of each option.
`Invalid response from "${options_source}". The response must be an array of objects with a 'label' and a 'value' property. Got: ${JSON.stringify(results[0])} in the first object instead.`,
0 commit comments