Skip to content

Commit 69b3207

Browse files
committed
new auto_submit parameter in the form component
1 parent f58befe commit 69b3207

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
- The previous behavior is preserved (the `.sql` suffix is now optional), so this is a new feature, not a breaking change.
1212
- Big thanks to @guspower who made big contributions to this feature.
1313
- 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+
- 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+
- 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.
1416

1517
## 0.32.1 (2025-01-03)
1618

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ INSERT INTO parameter(component, name, description, type, top_level, optional) S
274274
('validate_outline', 'A color to outline the validation button.', 'COLOR', TRUE, TRUE),
275275
('reset', 'The text to display in the button at the bottom of the form that resets the form to its original state. Omit this property not to show a reset button at all.', 'TEXT', TRUE, TRUE),
276276
('id', 'A unique identifier for the form, which can then be used to validate the form from a button outside of the form.', 'TEXT', TRUE, TRUE),
277+
('auto_submit', 'Automatically submit the form when the user changes any of its fields, and remove the validation button.', 'BOOLEAN', TRUE, TRUE),
277278
-- item level
278279
('type', 'The type of input to use: text for a simple text field, textarea for a multi-line text input control, number to accept only numbers, checkbox or radio for a button that is part of a group specified in the ''name'' parameter, hidden for a value that will be submitted but not shown to the user. text by default.', 'TEXT', FALSE, TRUE),
279280
('name', 'The name of the input field, that you can use in the target page to get the value the user entered for the field.', 'TEXT', FALSE, FALSE),
@@ -436,6 +437,18 @@ In this example, depending on what the user clicks, the page will be reloaded wi
436437
{"name": "component", "type": "radio", "value": "map", "checked": true, "description": "Display a map based on database data", "label": "Map"},
437438
{"name": "component", "type": "radio", "value": "chart", "description": "Interactive plots of SQL query results", "label": "Chart"}
438439
]')),
440+
('form', '
441+
### Dynamically refresh the page when the user changes the form
442+
443+
The form will be automatically submitted when the user changes any of its fields, and the page will be reloaded with the new value.
444+
The validation button is removed.
445+
', json('[{"component":"form", "auto_submit": true},
446+
{"name": "component", "type": "select", "autocomplete": false, "options": [
447+
{"label": "Form", "value": "form", "selected": true},
448+
{"label": "Map", "value": "map"},
449+
{"label": "Chart", "value": "chart"}
450+
], "description": "Choose a component to view its documentation. No need to click a button, the page will be reloaded automatically.", "label": "Component"}
451+
]')),
439452
('form', 'When you want to include some information in the form data, but not display it to the user, you can use a hidden field.
440453
441454
This can be used to track simple data such as the current user''s id,

sqlpage/sqlpage.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ function sqlpage_form() {
216216
}
217217
});
218218
}
219+
220+
const auto_submit_forms = document.querySelectorAll("form[data-auto-submit]");
221+
for (const form of auto_submit_forms) {
222+
form.addEventListener("change", () => form.submit());
223+
}
219224
}
220225

221226
function get_tabler_color(name) {

sqlpage/templates/form.handlebars

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{{else}}
88
{{#if id}}action="#{{id}}"{{/if}}
99
{{/if}}
10+
{{#if auto_submit}}data-auto-submit{{/if}}
1011
>
1112
<fieldset class="form-fieldset mb-1">
1213
{{#if title}}
@@ -130,7 +131,7 @@
130131
{{/if}}
131132
{{/each_row}}
132133
</div>
133-
{{#if (ne validate '')}}
134+
{{#if (and (ne validate '') (not auto_submit))}}
134135
<input class="btn
135136
btn-{{default validate_color "primary"}}
136137
{{#if validate_shape}} btn-{{validate_shape}} {{/if}}

0 commit comments

Comments
 (0)