Support

Account

Home Forums Add-ons Repeater Field Add row to repeater field with js?

Solving

Add row to repeater field with js?

  • Hello, is there a way to add a row to the repeater field with javascript? I want to add a new row when user fills an input and presses a button.

  • 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 don’t know if this information will help you or anyone, but there it is. I can try to answer questions, but anything involving JavaScript for ACF fields is going to take a bit of trial and error.

    • city17

    • September 9, 2019 at 7:41 pm

    I know this is a bit of an old topic, but I’m having the same problem and your link no longer works @hube2 .

    Did you eventually figure out how to (easily?) add a repeater row in Javascript?

  • 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

    Steps:
    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.

    Sorry I can’t supply any code, but that project died and all of the code in it would need to be rewritten to use the new ACF JS API https://www.advancedcustomfields.com/resources/javascript-api/ for the getting and setting of values as well as having all the ajax requests rebuilt.

    • city17

    • September 12, 2019 at 12:36 am

    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.

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

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.