Skip to content

Instantly share code, notes, and snippets.

@ensostyle
Created January 11, 2017 12:15
Show Gist options
  • Save ensostyle/24c9ace4526c9ac63f78bfdd8a861570 to your computer and use it in GitHub Desktop.
Save ensostyle/24c9ace4526c9ac63f78bfdd8a861570 to your computer and use it in GitHub Desktop.
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>
@samjco
Copy link

samjco commented Nov 22, 2021

Can you kindly explain your code?

@ensostyle
Copy link
Author

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>

@basmiddelham
Copy link

Updated for latest Gravityforms version:

function ListFieldRowCount(listField, totalField) {
  var totalRows = $(listField).find('.gfield_list .gfield_list_groups .gfield_list_group').length;
  $(totalField).val(totalRows).change();
}

function ListFieldRowTotal(formId, fieldId, totalFieldId) {
  var listField = '#field_' + formId + '_' + fieldId;
  var totalField = '#input_' + formId + '_' + totalFieldId;
  ListFieldRowCount(listField, totalField);

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

  $(listField).on('click', '.delete_list_item', function () {
    gformDeleteListItem(this, 0);
    listField = '#field_' + formId + '_' + fieldId;
    ListFieldRowCount(listField, totalField);
  });
}

ListFieldRowTotal(7, 35, 37);
// ListFieldColumnTotal( form id, list field id, result field id )
// enable dynamic population on field that is to hold result

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