Skip to content
Snippets Groups Projects
Commit ff198de8 authored by Elmar Ludwig's avatar Elmar Ludwig Committed by Jan-Hendrik Willms
Browse files

add data-formaction attribute for submit-upon-select, fixes #3115

Closes #3115

Merge request studip/studip!2097
parent 583cd98f
Branches
No related tags found
No related merge requests found
...@@ -77,20 +77,26 @@ class OptionsWidget extends ListWidget ...@@ -77,20 +77,26 @@ class OptionsWidget extends ListWidget
*/ */
public function addSelect($label, $url, $name, $options, $selected_option = false, $attributes = []) public function addSelect($label, $url, $name, $options, $selected_option = false, $attributes = [])
{ {
$widget = new SelectWidget($label, $url, $name); $option_content = '';
$widget->layout = false;
foreach ($options as $value => $option_label) { foreach ($options as $value => $option) {
$widget->addElement(new SelectElement($value, $option_label, $value === $selected_option)); $selected = $value === $selected_option ? 'selected' : '';
$option_content .= sprintf(
'<option value="%s" %s>%s</option>',
htmlReady($value),
$selected,
htmlReady($option)
);
} }
if (isset($widget->attributes) && is_array($widget->attributes)) { $content = sprintf(
$widget->attributes = array_merge($widget->attributes, $attributes); '<select data-formaction="%s" class="sidebar-selectlist submit-upon-select" name="%s" aria-label="%s" %s>%s</select>',
} else { htmlReady($url),
$widget->attributes = $attributes; htmlReady($name),
} htmlReady($label),
arrayToHtmlAttributes($attributes),
$content = $widget->render(); $option_content
);
$this->addElement(new WidgetElement($content)); $this->addElement(new WidgetElement($content));
} }
......
...@@ -107,6 +107,9 @@ $(document) ...@@ -107,6 +107,9 @@ $(document)
// Submit only if value has changed and either enter was pressed or // Submit only if value has changed and either enter was pressed or
// select was opened by click // select was opened by click
if (!is_default && shouldSubmit) { if (!is_default && shouldSubmit) {
if ($(this).data('formaction')) {
$(this.form).attr('action', $(this).data('formaction'));
}
$(this.form).submit(); $(this.form).submit();
$('option', this).prop('defaultSelected', false).filter(':selected').prop('defaultSelected', true); $('option', this).prop('defaultSelected', false).filter(':selected').prop('defaultSelected', true);
return false; return false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment