3

I have dynamically created lightning:comboboxes that are instantiated in my JavaScript controller for my lightning component. In my callback function after the component is created, I want to set the value of that combobox if a value exists for it (user is editing instead of creating).

I tried something like this:

$A.createComponent(
            'lightning:combobox',
            {
                'aura:id': 'objectSelect',
                'id': index,
                'placeholder': 'Select Object',
                'label': label,
                'value': 'selectedObject' + index,
                'options': options,
                'onchange': component.getReference('c.handleObjectSelect')
            },
            function(newCombobox, status, error){
                if(status === 'SUCCESS'){
                    var cmpBody = component.get('v.comboboxes');
                    cmpBody.push(newCombobox);
                    component.set('v.comboboxes', cmpBody);

                    var thisCriteria = component.get('v.currentCriteria');
                    if(thisCriteria != null){
                        newCombobox.set('v.value', thisCriteria.objectName);
                    }
                }
                else if(status === 'INCOMPLETE'){
                    console.log('No response from server');
                }
                else if(status === 'ERROR'){
                    console.log(error);
                }
            }
        );

But doing set('v.value') does not seem to work. I know you can do .get('v.value') to get the selected value, I had hoped you could also set it from there.

I also know you can specify 'value' as an attribute for the combobox and bind it to an aura attribute, but I am dynamically creating these, I don't want to hardcode aura attributes in case the combobox is there for it, I would like to be able to just set the value directly instead of through an aura attribute.

Is such a thing possible?

0

1 Answer 1

0

Couldn't you evaluate this just before creating the component? Just check for your object name, and then instantiate. Like so:

var thisCriteria = component.get('v.currentCriteria');
var comboboxValue = thisCriteria == null ? 'selectedObject' + index : thisCriteria.objectName

$A.createComponent(
        'lightning:combobox',
        {
            'aura:id': 'objectSelect',
            'id': index,
            'placeholder': 'Select Object',
            'label': label,
            'value': comboboxValue,
            'options': options,
            'onchange': component.getReference('c.handleObjectSelect')
        },
5
  • Oh, I suppose that makes sense. I was thinking that value had to be bound to an attribute, but that makes sense that you could just state the value directly. I'll give it a try. Commented Sep 11, 2018 at 21:17
  • Hmm, this does not seem to be working for me. In my console, I am seeing that the label, value object I am passing to value in my dynamic component is indeed exactly like a label, value object I am passing in my options, but it is not actually selecting it. Commented Sep 11, 2018 at 22:10
  • You want to be passing just a value - it doesn't want an object, just a scalar string or number (as long as it's one of the options in your options array). I'm not sure what is in thisCriteria.objectName but it can't be an object. Commented Sep 11, 2018 at 22:25
  • In fact, if you do that, your original code will likely work too. Commented Sep 11, 2018 at 22:25
  • Ahh, of course, haha. I'll give that a try Commented Sep 11, 2018 at 22:30

You must log in to answer this question.

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