Well, it’s been a month on this topic, but I have recently been working on a project where I wanted to do this so I thought I would share my solution.
Take a look at this file I uploaded to GitHub https://github.com/Hube2/blunt-parametric-search/blob/master/admin/js/admin.js
Look at the function _para_meta_key_change.
This function gets a meta_key value from one of two possible fields. Depending on other values it then removes the current rows from a repeater field, then it does an AJAX request and re-populates the repeater with the values that in the AJAX response.
I can’t really explain the whole thing, it took me several hours of poking and prodding to figure it out.
The important bits:
line 128: triggering the click action for the add row link
lines 130 to 135: getting the
data-id, which is a unique ID for a cloned field that ACF adds, of each row that is added, and then setting the values of the fields in that row.
The setting of the values will be highly dependent on what type of field your setting, in my case these are simple text fields.
Also since your adding one field and then setting the values you won’t need a loop, you’ll just need to look at the last row, the one that was just added.
I still have that code, but none of it will work any more because of the new ACF JS API.
Basically you need to add an event to something, when that happens I just trigger the add row link for the repeater. In my case it’s something like
$('[data-key="field_56f3e299b6407"] ul.acf-actions a[data-event="add-row"]').trigger('click');
field_56f3e299b6407 is the repeater key.
You can trigger the removal of a row using
$('[data-key="field_56f3e299b6407"] tr a[data-event="remove-row"]').not('[data-key="field_56f3e299b6407"] tr.acf-clone a[data-event="remove-row"]').trigger('click');
The actual selectors may be different depending on your repeater layout.
If you’re going to remove and add multiple rows to the repeater you need to be careful of the order that you do things because the rows you want to remove may still be there when you start adding data. There is a delay in the removal
1) Remove repeater rows
2) Add enough repeater rows to hold the data you want to add
3) Add the data to sub fields starting at the end and moving up/backwards. This is the important part because the fields you removed will still be there until your script ends and the DOM refreshes.
Thanks very much, John. I already figured out how to add a repeater row, but I wasn’t sure how to deal with removing (because of the confirmation popup). Your code is very helpful.
I won’t be adding and removing at the same time, they’re separate actions. But it’s good to know there’s a workaround.
You must be logged in to reply to this topic.
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!
© 2020 Advanced Custom Fields. Subscribe