1

I'm building out a front end form that users will use to fill out with their information. Naturally, when a user first signs up, their profile fields are empty and a blank space appears for them to add in their info later.

This works well on all fields on my form except the table field type. If nothing has been entered the loop will simply display nothing. I've been reading through this answer, but so far checking is empty or |length is not working on my form.

Here's my current loop, which works great as long as the info is entered through the CP. How do I get an empty table field type to display on the front-end so a user can update their profile?

<tr>
    <td>Institution</td>
    {% for row in currentUser.providerCredentials %}
      <td><input type="text" id="accreditingInstitution" class="name-field" name="fields[providerCredentials][0][col1]" value="{{row.accreditingInstitution}}"></td>
    {% endfor %}
</tr>
  • When you say 'row fieldtype', do you mean the table fieldtype? – Brad Bell Jan 5 '15 at 19:44
  • Yep, I meant the table field type. I kept staring at 'row' in the code so in my mind I changed it to the 'row field type'. I updated the question. Thanks, Brad. – Matt Steele Jan 5 '15 at 19:47
1

As far as I know, I don't think that there is any built-in way to replicate the table fieldtype functionality on the front-end (i.e. with 'add row','remove row', etc.) I think that you would have to create your own buttons and then manipulate the dom using javascript.

In your template you would want something like this:

Provider Credentials
<table class="js-providerCredentials">
    {% for row in currentUser.providerCredentials %}
        <tr id="accreditingInstitution-{{ loop.index }}">
            <td>Institution</td>
            <td><input type="text" class="name-field" name="fields[providerCredentials][{{ loop.index }}][col1]" value="{{ row.accreditingInstitution }}"></td>
        </tr>
    {% endfor %}
</table>
<a class="button add-row" role="button">Add row</a>

And in your javascript file:

$('.add-row').on('click', function(event){
    $(this).preventDefault();

    var rowCount = $('.js-providerCredentials').children('tr').length,
        newRow = '<tr id="accreditingInstitution-' + (rowCount+1) + '"><td>Institution</td><td><input type="text" class="name-field" name="fields[providerCredentials][' + (rowCount+1) + '][col1]" value=""></td></tr>';

    $('.js-providerCredentials').append(newRow);
})

Not sure how your table is set up exactly, but the principle should be the same. For another example of how to format the dom for form submission see the answer to this question, editing a table field type from the front end.

  • Thanks for the answer, Douglas. I've got JS set up to add rows, but had trouble getting the initial empty fields in there. The loop.index may be the key to getting all working. – Matt Steele Jan 5 '15 at 22:44
  • Great! fyi.. I noticed another error: 'rowCount' should be 'rowCount+1' of course. I updated the answer. – Douglas McDonald Jan 5 '15 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.