2

I am trying to replicate the below code in my lightning app component

var opts = [
        { "class": "optionClass", label: "Option1", value: "opt1", selected: "true" },
        { "class": "optionClass", label: "Option2", value: "opt2" },
        { "class": "optionClass", label: "Option3", value: "opt3" }

    ];
    cmp.find("InputSelectDynamic").set("v.options", opts);

All works well except the selected is not being set to specified row item.

My Code. Where isSelected is passing "true" for one of the value.

opts.push({class : "optionClass", label : labelData, value : innerRootLevel3, selected : isSelected});
                                            opts.join(",");
1
  • FYI lightning:select correctly selects the option that matches its value attribute.
    – Keith C
    Apr 24, 2017 at 17:58

1 Answer 1

1

I had also problems like this, eventually I have created the component dynamically like that (with 2 methods of setting the selected value because sometimes one does not work and sometimes the other):

on my markup, the drop-down is:

<lightning:select label="mypicklist" name="type" aura:id="mypicklist" />

On my javascript code I am filling the data like this:

var selectedValue = 'Option2';

var cmp = component.find('mypicklist');

// clear all options
cmp.set('v.body', []); 
var body = cmp.get('v.body');

// create the option list
var options = [
    { id: Option1, label: Option1 },
    { id: Option2, label: Option2 },
    { id: Option3, label: Option3 }
];


// add each option
options.forEach(function (opt) {
    var selected = 'false';
    if (opt.id == selectedValue)
        selected = 'true';
    $A.createComponent(
        'aura:html',
        {
            tag: 'option',
            HTMLAttributes: {
                value: opt.id,
                text: opt.label,
                selected: selected
            }
        },

        function (newOption) {
            //Add options to the body
            if (component.isValid()) {
                body.push(newOption);
                cmp.set('v.body', body);
            }
        })
});
/*TODO: this sometimes does not work*/
cmp.set('v.value', selectedValue);

I Hope you can use this to solve your problem

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .