Show checkbox while loading component

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












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.







share|improve this question




























    up vote
    1
    down vote

    favorite












    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.







    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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.







      share|improve this question














      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.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 27 at 16:47









      battery.cord

      6,26951742




      6,26951742










      asked Aug 27 at 16:01









      Issac Pal

      13




      13




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote













          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.






          share|improve this answer




















          • 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
            Aug 27 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
            Aug 27 at 16:41






          • 2




            I'd suggest you accept Kal's answer, which provides a good model to follow.
            – David Reed
            Aug 27 at 17:07

















          up vote
          4
          down vote













          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);






          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f230244%2fshow-checkbox-while-loading-component%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            5
            down vote













            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.






            share|improve this answer




















            • 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
              Aug 27 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
              Aug 27 at 16:41






            • 2




              I'd suggest you accept Kal's answer, which provides a good model to follow.
              – David Reed
              Aug 27 at 17:07














            up vote
            5
            down vote













            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.






            share|improve this answer




















            • 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
              Aug 27 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
              Aug 27 at 16:41






            • 2




              I'd suggest you accept Kal's answer, which provides a good model to follow.
              – David Reed
              Aug 27 at 17:07












            up vote
            5
            down vote










            up vote
            5
            down vote









            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 27 at 16:07









            David Reed

            19.3k21540




            19.3k21540











            • 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
              Aug 27 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
              Aug 27 at 16:41






            • 2




              I'd suggest you accept Kal's answer, which provides a good model to follow.
              – David Reed
              Aug 27 at 17:07
















            • 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
              Aug 27 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
              Aug 27 at 16:41






            • 2




              I'd suggest you accept Kal's answer, which provides a good model to follow.
              – David Reed
              Aug 27 at 17:07















            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
            Aug 27 at 16:12




            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
            Aug 27 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
            Aug 27 at 16:41




            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
            Aug 27 at 16:41




            2




            2




            I'd suggest you accept Kal's answer, which provides a good model to follow.
            – David Reed
            Aug 27 at 17:07




            I'd suggest you accept Kal's answer, which provides a good model to follow.
            – David Reed
            Aug 27 at 17:07












            up vote
            4
            down vote













            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);






            share|improve this answer


























              up vote
              4
              down vote













              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);






              share|improve this answer
























                up vote
                4
                down vote










                up vote
                4
                down vote









                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);






                share|improve this answer














                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);







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 27 at 16:29

























                answered Aug 27 at 16:05









                Kal

                1,5641032




                1,5641032



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f230244%2fshow-checkbox-while-loading-component%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    這個網誌中的熱門文章

                    How to combine Bézier curves to a surface?

                    Mutual Information Always Non-negative

                    Why am i infinitely getting the same tweet with the Twitter Search API?