Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetching the multivalued_field_param_name from the multivalued_value … #279

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions js/mmoda.instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -1422,11 +1422,14 @@ function panel_title(srcname, param) {
return (item);
});
var instrument_form_serializeJSON = $($(this)[0]).serializeJSON();
//var instrument_form_serializeArray = $($(this)[0]).serializeArray();
var instrument_form_serializeArray = [];
multival_pars = $.unique($.map($(this).find('.multivalued-value .form-control'), (x) => x.name.split('[')[0]));
$.each(instrument_form_serializeJSON, function(param, value) {
if (multival_pars.includes(param)) instrument_form_serializeArray.push({ 'name': param, 'value': JSON.stringify(value) });
if (multival_pars.includes(param)) {
let multivalued_param_element = $(`.multivalued-value .form-control[name^="${param}"]`);
let multivalued_param_name = multivalued_param_element.attr('multivalued_field_param_name') || multivalued_param_element.attr('name');
instrument_form_serializeArray.push({ 'name': multivalued_param_name, 'value': JSON.stringify(value) });
}
else instrument_form_serializeArray.push({ 'name': param, 'value': value });
});

Expand Down Expand Up @@ -1604,31 +1607,35 @@ function panel_title(srcname, param) {

$('div.multivalued-field', 'form.' + request_parameters.instrument + '-form') .each(function() {
// get name of the first select element
var re_multivalued_field = new RegExp('\\[[^\\]]*\\]\\[\\]');
var field_name = $('select', this).attr('name').replace(re_multivalued_field, '');
let add_multivalued_button = $(this).find('.add-multivalued-element');
if (request_parameters.hasOwnProperty(field_name)) {
var field_values = request_parameters[field_name];
let parsed_field_obj = JSON.parse(field_values);
// for each key in the object, add a new multivalued field
let parsed_field_obj_keys = Object.keys(parsed_field_obj);
let num_multivalued_field = parsed_field_obj[parsed_field_obj_keys[0]].length;
for (let i = 0; i < num_multivalued_field ; i++) {
for(key of parsed_field_obj_keys) {
var select_list = $('select', this).filter(function() {
return $(this).attr('name') === `${field_name}[${key}][]`;
});
// check if the select has the value amongst its options
let select_element = $(select_list[select_list.length - 1]);
let hasOption = select_element.find('option').filter(function() {
return $(this).val() === parsed_field_obj[key][i];
}).length > 0;
if (!hasOption)
select_element.append(new Option(parsed_field_obj[key][i], parsed_field_obj[key][i]));
select_element.val(parsed_field_obj[key][i]);
let multivalued_field_product_name = $('input', this).attr('multivalued_field_product_name') ? $('input', this).attr('multivalued_field_product_name') : undefined;
if(request_parameters.hasOwnProperty("product_type") && multivalued_field_product_name == request_parameters.product_type) {
var re_multivalued_field = new RegExp('\\[[^\\]]*\\]\\[\\]');
let multivalued_field_name = $('input', this).attr('multivalued_field_param_name') ? $('input', this).attr('multivalued_field_param_name') : $('input', this).attr('name');
var field_name = $('input', this).attr('name').replace(re_multivalued_field, '');
let add_multivalued_button = $(this).find('.add-multivalued-element');
if (request_parameters.hasOwnProperty(multivalued_field_name)) {
var multivalued_field_values = request_parameters[multivalued_field_name];
let parsed_field_obj = JSON.parse(multivalued_field_values);
// for each key in the object, add a new multivalued field
let parsed_field_obj_keys = Object.keys(parsed_field_obj);
let num_multivalued_field = parsed_field_obj[parsed_field_obj_keys[0]].length;
for (let i = 0; i < num_multivalued_field ; i++) {
for(key of parsed_field_obj_keys) {
var select_list = $(this).find('input, select').filter(function() {
return $(this).attr('name') === `${field_name}[${key}][]`;
});
// check if the select has the value amongst its options
let select_element = $(select_list[select_list.length - 1]);
let hasOption = select_element.find('option').filter(function() {
return $(this).val() === parsed_field_obj[key][i];
}).length > 0;
if (!hasOption)
select_element.append(new Option(parsed_field_obj[key][i], parsed_field_obj[key][i]));
select_element.val(parsed_field_obj[key][i]);
}
if($('.multivalued-value', this).length < num_multivalued_field)
insert_new_multivalued_field(add_multivalued_button[0]);
}
if($('.multivalued-value', this).length < num_multivalued_field)
insert_new_multivalued_field(add_multivalued_button[0]);
}
}
});
Expand Down