0

I want to assign current selected option which is '0' and I want to assign it to '2' when URL is change. How can I do that? Please help.

Example.cmp

<div class="slds-col slds-size_1-of-5 slds-grid--vertical-align-start">
                <ui:inputSelect aura:id="mySelect" change="{!c.onChangeFunction}">
                    <aura:iteration items="{!v.options}" var="item" >
                        <ui:inputSelectOption text="{!item.label}" value="{!item.value}"/>
                    </aura:iteration>
                </ui:inputSelect>
                </div>

Example.js

component.set("v.options", [
            {'label': 'Default', 'value': 0, selected: "true"},
            {'label':'Yes','value':1},
            {'label':'No','value':2},]);

Example.helper

handleEvent :function(component,event,helper){  
        var sPageURL = decodeURIComponent(window.location); //get params from URL
        console.log('sPageURL' + sPageURL);
        const queryString = window.location.search;
        var option = component.find("mySelect").get("v.value"); //show undefined
},

1 Answer 1

0

I'm not sure if this is what you're looking for, but here's a demonstration of this technique:

<aura:application >
    <aura:handler name="init" action="{!c.init}" value="{!this}" />
    <aura:attribute name="options" type="List" default="[{'label':'Value 1','value':'1'},{'label':'Value 2','value':'2'}]" />
    <select aura:id="selection">
        <aura:iteration items="{!v.options}" var="option">
            <option value="{!option.value}">{!option.label}</option>
        </aura:iteration>
    </select>
    <div>Choose Value</div><aura:iteration items="{!v.options}" var="option">
    <div><a href="{!'#'+option.value}">{!option.label}</a></div>
    </aura:iteration>
</aura:application>

...

({
    init: function(component, event, helper) {
        window.addEventListener(
            'hashchange',
            $A.getCallback(function () {
                let sel = component.find("selection").getElement();
                let target = window.location.hash.substr(1);
                let index = [...sel.options].findIndex(option => option.value === target);
                sel.selectedIndex = index;
            })
        );
    }
})

...

We use the onhashchange event to detect URL changes, and then perform an update to the value based on that information.

Please that ui:inputSelect and ui:inputSelectOption (as well as all other "ui" elements) are deprecated, will stop working in the near future, and should not be used in any new code.

You must log in to answer this question.

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