You are viewing limited content. For full access, please sign in.

Question

Question

How to count number of rows in a table and place in another place?

asked on June 13, 2017

I have a table where the user can add rows. I would like to place the number of rows in another field. How would I go about doing this? So I would like the number of rows in the New Product table to be placed into the Total Batches Field:

 

 

Thanks!

0 0

Answer

SELECTED ANSWER
replied on June 13, 2017

You can use custom javascript for this. In the example, the table has the css class name "mytable" and the field containing the row count has the css class name "myrows"

$(document).ready(function() {
  
  rowcount();

  $('.mytable').on('click', '.form-del-field', function () {
    rowcount();
  });
  
  $('.mytable').on('click', '.cf-table-add-row', function () {
    rowcount();
  });
  
  function rowcount() {
    $('.myrows input').val($('.mytable tr').length-1);
  }
});
4 0
replied on June 13, 2017

Perfect! Thanks you

1 0
replied on September 4, 2018

Thank you for asking and posting this!  Worked Perfectly.

0 0

Replies

replied on July 6, 2022

I used the code given here to build a table (tablejq). When adding rows, the total-field (totalInTable) gets updated perfectly, but when a row is deleted using the 'x', the total-field remains the same.  Can someone please help. Thank you.

My code is:

$(document).ready(function() {

  rowcount();

  $('.tablejq').on('click', '.form-del-field', function () {
    rowcount();
  });
  
  $('.tablejq').on('click', '.cf-table-add-row', function () {
    rowcount();
  });
  
  function rowcount() {
    $('.totalInTable input').val($('.tablejq tr').length-1);
  }
});
 

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.