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

Question

Question

Can rows be automatically added to a table based on a previous field input?

asked on April 4, 2016

The form I have is a "Crew Summary" sheet, recapping activity performed by workers on a specific crew.  In a field prior to the table, the user will be entering the number of employees on the crew, and I would like to have exactly that number of rows appear in a table further down in the form.  The user will then need to enter data in columns for each of those rows.  

If this is possible, can someone give an example of the javascript that would be necessary to complete this task?  

Any help is greatly appreciated!

0 0

Answer

SELECTED ANSWER
replied on April 4, 2016

Melanie,

Something like the following should do what you want. It will add or delete rows as needed.

$(document).ready(function () {

  $('.Employees').on('change', 'input', addRows);
  
  function addRows(event) {
    var employees = parseInt($(event.target).val());
    var currentRows = $('.ActivityTable tr').length - 1; //Remove header row from count
    for (i=currentRows; i < employees; i++) {
      $('.ActivityTable .cf-table-add-row').click();
    }
    for (i=employees; i < currentRows; i++) {
      $('.ActivityTable tr .action .cf-table-delete').last().click();
    }
  }
});

Deleting rows removes them from the bottom of the table. If they have data in them, that data will be lost.

0 0

Replies

replied on April 4, 2016

Thank you so much!! This works perfectly.

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

Sign in to reply to this post.