I would like to preface this by stating that I only started Salesforce development about a month ago so forgive me if this an obvious questions.
Here is my problem:
I currently use an apex method to get field data from a fieldset in a JSON string and would like to display these fields and their picklist options in lightning comboboxes. I cannot assume the number of fields in the fieldset or the type of the fields or the values for the picklist.
I see that in the documentation (https://developer.salesforce.com/docs/component-library/bundle/lightning-combobox/example) the options for the combobox are directly pulled from a variable in the javascript but I would like to make it such that they are static and can be contructed in javascript.
I have tried using 'for:each' and directly manipulating the JSON string of the fields in the html, but no luck. Instead I am opting to loop through the fields in the JSON and would like to construct the combobox while looping and add it to the DOM. Is it possible to create the combo box from the javascript and add it to the DOM? Also, what are the best practices when wanting to add elements to the DOM.
questions.js
@wire(getQuestions, {objectName: 'Questions', fieldSetName: 'DND'})
questions;
if (questions) {
const obj = JSON.parse(questions);
var field = obj[0]
var label = field.fieldLabel;
var fieldAPIName = field.fieldAPIName;
var options = []
field.pickListValues.forEach(optionValues => {
options.push({label: optionValues.label, value: optionValues.value})
});
var element = '<lightning-combobox ' +
'name="' + fieldAPIName + '" '
'label="' + label + '" ' +
'placeholder="Select answers" ' +
'options=' + options +
'></lightning-combobox>';
document.getElementById('question-box').appendChild(element);
}
questions.html
<template>
<lightning-card title="Screening Questions">
<template id="question-box" if:true={questions.data}>
</template>
</lightning-card>
</template>
template for:each
approach to deal with this, generating an appropriate array of JavaScript objects from which to obtain the necessary parameters for each child component in turn. Each object will need to include a means to select which type of child component to render. This is a reasonable place to start.