1
0
-1

I have a repeating block that I need to pre set the number of repeats based on a dropdown field selection, my attempt to place the following code replicated from one of my composer forms doesn't work. Is there a way to do this is maestro?

 

var num = (data.howManyAdditionalProperties == "3 or more" ? 3 : Number(data.howManyAdditionalProperties ));

data.rptproperty.setInstanceCount(num);                                      

 

var repeat = Form.getItemFromPath(Form.getItemProperty("data.addFormgetitempropertydatacontentitemlabel3","repeatLink"));

repeat.$addItem(repeat.properties.transferFocus);

    CommentAdd your comment...

    4 answers

    1.  
      1
      0
      -1

      Ryan Mazi ThomasYou won't necessarily need the form.setInstanceCount call

      However, to ensure that the object is available, we use a calculate rule after the repeat object in the hierarchy that just does this using a node array:

      //Provide a value or calculation for this data field
      if (!Util.isBlank(data.documents)){
      var nodeArr = jQuery.parseJSON(data.documents);
      Util.addRepeatContent(Form.getItemFromPath("data.attachments"), nodeArr);
      }
      return "";
       


      No need to use setinstancecount

       

        CommentAdd your comment...
      1.  
        1
        0
        -1

        As of Maestro 5.0.18, you can set the instance count of a repeat with the following method.

        Form.setInstanceCount("data.davidRepeat", data.davidRepeat, 5);

        The first parameter can be the item or the "data path string" of the item. The second is the data path of the repeat (which will be an array) and the last is the desired count you want the repeat to be modified to. 

        See example:

         

        Additionally if you want to populate this repeat with some data you can do it with the Util.addRepeatContent method (This has been tested on a page load rule):

        var repeatData = [{"name":"Doctor Strange"},{"name":"Antman"},{"name":"Ironman"},{"name":"Hulk"},{"name":"Captain America"}];
         
        // Set count
        Form.setInstanceCount(Form.items.davidRepeat, data.davidRepeat, 5);
         
        // Add content
        Util.addRepeatContent(Form.items.davidRepeat, repeatData);

        Result:

         

         

        1. Blake Hunter

          Hi David,

          I tried the approach above but whenever I click the "Util.addRepeatContent" button I get the following error: 

          Error: Parameter repeatItem must be a repeat
          at e.addRepeatContent (<anonymous>:2807:51413)
          at Rules.click_addRepeatContent (<anonymous>:1648:6)
          at fn (eval at compile (:794:126), <anonymous>:4:690)
          at b (<anonymous>:685:128)
          at e (<anonymous>:839:195)
          at m.$eval (<anonymous>:704:293)
          at m.$apply (<anonymous>:705:17)
          at HTMLButtonElement.<anonymous> (<anonymous>:839:247)
          at HTMLButtonElement.dispatch (<anonymous>:5:6466)
          at HTMLButtonElement.r.handle (<anonymous>:5:3241)

          I double checked and my "Form.items.davidRepeat" is of type "repeat". I noticed that the "Form.items.davidRepeat" has a property "repeatingData: true". It seems that the function "addRepeatContent()" has the following line causing the error: 

          if (e.repeatingData)
                          throw new Error("Parameter repeatItem must be a repeat");

          I'm not sure why this is the case however. Is there something I am doing wrong or should be doing differently?

          Thanks,

          Blake

        CommentAdd your comment...
      2.  
        1
        0
        -1

        Hi Ben, 

        I am doing something similar and have this working. Here is my code:

        // Define data to add to new repeating block rows
        var repeatData = [{"name":"Doctor Strange"},{"name":"Antman"},{"name":"Ironman"},{"name":"Hulk"},{"name":"Captain America"}];
        
        // Get repeating block and add rows
        var repeatItem = Form.getItemFromPath('Form.data.content1');
        repeatItem.setInstanceCount(repeatData.length);
        
        // Add data to rows
        Util.addRepeatContent(repeatItem, repeatData);

        This works fine when the code is triggered via a button click rule, however I need this to be triggered on page load instead. When I try this I get the following error in the console:

        TypeError: repeatItem.setInstanceCount is not a function
            at Rules.pageload_page1 (<anonymous>:1000:12)
            at k (<anonymous>:2156:8285)
            at <anonymous>:2156:8843
            at <anonymous>:689:20
            at m.$eval (<anonymous>:703:347)
            at m.$digest (<anonymous>:700:420)
            at m.$apply (<anonymous>:704:113)
            at <anonymous>:717:68
            at e (<anonymous>:603:444)
            at <anonymous>:606:300

        Please can you advise?

        Cheers,
        Ryan

          CommentAdd your comment...
        1.  
          1
          0
          -1

          Hey Jordan, you kind of had the right functions but you are using them in the wrong way.

          Form.getItemFromPath expects a data path as a string (i.e. what you get when you double click a component to add its data path to the script). You will need to call the setInstanceCount function on the result of that call.

          Assuming the field Id of your repeat component was 'content2' and the number of repeats you want to set is contained in a field called repeatCount, your script should be as follows:

          Form.getItemFromPath("data.content2").setInstanceCount(data.repeatCount)

          I also would recommend you take a look at this article:

          1. Ben Warner

            Did you get this working Jordan?

          CommentAdd your comment...