0

I'm looking to have a dynamic input that can be either a Dynamic PickList OR A Dynamic Input such as text or DateTime.

$A.createComponent(String type, Object attributes, function callback)

I have been using the above to great effect with lightning input. However, when I try to do the same with lightning select, all of the guidance says that I can't have a dynamic amount of options as it seems each option has to be hardcoded like below!

$A.createComponent(
    "lightning:select", [
        [
            "aura:id": "input",
            "label": cmp.get("v.label"),
            "onchange": cmp.getReference("c.onChange")
        ],
        [
            "option", {
                value: "Option 1",
                label: "Option 1"
            }
        ],
        [
            "option", {
                value: "Option 2",
                label: "Option 2"
            }
        ]
    ],
    function(icmp, status, errorMessage) {
        if (status === "SUCCESS") {
            var body = cmp.get("v.body");
            body.push(icmp);
            cmp.set("v.body", body);
        } else if (status === "INCOMPLETE") {
            console.log("No response from server or client is offline.")
        } else if (status === "ERROR") {
            console.log("Error: " + errorMessage);
        }
    }
);

Now I know I can set the value/label of each dynamically, but I cannot seem to have a dynamic number of options matching the length of a list, for example.

0
1

from what I understood you want to bind options to variable, you can do this by using component.setReference. From what I remember it'll work like this icmp.setReference('v.options', cmp.getReference('v.myDynamicOptions'))

8
  • This will work for lightning select? Normally you have to do something like the below if setting static <lightning:select name="select1" label="How many tickets?" required="true"> <option value="">choose one...</option> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </lightning:select> – KarlLivesey Mar 30 at 14:46
  • What format would myDynamicOptions need to be in? – KarlLivesey Mar 30 at 15:08
  • the same way you specified in your parameters array of {label, value} – ytiq Mar 30 at 18:05
  • ok. mb I forgot that you can't just use attribute for it. Then you need to create a custom component with options attribute which will create this child options for you in <aura:iteration> – ytiq Mar 30 at 18:08
  • I can't do that though as it could be a lightning:select or a lightning:input radiobutton so I cannot hard code it like that – KarlLivesey Apr 1 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.