You need to sign in to do that
Don't have an account?
Generate Dynamic InputSelect components (and inputSelectOptions) from fieldSets
The component containing all of this looks ike this:
<aura:component controller="ltngController"> <aura:attribute name="userId" type="String" /><!--used to find the Contact--> <aura:attribute name="contact" type="Contact"/> <aura:attribute name="fieldSetName" type="String"/> <aura:attribute name="fields" type="Object[]"/><!--custom FieldSetMember class--> <aura:attribute name="picklistValues" type="Object[]" /><!--custom picklistOptions class--> <aura:attribute name="form" type="Aura.Component[]"/><!--dynamic component where generated components will be placed--> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <!--HTML and other Lightning components--> <div class="row"> <div class="section"> <div class="cell form"> {!v.form} </div> </div> </div> </aura:componentThe JSController runs the doInit which calls the server and gets the fieldSet data. This data is moved into a custom FieldSetMember class whose properties are AuraEnabled. For picklists, a describe call gets the Picklist value for this field and stores them in a list of custom PicklistValues objects. All this and the Contact record is bundled into a wrapper object and sent back to the JSController callback method.
Checking the Debug Log and the Chrome Inspector verifies that all the data is available in the component controller, so that's not the problem.
Next step is to dynamcially create the components to hold the fieldSetdata in a helper method called 'createForm'. When a picklist value is encountered, I run the following
createForm : function(cmp) { var fields = cmp.get('v.fields'); var obj = cmp.get('v.contact'); for (var i = 0; i < fields.length; i++) { var field = fields[i]; if (field.type == 'PICKLIST'){ var picklistValues = cmp.get("v.picklistValues");//all values for all picklist fields $A.createComponent("ui:inputSelect", {"label":field.label, "aura:id":field.label, "value":obj[field.fieldName], "required":field.required}, function(newSelect){ if (newSelect.isValid()){ // loop through the values to create options for (var i = 0; i < picklistValues.length; i++){ var pItem = picklistValues[i]; if (pItem.fieldName == field.fieldPath){ //only create for values that //match the field's options $A.createComponent("ui:inputSelectOption", {"label":pItem.ItemLabel, "text":pItem.ItemValue}, function(newOption){ if (obj[field.fieldName] == pItem.ItemValue) newOption.set("v.value":true); // get the inputSelect component's options attribute var options = newSelect.get("v.options"); options.push(newOption); newSelect.set("v.options",options); }); } } } // after all picklist options are created and pushed into the v.options // attribute of the created inputSelect component, it all gets pushed into // the main component's v.form. var form = cmp.get("v.form"); form.push(newSelect); cmp.set("v.form",form); }); } } }
OK, so after checking with the debugger; statement, all of those components appear to be created correctly. However, when I run the process, the picklist fields appear, with the correct label, but the drop-down for each reveals no selectOptions, even though there is enough empty space in the dropdown to match the number of options that should be visible. Here is a screen show of the rendered page:
Agai, looking at the inputSelectOptions using the debugger statement in the Chrome DevTools Inspector showed that even as they were being inserted into the inputSelect component's options list, they contained the expected data, and the inputSelect components are showing in the form tag alongside the other field types.
But WHY do the values not appear????? Any help is appreciated
Here is the entire createForm method with notes as to what is happening: Now looking at the HTML in the page:
Here is the HTML for the Last Name inputText component which is rendering
And here is the HTML for the Gender inputSelect component. The options are coming up as undefined!! OK, so as I showed in the Javascript, I am able to 'get' this select component and its options from the form component and see their values AFTER they have been loaded into the form component. So, they exist, but to the browser they come up as undefined.