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

Question

Question

For Each Row, Add Collection

asked on March 6, 2018

I am working on a Forms process that has 2 forms. The first form has a table that has 4 columns. The second form has a collection with a number of fields. What is wanted is that for each row that is in the table in the first form (can be displayed on the 2nd form as well) to add a collection in the second form and copy fields from columns 1 and 2 from the corresponding row into the collection that was added.

Does anyone now if that is possible? I've never programmatically added a collection and wasn't sure if it was even possible. Forms 10.3.

1 0

Answer

SELECTED ANSWER
replied on March 6, 2018 Show version history

I gave the collection and table the following classes, respectively:

  • myCollection
  • myTable

You can have your collection and table match in number of rows using the following in the second form:

$(document).ready(function(){
  var rowNumber = $(".myTable tbody tr").length;
  for (i=1; i<rowNumber; i++)
  {
    $('.myCollection .cf-collection-append').trigger('click');
  }
});

As for setting/mirroring values, this is most easily done through assigning the fields within the table and collections a class. For my example, I have two fields I want to mirror in both the table and the collection, "Number" and Color". 

  • number
  • numberCol
  • color
  • colorCol

So the first step is grabbing the appropriate value from the table. This is done with:

var number = $('.myTable > div.cf-table_parent > table > tbody > tr:nth-child(1) > td.number > div input').val();

For this example, I'm using the first value in the "Number" field of the table. This is denoted by both "tr:nth-child(1)", which is selecting the first child of "tbody", and "td.number", which selects the number cell of the table.

Then I need to set the value to the corresponding collection row and field.

$('.myCollection > div.cf-collection.clearfix.kx-repeatable > div:nth-child(1) > ul.rpx > li.numberCol > div input').val(number);

"div:nth-child(1)" refers to the first row. "li.numberCol" refers to the list item/numberCol class we assigned to this field. 

Combining this with the code to add rows to the collection, and using a variable i to alter my selector only if my number of rows is greater than 1, I get the following:

$(document).ready(function(){
  var rowNumber = $(".myTable tbody tr").length;

  //Set collection row 1 values from table row 1 values
  var number = $('.myTable > div.cf-table_parent > table > tbody > tr:nth-child(1) > td.number > div input').val();
  $('.myCollection > div.cf-collection.clearfix.kx-repeatable > div:nth-child(1) > ul.rpx > li.numberCol > div input').val(number);

  var color = $('.myTable > div.cf-table_parent > table > tbody > tr:nth-child(1) > td.color > div input').val();
  $('.myCollection > div.cf-collection.clearfix.kx-repeatable > div:nth-child(1) > ul.rpx > li.colorCol > div input').val(color);

  //Add rows and enter data 
  if(rowNumber > 1)
  {
    for (i=2; i<rowNumber+1; i++) // mind the weird indexing :P
    {
      $('.myCollection .cf-collection-append').trigger('click');

      var number = $('.myTable > div.cf-table_parent > table > tbody > tr:nth-child(' + i +') > td.number > div input').val();
      $('.myCollection > div.cf-collection.clearfix.kx-repeatable > div:nth-child(' + i +') > ul.rpx > li.numberCol > div input').val(number);

      var color = $('.myTable > div.cf-table_parent > table > tbody > tr:nth-child(' + i +') > td.color > div input').val();
      $('.myCollection > div.cf-collection.clearfix.kx-repeatable > div:nth-child(' + i +') > ul.rpx > li.colorCol > div input').val(color);
    }
  }
});

Let me know if you have any questions.

2 0

Replies

replied on August 18, 2020

How do you grab the rowNumber of a collection instead of a table shown above?

1 0
replied on June 28, 2020

Hello all,

I have a checkbox collection in two different Collections in the form.  Can someone help me with copying the checked state of the checkbox collection from one to the other similar to the textbox in this thread. I have managed to copy the selected index of a dropdown list but unable to figure out the checkbox.  Any help would be appreciated.  Thanks in advance.

 

Bala

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

Sign in to reply to this post.