Support

Account

Home Forums Add-ons Repeater Field Repeater field – different default values sets Reply To: Repeater field – different default values sets

  • This could be done in PHP, but it would mean that you’d have to select the option, save the post and then when the page reloads you can populate it with the values you want based on the setting in the other field.

    Continuing the JS example, if the repeater is only found on the page once, you can get the rows in the current repeater like this.

    
    // find repeater rows, replace field_XYZ with repeater field key
    var rows = $('[data-key="field_XYZ"] .acf-row');
    
    // add rows
    if (rows.length < YOUR NUMBER OF ROWS) {
      for (i=rows.length; i<YOUR NUMBER OF ROWS; i++) {
        $('[data-key="field_XYZ"] .acf-actions a.add-row').trigger('click');
      }
    }
    
    // remove rows
    if (rows.length > YOUR NUMBER OF ROWS) {
      // remove rows from the end
      for (i=rows.length-1; i>=YOUR NUMBER OF ROWS; i--) {
        rows[i].find('a.remove-row').trigger('click');
      }
    }
    
    // populate rows
    // counter holds current row
    var counter = 0;
    // values holds value for each row
    var values = ['Production', 'Materials', 'Cord', 'Dimensions (mm)', 'test (mm)']
    rows.each(function(index, element) {
      // just in case
      if (counter+1 > values.length) {
        // no value for this row
        return;
      }
      // assuming a text field
      // populate this row from values
      // key of text field
      $(element).find('[data-key="field_ZABC"] .acf-input input).val(values[counter]);
      counter++;
    });