1

I have used Check all / Uncheck All checkboxes function to select group of Values. I used to display some data in aura iteration with checkbox values checked.

If the data row is more than one its working fine suppose if the row count is one im facing error:

"Uncaught Error in $A.getCallback() [Cannot read property 'set' of undefined]"

I used <ui:inputCheckbox>

Code:

component.find("checkBox")[i].set("v.value", true);    

var recordfinallist = component.get('v.attributename');                   
        for (var i = 0; i < recordfinallist.length; i++) {                       
            if (recordfinallist [i].isvalid__C== true){                                                      
                component.find("checkBox")[i].set("v.value", true);                               
            }                                  
         }                                     
     }

*isvalid__C is field name

This code is working proper for more than 1 rows but not for one row.

2 Answers 2

5

component.find() returns one of three types of value:

  • If the local ID is unique, find() returns the component.
  • If there are multiple components with the same local ID, find() returns an array of the components.
  • If there is no matching local ID, find() returns undefined.

Your code is assuming that the result of find() is an array, which is why it fails when only a single checkbox is present. You need to be prepared to handle a single return value as well.

3
  • Yes of course im getting "undefined" for this line----> component.find("checkBox")[i]. How to handle it like a single return value as well? Can you please in details with examples.
    – Issac Pal
    Commented Aug 27, 2018 at 16:12
  • how would you check for a single value ? your original question has been answered, if you are unsure how to check for a single value or are facing issues while implementing code for this specific behavior, you might want to open a new post.
    – glls
    Commented Aug 27, 2018 at 16:41
  • 2
    I'd suggest you accept Kal's answer, which provides a good model to follow.
    – David Reed
    Commented Aug 27, 2018 at 17:07
4

component.find() returns an array if there are more than one matches. Otherwise, it just returns that one element (and NOT an array). To avoid this situation, I almost always pass the result of my component.find() through a custom helper function that ALWAYS returns an array. Like so:

arrayfy: function(component) {
    //If input is an array, then just return it
    //If only one element found, then convert it into a single element array anyway
    if(Array.isArray(component)) 
        return component;
    else 
        return component? [component] : [];
},

Use it like so:

var recordfinallist = component.get('v.attributename');
for (var i = 0; i < recordfinallist.length; i++) {
    if(recordfinallist[i].isvalid__C == true)
        helper.arrayfy(component.find("checkBox"))[i].set("v.value", true);
}
0

You must log in to answer this question.

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