Support

Account

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

Solved

Rows count based on user's input (another field)

  • Hi there.
    Is there a way to bind the rows count in a repeater field to another field like the image below?
    Now the rows count should be set on the min and max values, nut I want them to be dynamic and based on the user’s input.

  • There might be a way to do this by adding custom javascript, but I don’t have any example code I can share.

    This seems like a lot of work to me since the same person who is setting the number of rows is also entering information into those rows. Can’t they just create the number of rows that they want? I don’t see the purpose of the row count field.

  • Thank you @hube2

    The purpose of this feature is to have a control on the input data. The row count I mentioned earlier is actually the count of company board members. Because there is a lot of companies have to be imported in this system, we have to make sure the data of all of them are imported, not just some of them.

    I will make an update of my solution here for others.

  • @hube2 I found this tread:
    https://support.advancedcustomfields.com/forums/topic/fixing-number-of-repeater-rows-with-dropdown/

    1. I’m doing the same, but as the post author said, setting data-min and data-max does not do anything and I have to disable the “Add row” button myself. Can you help me?

    2. How can I get the row numbers using jQuery? It could be a great help.

  • 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.

  • @hube2 the solution worked, thanks alot!

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.