0

In an aura:iteration, I pass the selected Order to the JS controller as in the code:

    <aura:iteration items="{!v.ordersAlt}" var="item">
    <tr class="slds-hint-parent">
        <td data-label="Selected">
            <div class="slds-truncate" title="Selected"><input type="checkbox" checked="true" name="options" id="checkbox-01" value="checkbox-01" tabindex="0" aria-labelledby="check-button-label-01 column-group-header" onchange="{! c.selectOrder }" data-order ="{!item}"/></div>
        </td>
        <th data-label="Order Id" scope="row">
            <div class="slds-truncate" title="Order Id">
                <a href="javascript:void(0);" tabindex="-1">{!item.Id}</a>
            </div>
        </th>
    </tr>
   </aura:iteration>

For {!item.Id} I get a result, so the iteration works fine. The function in the controller looks like this:

selectOrder : function(component, event, helper) {

        var order = event.currentTarget.dataset.order;
        var selectedOrders = component.get("v.selectedOrders");

        var listOrderIds = [];
        for (var i = 0; i < selectedOrders.length; i += 1){
            listOrderIds.push(selectedOrders[i].Id);
            console.log('selectedOrders[i].Id '+selectedOrders[i].Id);
        }

        var index = listOrderIds.indexOf(order.Id);
        console.log('order.Id '+order.Id)

        console.log("index "+index); 
        //console.log("selectedOrders before "+selectedOrders); 

    }

Everything works fine. When I pass the parameter via data-order ="{!item.Id}" that also works. Just if I use data-order ="{!item}" I get the result of undefined for order.Id. How can I modify so that the code works?

2

You can pass ONLY strings as attribute values and so if you are passing the whole object as attribute value, it will be stored as [object Object] string and [object Object].Id does not make any sense and so it will return undefined.

You should pass the Id by using data-order ="{!item.Id}" and get the record through Id as below:

let orderId = event.currentTarget.dataset.order;
var ordersAlt = component.get("v.ordersAlt");  // list of records which are used in iteration
let clickedRecord = ordersAlt.find(item => item.Id===orderId);
console.log(clickedRecord.Id); // will give you correct Id

Here is the playground link if it helps (in LWC)

| improve this answer | |
  • Ok, thanks, so if I need the entire object I need to get it from an Apex controller? – user40501 Nov 14 '19 at 8:59
  • You already have the list ordersAlt. So you can get it by using find javascript function as shown. And yes if it suits your requirment you can pass the order Id to apex controller and query for the record. – salesforce-sas Nov 14 '19 at 9:09
  • ordersAlt may not contain the order, so I need the information from somewhere else. Thanks for the response. – user40501 Nov 14 '19 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.