Load default values into rendered form

This commit is contained in:
Oliver 2021-06-27 21:58:22 +10:00
parent d80948369b
commit 0e9b82c475
2 changed files with 52 additions and 1 deletions

View File

@ -46,6 +46,11 @@
fields: [
'name',
'description',
'website',
'address',
'phone',
'email',
'contact',
'is_supplier',
'is_manufacturer',
'is_customer',

View File

@ -110,6 +110,16 @@ function getApiEndpointOptions(url, callback, options) {
*/
function constructCreateForm(fields, options) {
// Check if default values were provided for any fields
for (const name in fields) {
var field = fields[name];
if (field.default != null) {
field.value = field.default;
}
}
// We should have enough information to create the form!
constructFormBody(fields, options);
}
@ -320,6 +330,8 @@ function constructFormBody(fields, options) {
$(modal).modal('show');
updateFieldValues(fields, options);
// Setup related fields
initializeRelatedFields(fields, options)
@ -387,6 +399,41 @@ function submitFormData(fields, options) {
}
/*
* Update (set) the field values based on the specified data.
*
* Iterate through each of the displayed fields,
* and set the 'val' attribute of each one.
*
*/
function updateFieldValues(fields, options) {
for (var idx = 0; idx < options.field_names.length; idx++) {
var name = options.field_names[idx];
var field = fields[name] || null;
if (field == null) { continue; }
var value = field.value || field.default || null;
if (value == null) { continue; }
var el = $(options.modal).find(`#id_${name}`);
switch (field.type) {
case 'boolean':
el.prop('checked', value);
break;
default:
el.val(value);
break;
}
}
}
/*
* Extract and field value before sending back to the server
*
@ -409,7 +456,6 @@ function getFormFieldValue(name, field, options) {
}
/*
* Handle successful form posting
*