Questions tagged [aura-iteration]

462    Asked by anurhea in Salesforce , Asked on Apr 24, 2021

How to get the selected value by dynamic id in Aura Component during aura: iteration? I'm in trouble getting the selected picklist value.using the below code, multiple select boxes are displayed by iteration. each selection has a dynamic id. now by using the dynamic id. I want to get the selected value of each select box. As per my code, it returns undefined.

hope someone helps me.

cmp File

js file
 
doSave1: function(component, event, helper) {        
    var inputfield = component.find("textfield").get("v.value");
    var indexvar = document.getElementsByClassName("icon_image_canvas").length;
    var imgURL = [];
    
    for(var i=0; i < indexvar>        var tempCanvas =  document.getElementById( "targetIcon_" + i );
        var statusName = component.find("statusName_" + i).get("v.value");
        //var statusName = document.getElementsByClassName( "statusName_" + i )[0] ;
        alert(statusName);
        console.log("test: "+ statusName);
        
        var _imgURL = tempCanvas.toDataURL("image/png");
        imgURL.push(_imgURL.split(',', 2)[1]);
    }
    
        var action = component.get("c.createTorikago");        
        action.setParams({torikagoObj : component.get('v.torikagoSetupObj')});
        //action.setParams({torikagoObj : component.get('v.torikagoSetupObj'), imgURL : imgURL});
        
        action.setCallback(this,function(response){
            helper.doResponseProcess(component, response, 5);  
        });
        $A.enqueueAction(action);
    
},

Thanks in advance.

Answered by arti Trivedi

If you are developing something in the lightning component then you must have face the situation to store the selected value in your picklist.Sometimes it works directly by setting the value in your lightning select. But if you are using a dynamic list and Key-Value pair with an iteration to set the values, in that case setting value directly will not work. We have to do some extra efforts with setting the value from the controller.Please check the example below : In this example, I am having a picklist of months and I want to change the selected value dynamically.

Component

       
     
         {!item.value}
      

I have created a picklist in my component, in value I am having an attribute to store a selected month.

I have a list of months in the picklist values attribute. And in option, I am setting the key and values.

For example suppose in value I am having Jan, Feb, March and in key, I have 1,2,3.

To set the selected value in the selected month attribute we have to set the value in it from the controller and after that, we just have to add an extra line in the iterations option, Please check the below example.

Component after setting the value

    
        {!item.value}
   

As you can see in the above code we have added a new property in the option and we have to check it is matching the current item's key. It will set the value dynamically in your picklist.



Your Answer

Answer (1)

I got the point of the story, it's great that you gave me a lot of useful wordle 2 knowledge.

1 Month

Interviews

Parent Categories