Home › Forums › Add-ons › Repeater Field › Dynamically populate repeater select subfields per row › Reply To: Dynamically populate repeater select subfields per row
This is not possible without using JS/AJAX. As you have found, acf/prepeare_field only runs once for sub fields of a repeater. I have actually looked into doing this with repeater sub fields and found it impossible.
The main issue that this will cause is that after a post is saved, since the field actually has no choices any value that the field has will be removed because it cannot be selected. The only thing that you can do to prevent this is to make sure that any already selected values exist as a choice.
add_filter('acf/prepare_field/name={DYNAMIC SELECT FIELD NAME}', 'populate_selected');
function populate_selected($field) {
// I don't recall how to get the post ID at the moment
// you may be able to use global $post
// or you may need to look at $_GET value
$post_id = ??
$choices = array();
if (have_rows('repeater', $post_id)) {
while (have_rows('repeater', $post_id)) {
the_row();
$choices[get_sub_field('select field')] = get_sub_field('select field');
}
}
$field['choices'] = $choices;
return $field;
}
In your JS you can get the selected value before populating the field and then after your AJAX and the field is populated you can then reset the selected value.
Not sure if any of this will help you.
Let me know if you have questions about targeting each row in your JS to populate the model field based on the manufacturer of that row but basically it looks something like
var other_field = $(field).closest('.acf-row').find('[data-key-"field_XXXXX"] select');
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.