function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
divya1234divya1234 

How to trim the string inside aura iteration in lightning component

I have a situation where i want to display the field value which should show sub string value of field inside aura iteration eg email field is test1.user1@gmail.com i want to display value as test1.user1. ho can i achieve this?
<th data-label="Id" scope="row">
          <div class="slds-truncate" title="Cloudhub">
        <a href="{!'/one/one.app?#/sObject/'+ singleRec.Id + '/view'}" target="_blank">{!singleRec.Email}</a>                                           </div>
                                        </th>

 
Best Answer chosen by divya1234
Danish HodaDanish Hoda
Hi Divya,
Please refer belwo code, where I have queried a list of contacts and have displayed their username by splitting the Email with '@':
 
Controller.js:

({
	doInit : function(component, event, helper) {
		var action = component.get("c.getContacts");   //fetching a list of Contacts
        action.setCallback(this, function(response){
            if(response.getState() == "SUCCESS"){
                var result= response.getReturnValue();
                var lst = [];
                component.set("v.contcts", response.getReturnValue());
                for(var i=0; i<result.length; i++){
                    let username = result[i].Email.split('@')[0];
                    lst.push({value:username, key:result[i]});
                }
                component.set("v.myMap", lst);
            }
        });
        $A.enqueueAction(action);
	}
})

CMP:

    <aura:attribute name="myMap" type="Map"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    
    <aura:iteration items="{!v.myMap}" var="mp" indexVar="key">
            <tr>
                <td title="Contact" style="width: 50%">
                    {!mp.key.Id}<br/><br/>
                </td>
                <td title="Email" style="width: 50%">
                    {!mp.value}
                </td>
            </tr>
        </aura:iteration>

 

All Answers

Danish HodaDanish Hoda
Hi Divya,
You can't perform operations here, you need to use an attribute to substring the value in your controller/helper.js and render it here.
Danish HodaDanish Hoda
Hi Divya,
Please refer belwo code, where I have queried a list of contacts and have displayed their username by splitting the Email with '@':
 
Controller.js:

({
	doInit : function(component, event, helper) {
		var action = component.get("c.getContacts");   //fetching a list of Contacts
        action.setCallback(this, function(response){
            if(response.getState() == "SUCCESS"){
                var result= response.getReturnValue();
                var lst = [];
                component.set("v.contcts", response.getReturnValue());
                for(var i=0; i<result.length; i++){
                    let username = result[i].Email.split('@')[0];
                    lst.push({value:username, key:result[i]});
                }
                component.set("v.myMap", lst);
            }
        });
        $A.enqueueAction(action);
	}
})

CMP:

    <aura:attribute name="myMap" type="Map"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    
    <aura:iteration items="{!v.myMap}" var="mp" indexVar="key">
            <tr>
                <td title="Contact" style="width: 50%">
                    {!mp.key.Id}<br/><br/>
                </td>
                <td title="Email" style="width: 50%">
                    {!mp.value}
                </td>
            </tr>
        </aura:iteration>

 
This was selected as the best answer