Reload settings values "live" (without a page refresh)

This commit is contained in:
Oliver 2021-07-27 17:03:37 +10:00
parent 46d5a6f00b
commit 990b987692
3 changed files with 47 additions and 10 deletions

View File

@ -50,6 +50,23 @@ class SettingEdit(AjaxUpdateView):
return ctx
def get_data(self):
"""
Custom data to return to the client after POST success
"""
data = {}
setting = self.get_object()
data['pk'] = setting.pk
data['key'] = setting.key
data['value'] = setting.value
data['is_bool'] = setting.is_bool()
data['is_int'] = setting.is_int()
return data
def get_form(self):
"""
Override default get_form behaviour

View File

@ -17,16 +17,21 @@
<td>
{% if setting.is_bool %}
<div>
<input fieldname='{{ setting.key }}' class='slidey' type='checkbox' data-offstyle='warning' data-onstyle='success' data-size='small' data-toggle='toggle' disabled autocomplete='off' {% if setting.as_bool %}checked=''{% endif %}>
<input fieldname='{{ setting.key }}' id='setting-value-{{ setting.key }}' type='checkbox' disabled='' {% if setting.as_bool %}checked=''{% endif %}>
</div>
{% else %}
<div id='setting-{{ setting.pk }}'>
<b>
<span id='setting-value-{{ setting.key }}'>
{% if setting.value %}
<i><b>
{{ setting.value }}</b> {{ setting.units }}
</i>
{{ setting.value }}
{% else %}
<i>{% trans "No value set" %}</i>
{% endif %}
</span>
</b>
{{ setting.units }}
</div>
{% endif %}
<td>
{% trans setting.description %}

View File

@ -17,7 +17,7 @@
{% include "InvenTree/settings/user.html" %}
{% include "InvenTree/settings/user_settings.html" %}
{% include "InvenTree/settings/user_homepage.html" %}
{% include "InvenTree/settings/user_search.html" %}
{% include "InvenTree/settings/user_homepage.html" %}
{% if user.is_staff %}
@ -54,7 +54,22 @@ $('table').find('.btn-edit-setting').click(function() {
launchModalForm(
url,
{
reload: true,
success: function(response) {
console.log(`setting ${pk} -> ${response.value}`);
if (response.is_bool) {
var enabled = response.value.toLowerCase() == 'true';
console.log("bool result:", enabled);
$(`#setting-value-${setting}`).prop('checked', enabled);
} else {
$(`#setting-value-${setting}`).html(response.value);
console.log("Normal result:", response.value);
}
}
}
);
});