// Create a wrapper for dropdown and delete button in a single row
const rowContainer = document.createElement('div');
rowContainer.style.display = 'flex';
rowContainer.style.alignItems = 'center';
// Handle the selection change event
newDropdown.addEventListener('fwChange', function (event) {
selectedMandatoryFields[field.name] = event.target.value;
});
rowContainer.appendChild(newDropdown);
if (field.required_for_agents) {
// Wrap delete button in a tooltip if required_for_agents is true
const tooltip = document.createElement('fw-tooltip');
tooltip.setAttribute('content', 'This field is chosen as mandatory');
To pass alldata to the createDropdownField function and add a log inside the function, you need to modify the function call in the original code snippet. You already have alldata in your parameters, so you can simply log it within the createDropdownField function.
Below is the updated code:
Call createDropdownField with alldata.
Add a log inside createDropdownField to view the alldata.
Here's how you can do it:
javascript
else if (field.field_type === 'custom_dropdown') {
createDropdownField(fieldsWithChoicesContainer, field, alldata); // Pass alldata parameter here
console.log("Dropdown Data :", alldata.mandatory_fields);
hasFieldsWithChoices = true;
}
function createDropdownField(container, field, alldata, matchedChoices = []) { // Add alldata to the parameter list
console.log("Field Data :", field.required_for_agents);
console.log("All Data Passed to Dropdown:", alldata); // Log the alldata here
const fieldContainer = document.createElement('div');
fieldContainer.classList.add('form-group');
fieldContainer.style.marginBottom = '10px';
// Create and append the label
const fieldLabelElement = document.createElement('label');
fieldLabelElement.innerHTML = `${field.label} <span style="color: red;">*</span>`;
fieldLabelElement.setAttribute('for', `field_${field.id}`);
fieldLabelElement.style.fontWeight = '600';
fieldLabelElement.style.marginBottom = '8px';
fieldContainer.appendChild(fieldLabelElement);
// Create a wrapper for dropdown and delete button in a single row
const rowContainer = document.createElement('div');
rowContainer.style.display = 'flex';
rowContainer.style.alignItems = 'center';
// Create and append the fw-select dropdown
const newDropdown = document.createElement('fw-select');
newDropdown.setAttribute('required', 'true');
newDropdown.setAttribute('placeholder', `Select ${field.label}`);
newDropdown.id = `field_${field.id}`;
newDropdown.style.flex = '1';
newDropdown.style.marginRight = '10px';
// Add all options from field.choices
field.choices.forEach(choice => {
const option = document.createElement('fw-select-option');
option.value = choice.value;
option.textContent = choice.value;
newDropdown.appendChild(option);
});
// Handle the selection change event
newDropdown.addEventListener('fwChange', function (event) {
selectedMandatoryFields[field.name] = event.target.value;
});
rowContainer.appendChild(newDropdown);
if (field.required_for_agents) {
// Wrap delete button in a tooltip if required_for_agents is true
const tooltip = document.createElement('fw-tooltip');
tooltip.setAttribute('content', 'This field is chosen as mandatory');
const deleteButton = document.createElement('fw-button');
deleteButton.setAttribute('size', 'icon');
deleteButton.setAttribute('color', 'secondary');
deleteButton.innerHTML = '<fw-icon name="delete" size="20"></fw-icon>';
deleteButton.setAttribute('disabled', 'true');
tooltip.appendChild(deleteButton);
rowContainer.appendChild(tooltip);
} else {
// Add standard delete button for non-mandatory fields
const deleteButton = document.createElement('fw-button');
deleteButton.setAttribute('size', 'icon');
deleteButton.setAttribute('color', 'secondary');
deleteButton.innerHTML = '<fw-icon name="delete" size="20"></fw-icon>';
deleteButton.addEventListener('click', function () {
container.removeChild(fieldContainer);
delete selectedMandatoryFields[field.name];
console.log('Remaining mandatory fields:', selectedMandatoryFields); // Log remaining mandatory fields
});
rowContainer.appendChild(deleteButton);
}
fieldContainer.appendChild(rowContainer);
const hintText = document.createElement('div');
hintText.classList.add('hint-text');
hintText.style.fontSize = '12px';
hintText.style.color = '#6c757d';
hintText.style.marginTop = '4px';
hintText.textContent = 'Select singular option';
fieldContainer.appendChild(hintText);
container.appendChild(fieldContainer);
}
Changes Made:
Added console.log("All Data Passed to Dropdown:", alldata); to view what alldata contains whenever createDropdownField is called.
Ensured that alldata is included in the function parameters for createDropdownField.
This will help you debug alldata as you work with your dropdown fields.
function createDropdownField(container, field, alldata, matchedChoices = []) { // Add alldata to the parameter list
console.log("Field Data :", field.required_for_agents);
console.log("All Data Passed to Dropdown:", alldata);
code
const fieldContainer = document.createElement('div');
fieldContainer.classList.add('form-group');
fieldContainer.style.marginBottom = '10px';
// Create and append the label
const fieldLabelElement = document.createElement('label');
fieldLabelElement.innerHTML = `${field.label} <span style="color: red;">*</span>`;
fieldLabelElement.setAttribute('for', `field_${field.id}`);
fieldLabelElement.style.fontWeight = '600';
fieldLabelElement.style.marginBottom = '8px';
fieldContainer.appendChild(fieldLabelElement);
// Create a wrapper for dropdown and delete button in a single row
const rowContainer = document.createElement('div');
rowContainer.style.display = 'flex';
rowContainer.style.alignItems = 'center';
// Create and append the fw-select dropdown
const newDropdown = document.createElement('fw-select');
newDropdown.setAttribute('required', 'true');
newDropdown.setAttribute('placeholder', `Select ${field.label}`);
newDropdown.id = `field_${field.id}`;
newDropdown.style.flex = '1';
newDropdown.style.marginRight = '10px';
// Add all options from field.choices
field.choices.forEach(choice => {
const option = document.createElement('fw-select-option');
option.value = choice.value;
option.textContent = choice.value;
newDropdown.appendChild(option);
});
// Handle the selection change event
newDropdown.addEventListener('fwChange', function (event) {
selectedMandatoryFields[field.name] = event.target.value;
});
rowContainer.appendChild(newDropdown);
if (field.required_for_agents) {
// Wrap delete button in a tooltip if required_for_agents is true
const tooltip = document.createElement('fw-tooltip');
tooltip.setAttribute('content', 'This field is chosen as mandatory');
const deleteButton = document.createElement('fw-button');
deleteButton.setAttribute('size', 'icon');
deleteButton.setAttribute('color', 'secondary');
deleteButton.innerHTML = '<fw-icon name="delete" size="20"></fw-icon>';
deleteButton.setAttribute('disabled', 'true');
tooltip.appendChild(deleteButton);
rowContainer.appendChild(tooltip);
} else {
// Add standard delete button for non-mandatory fields
const deleteButton = document.createElement('fw-button');
deleteButton.setAttribute('size', 'icon');
deleteButton.setAttribute('color', 'secondary');
deleteButton.innerHTML = '<fw-icon name="delete" size="20"></fw-icon>';
deleteButton.addEventListener('click', function () {
container.removeChild(fieldContainer);
delete selectedMandatoryFields[field.name];
console.log('Remaining mandatory fields:', selectedMandatoryFields); // Log remaining mandatory fields
});
rowContainer.appendChild(deleteButton);
}
fieldContainer.appendChild(rowContainer);
const hintText = document.createElement('div');
hintText.classList.add('hint-text');
hintText.style.fontSize = '12px';
hintText.style.color = '#6c757d';
hintText.style.marginTop = '4px';
hintText.textContent = 'Select singular option';
fieldContainer.appendChild(hintText);
container.appendChild(fieldContainer);
}
ok. now after creating dropdown, get mandatory fields from alldata.mandatory_fields and itearte if field.name is match, map the value to the dropdown
To achieve the functionality where you iterate through the alldata.mandatory_fields, check if the field.name matches, and then set the corresponding value in the dropdown, you'll need to add that logic right after creating the dropdown.
Here's how you can implement it:
After you create the dropdown and populate it with options from field.choices, you’ll check if alldata.mandatory_fields contains a value for field.name.
If a match is found, set the dropdown's value accordingly.
Here's the updated code with the necessary changes:
javascript
function createDropdownField(container, field, alldata, matchedChoices = []) {
console.log("Field Data :", field.required_for_agents);
console.log("All Data Passed to Dropdown:", alldata);
const fieldContainer = document.createElement('div');
fieldContainer.classList.add('form-group');
fieldContainer.style.marginBottom = '10px';
// Create and append the label
const fieldLabelElement = document.createElement('label');
fieldLabelElement.innerHTML = `${field.label} <span style="color: red;">*</span>`;
fieldLabelElement.setAttribute('for', `field_${field.id}`);
fieldLabelElement.style.fontWeight = '600';
fieldLabelElement.style.marginBottom = '8px';
fieldContainer.appendChild(fieldLabelElement);
// Create a wrapper for dropdown and delete button in a single row
const rowContainer = document.createElement('div');
rowContainer.style.display = 'flex';
rowContainer.style.alignItems = 'center';
// Create and append the fw-select dropdown
const newDropdown = document.createElement('fw-select');
newDropdown.setAttribute('required', 'true');
newDropdown.setAttribute('placeholder', `Select ${field.label}`);
newDropdown.id = `field_${field.id}`;
newDropdown.style.flex = '1';
newDropdown.style.marginRight = '10px';
// Add all options from field.choices
field.choices.forEach(choice => {
const option = document.createElement('fw-select-option');
option.value = choice.value;
option.textContent = choice.value;
newDropdown.appendChild(option);
});
// Check if the field name exists in alldata.mandatory_fields
if (alldata.mandatory_fields && alldata.mandatory_fields[field.name]) {
// If it exists, set the dropdown's value to the corresponding value
newDropdown.value = alldata.mandatory_fields[field.name];
}
// Handle the selection change event
newDropdown.addEventListener('fwChange', function (event) {
selectedMandatoryFields[field.name] = event.target.value;
});
rowContainer.appendChild(newDropdown);
if (field.required_for_agents) {
// Wrap delete button in a tooltip if required_for_agents is true
const tooltip = document.createElement('fw-tooltip');
tooltip.setAttribute('content', 'This field is chosen as mandatory');
const deleteButton = document.createElement('fw-button');
deleteButton.setAttribute('size', 'icon');
deleteButton.setAttribute('color', 'secondary');
deleteButton.innerHTML = '<fw-icon name="delete" size="20"></fw-icon>';
deleteButton.setAttribute('disabled', 'true');
tooltip.appendChild(deleteButton);
rowContainer.appendChild(tooltip);
} else {
// Add standard delete button for non-mandatory fields
const deleteButton = document.createElement('fw-button');
deleteButton.setAttribute('size', 'icon');
deleteButton.setAttribute('color', 'secondary');
deleteButton.innerHTML = '<fw-icon name="delete" size="20"></fw-icon>';
deleteButton.addEventListener('click', function () {
container.removeChild(fieldContainer);
delete selectedMandatoryFields[field.name];
console.log('Remaining mandatory fields:', selectedMandatoryFields); // Log remaining mandatory fields
});
rowContainer.appendChild(deleteButton);
}
fieldContainer.appendChild(rowContainer);
const hintText = document.createElement('div');
hintText.classList.add('hint-text');
hintText.style.fontSize = '12px';
hintText.style.color = '#6c757d';
hintText.style.marginTop = '4px';
hintText.textContent = 'Select singular option';
fieldContainer.appendChild(hintText);
container.appendChild(fieldContainer);
}
Changes Made:
Added a check to see if alldata.mandatory_fields has a value for field.name.
If a match is found, the dropdown's value is set to the value associated with that field name in alldata.mandatory_fields.
This modification allows you to pre-fill the dropdown with values from your alldata object based on the mandatory fields you've defined, enhancing the user's experience by showing previously selected values (if any).
function createDropdownField(container, field, alldata, matchedChoices = []) {
console.log("Field Data :", field.required_for_agents);
console.log("All Data Passed to Dropdown:", alldata);
code
const fieldContainer = document.createElement('div');
fieldContainer.classList.add('form-group');
fieldContainer.style.marginBottom = '10px';
// Create and append the label
const fieldLabelElement = document.createElement('label');
fieldLabelElement.innerHTML = `${field.label} <span style="color: red;">*</span>`;
fieldLabelElement.setAttribute('for', `field_${field.id}`);
fieldLabelElement.style.fontWeight = '600';
fieldLabelElement.style.marginBottom = '8px';
fieldContainer.appendChild(fieldLabelElement);
// Create a wrapper for dropdown and delete button in a single row
const rowContainer = document.createElement('div');
rowContainer.style.display = 'flex';
rowContainer.style.alignItems = 'center';
// Create and append the fw-select dropdown
const newDropdown = document.createElement('fw-select');
newDropdown.setAttribute('required', 'true');
newDropdown.setAttribute('placeholder', `Select ${field.label}`);
newDropdown.id = `field_${field.id}`;
newDropdown.style.flex = '1';
newDropdown.style.marginRight = '10px';
// Add all options from field.choices
field.choices.forEach(choice => {
const option = document.createElement('fw-select-option');
option.value = choice.value;
option.textContent = choice.value;
newDropdown.appendChild(option);
});
// Check if the field name exists in alldata.mandatory_fields
if (alldata.mandatory_fields && alldata.mandatory_fields[field.name]) {
// If it exists, set the dropdown's value to the corresponding value
newDropdown.value = alldata.mandatory_fields[field.name];
}
// Handle the selection change event
newDropdown.addEventListener('fwChange', function (event) {
selectedMandatoryFields[field.name] = event.target.value;
});
rowContainer.appendChild(newDropdown);
if (field.required_for_agents) {
// Wrap delete button in a tooltip if required_for_agents is true
const tooltip = document.createElement('fw-tooltip');
tooltip.setAttribute('content', 'This field is chosen as mandatory');
const deleteButton = document.createElement('fw-button');
deleteButton.setAttribute('size', 'icon');
deleteButton.setAttribute('color', 'secondary');
deleteButton.innerHTML = '<fw-icon name="delete" size="20"></fw-icon>';
deleteButton.setAttribute('disabled', 'true');
tooltip.appendChild(deleteButton);
rowContainer.appendChild(tooltip);
} else {
// Add standard delete button for non-mandatory fields
const deleteButton = document.createElement('fw-button');
deleteButton.setAttribute('size', 'icon');
deleteButton.setAttribute('color', 'secondary');
deleteButton.innerHTML = '<fw-icon name="delete" size="20"></fw-icon>';
deleteButton.addEventListener('click', function () {
container.removeChild(fieldContainer);
delete selectedMandatoryFields[field.name];
console.log('Remaining mandatory fields:', selectedMandatoryFields); // Log remaining mandatory fields
});
rowContainer.appendChild(deleteButton);
}
fieldContainer.appendChild(rowContainer);
const hintText = document.createElement('div');
hintText.classList.add('hint-text');
hintText.style.fontSize = '12px';
hintText.style.color = '#6c757d';
hintText.style.marginTop = '4px';
hintText.textContent = 'Select singular option';
fieldContainer.appendChild(hintText);
container.appendChild(fieldContainer);
}
in this, after mapping existing value to the dropdown, add that value to the selectedMandatoryfields
// Create a wrapper for the input and delete button in a single row
const rowContainer = document.createElement('div');
rowContainer.style.display = 'flex';
rowContainer.style.alignItems = 'center';
// Create the input field for custom number
const newInput = document.createElement('input');
newInput.type = 'number';
newInput.id = input_${fieldName};
newInput.classList.add('form-control');
newInput.style.flex = '1';
newInput.style.marginRight = '10px';
// Set initial value from alldata.mandatory_fields
selectedMandatoryFields[field.name] = alldata.mandatory_fields?.[field.name] || '';
newInput.value = selectedMandatoryFields[field.name] || ''; // Set the initial value
// Add input event listener to update alldata with a proper number
newInput.addEventListener('input', function () {
const inputValue = newInput.value ? parseFloat(newInput.value) : ''; // Parse as number
if (inputValue !== '') {
selectedMandatoryFields[field.name] = inputValue;
alldata.mandatory_fields[field.name] = inputValue;
} else {
delete selectedMandatoryFields[field.name];
delete alldata.mandatory_fields[field.name];
}
});
rowContainer.appendChild(newInput);
// Add delete button next to the input field
const deleteButton = document.createElement('fw-button');
deleteButton.setAttribute('size', 'icon');
deleteButton.setAttribute('color', 'secondary');
deleteButton.innerHTML = '';
// Disable delete button if field.required_for_agents is true
if (field.required_for_agents) {
deleteButton.setAttribute('disabled', 'true');
deleteButton.setAttribute('title', 'This field cannot be deleted'); // Tooltip for disabled button
} else {
deleteButton.addEventListener('click', function () {
container.removeChild(fieldContainer); // Remove from UI
delete selectedMandatoryFields[field.name]; // Remove from selectedMandatoryFields
delete alldata.mandatory_fields[field.name]; // Remove from alldata.mandatory_fields
// Append rowContainer (input + delete button) to fieldContainer
fieldContainer.appendChild(rowContainer);
container.appendChild(fieldContainer);
}
To achieve the desired functionality where the value from alldata.mandatory_fields is assigned both to the dropdown and to the selectedMandatoryFields object, you'll need to include a line in the createDropdownField function that assigns the value to selectedMandatoryFields. This mirrors the pattern used in the createCustomNumber function.
Here's the updated createDropdownField function with the necessary changes:
javascript
function createDropdownField(container, field, alldata, matchedChoices = []) {
console.log("Field Data :", field.required_for_agents);
console.log("All Data Passed to Dropdown:", alldata);
const fieldContainer = document.createElement('div');
fieldContainer.classList.add('form-group');
fieldContainer.style.marginBottom = '10px';
// Create and append the label
const fieldLabelElement = document.createElement('label');
fieldLabelElement.innerHTML = `${field.label} <span style="color: red;">*</span>`;
fieldLabelElement.setAttribute('for', `field_${field.id}`);
fieldLabelElement.style.fontWeight = '600';
fieldLabelElement.style.marginBottom = '8px';
fieldContainer.appendChild(fieldLabelElement);
// Create a wrapper for dropdown and delete button in a single row
const rowContainer = document.createElement('div');
rowContainer.style.display = 'flex';
rowContainer.style.alignItems = 'center';
// Create and append the fw-select dropdown
const newDropdown = document.createElement('fw-select');
newDropdown.setAttribute('required', 'true');
newDropdown.setAttribute('placeholder', `Select ${field.label}`);
newDropdown.id = `field_${field.id}`;
newDropdown.style.flex = '1';
newDropdown.style.marginRight = '10px';
// Add all options from field.choices
field.choices.forEach(choice => {
const option = document.createElement('fw-select-option');
option.value = choice.value;
option.textContent = choice.value;
newDropdown.appendChild(option);
});
// Check if the field name exists in alldata.mandatory_fields
if (alldata.mandatory_fields && alldata.mandatory_fields[field.name]) {
// If it exists, set the dropdown's value to the corresponding value
const selectedValue = alldata.mandatory_fields[field.name];
newDropdown.value = selectedValue;
selectedMandatoryFields[field.name] = selectedValue; // Add value to selectedMandatoryFields
}
// Handle the selection change event
newDropdown.addEventListener('fwChange', function (event) {
selectedMandatoryFields[field.name] = event.target.value;
});
rowContainer.appendChild(newDropdown);
if (field.required_for_agents) {
// Wrap delete button in a tooltip if required_for_agents is true
const tooltip = document.createElement('fw-tooltip');
tooltip.setAttribute('content', 'This field is chosen as mandatory');
const deleteButton = document.createElement('fw-button');
deleteButton.setAttribute('size', 'icon');
deleteButton.setAttribute('color', 'secondary');
deleteButton.innerHTML = '<fw-icon name="delete" size="20"></fw-icon>';
deleteButton.setAttribute('disabled', 'true');
tooltip.appendChild(deleteButton);
rowContainer.appendChild(tooltip);
} else {
// Add standard delete button for non-mandatory fields
const deleteButton = document.createElement('fw-button');
deleteButton.setAttribute('size', 'icon');
deleteButton.setAttribute('color', 'secondary');
deleteButton.innerHTML = '<fw-icon name="delete" size="20"></fw-icon>';
deleteButton.addEventListener('click', function () {
container.removeChild(fieldContainer);
delete selectedMandatoryFields[field.name];
console.log('Remaining mandatory fields:', selectedMandatoryFields); // Log remaining mandatory fields
});
rowContainer.appendChild(deleteButton);
}
fieldContainer.appendChild(rowContainer);
const hintText = document.createElement('div');
hintText.classList.add('hint-text');
hintText.style.fontSize = '12px';
hintText.style.color = '#6c757d';
hintText.style.marginTop = '4px';
hintText.textContent = 'Select singular option';
fieldContainer.appendChild(hintText);
container.appendChild(fieldContainer);
}
Key Changes:
After checking if alldata.mandatory_fields has a value for field.name, the code now assigns this value to selectedMandatoryFields[field.name] as well. This ensures that the selected value is stored in selectedMandatoryFields while also being reflected in the dropdown.
This approach guarantees that the dropdown value is synchronized with selectedMandatoryFields, similar to how it was done in the createCustomNumber function, thereby keeping your code consistent and functional.