Skip to main content
Hi,

how to get the value from <a> tag list to controllerI want an action that, when i will click ine of those link then my input box with that name.

im giving my codes..

Component:

<aura:attribute name="String" type="String[]"/>

<aura:handler name="init" value="{!this}" action="{!c.init}" />

<lightning:card title="Filtered Values">

<div class="search-field">

<lightning:input aura:id="nameFilter" label="Filter names" onchange="{!c.handleNameFilterChange}" />

</div>

<!--

<div class="results">

<aura:if isTrue="{!v.contacts.length > 0}">

<p class="has-results">

Showing {!v.contacts.length} contact(s):

</p>

<ol class="slds-list_dotted">

-->

<ol>

<aura:iteration items="{!v.String}" var="s">

<li>

<a id="getId" onclick="{!c.handleClick}">

{!s}

</a>

</li>

</aura:iteration>

</ol></lightning:card></aura:component>

controller

({

init: function(component, event, helper) {

helper.loadList(component);

},

handleNameFilterChange: function (component, event, helper) {

helper.loadList(component);

},

})

helper

 

({

loadList: function(component) {

var action = component.get("c.allContacts");

var nameFilterString = component.find("nameFilter").get("v.value");

action.setParams({

nameFilterString: nameFilterString

});

action.setCallback(this, function(a) {

component.set("v.String", a.getReturnValue());

});

$A.enqueueAction(action);

}

})

thanks,

​​​​​​​
답변 4개
  1. 2019년 11월 5일 오전 8:03
    Hi Agnibha,

    First of all, you have to remove id attribute from anchor tag, as we don not user static id in the iteration.

    You will also need to add a custom attribute to get the value of the selected anchor tag.

     

    <aura:component implements="flexipage:availableForAllPageTypes" access="global" >

    <aura:attribute name="String" type="String[]"/>

    <aura:handler name="init" value="{!this}" action="{!c.init}" />

    <lightning:card title="Filtered Values">

    <div class="search-field">

    <lightning:input aura:id="nameFilter" label="Filter names" onchange="{!c.handleNameFilterChange}" />

    </div>

    <!--

    <div class="results">

    <aura:if isTrue="{!v.contacts.length > 0}">

    <p class="has-results">

    Showing {!v.contacts.length} contact(s):

    </p>

    <ol class="slds-list_dotted">

    -->

    <ol>

    <aura:iteration items="{!v.String}" var="s">

    <li>

    <a onclick="{!c.handleClick}">

    {!s}

    </a>

    </li>

    </aura:iteration>

    </ol>

    </lightning:card>

    </aura:component>

    You have to define an method called handleClick for capturing anchor tag value and assign into the input box.

    ({

    init: function(component, event, helper) {

    helper.loadList(component);

    },

    handleNameFilterChange: function (component, event, helper) {

    helper.loadList(component);

    },

    handleClick: function (component, event, helper) {

    alert(event.currentTarget.dataset.item);

    component.find("nameFilter").set("v.value",event.currentTarget.dataset.item)

    },

    })

    If you found it useful please appreciate my efforts and mark it as the best answer

    Thanks,

    Soyab