Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Count the number of rows in a Gravity Forms list field and store the result in another field.
<script>
function ListFieldRowCount( listField, totalField ) {
var totalRows = jQuery( listField ).find('table.gfield_list tbody tr').length;
jQuery( totalField ).val( totalRows ).change();
}
function ListFieldRowTotal( formId, fieldId, totalFieldId ) {
var listField = '#field_' + formId + '_' + fieldId;
var totalField = '#input_' + formId + '_' + totalFieldId;
ListFieldRowCount( listField, totalField );
jQuery( listField ).on( 'click', '.add_list_item', function() {
ListFieldRowCount( listField, totalField );
jQuery( listField + ' .delete_list_item' ).removeProp( 'onclick' );
});
jQuery( listField ).on( 'click', '.delete_list_item', function() {
gformDeleteListItem( this, 0 );
ListFieldRowCount( listField, totalField );
});
}
ListFieldRowTotal( 158, 1, 7 );
//ListFieldColumnTotal( form id, list field id, result - field id );
// enable dynamic population on field that is to hold result
</script>
@ensostyle
Copy link
Author

ensostyle commented Nov 22, 2021

Count the number of rows in a Gravity Forms list field and store the result in another field.

@samjco
Copy link

samjco commented Nov 22, 2021

Thanks for your reply!
I've seen your explanation here:
//ListFieldColumnTotal( form id, list field id, result - field id );
:)

Also this function is missing: gformDeleteListItem OR is not working

@samjco
Copy link

samjco commented Jan 27, 2022

<script>

/**Global - for any list fields based on if there is the content inside 1st input**/

function ListFieldRowTotal( formId, fieldId, totalFieldId ) {

    var listField= $( '#gform_fields_' + formId +' .gfield_list_group .gfield_list_cell:first-child input' );
    var totalField = '#input_' + formId + '_' + totalFieldId;
    var totalRows = 0;


    $( listField ).on( 'click', '.add_list_item', function() {
        ListFieldRowCount( listField, totalField );
       // $( listField + ' .delete_list_item' ).removeProp( 'onclick' );
    });

    $( listField ).on( 'click', '.delete_list_item', function() {
        //gformDeleteListItem( this, 0 );
        ListFieldRowCount( listField, totalField );
    });

    function ListFieldRowCount( listField, totalField ) {

      $(listField).each(function(i, val) {
         if ($(this).val() > '') {
              totalRows ++;
          }
       });

            $( totalField ).val( totalRows ).change();
            console.log(totalRows );

     }

}

/**Example ListFieldColumnTotal( "your form id", "your list field id", "your custom field id for the count" )**/

ListFieldRowTotal( 158, 1, 7 );

</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment