Support

Account

Home Forums Add-ons Repeater Field Rows count based on user's input (another field) Reply To: Rows count based on user's input (another field)

  • I did some testing and I was unsuccessful at setting min/max on the repeater field based on another field. The min/max gets set but it seems that setting these after the field is loaded has no effect on the field. I have not had time to look at it further.

    This is my code for this so far

    
    add_action('acf/admin_head', 'test_add_script');
    function test_add_script() {
      ?>
        <script type="text/javascript">
          (function($){
            if (typeof(acf) == 'undefined') {
              return;
            }
            acf.addAction('ready', function(e){
              // on ready get number field
              // key of number field
              $('[data-key="field_607c5c906fe34"] .acf-input input').each(function(index, element) {
                var value = $(element).val();
                adjust_repeater(value);
              });
            });
            // on change of number field get new value
            // key of number field
            $(document).on('change', '[data-key="field_607c5c906fe34"] .acf-input input', function(e) {
              var field = acf.getField('field_607c5c906fe34');
              var value = field.val();
              adjust_repeater(value);
            });
            function adjust_repeater(value) {
              // get the repeater field and set min max
              // key of repeater field
              var field = acf.getField('field_607c5cb56fe35');
              field.data.min = value;
              field.data.max = value;
            }
          })(jQuery);
        </script>
      <?php 
    }
    

    Getting the row numbers would be problematic. Row numbers are generated differently depending on if they exist when a page is loaded or they are added. Already existing rows are like “row-0”, “row-1”, etc while new row values are generated using a uniqid function and look more like “6080311efeabf”. It is easier to get the count of rows by finding all all of a subfield in the rows, excluding the clone row that acf used to add additional rows.

    
    // field keys for repeater and sub field
    $(document).find('[data-key="field_XXXXXXX"] [data-key="field_YYYYYYY"]').not('[data-key="field_XXXXXXX"] .acf-clone [data-key="field_YYYYYYY"]').each(function(index, element) {
      // then you can work on each row by getting the .acf-row each sub field is in
      var row = $(element).closest('.acf-row');
    }
    

    Hopefully this helps you. Although I don’t know how doing this in the admin using JS is going to help you with a data import.