I gave the collection and table the following classes, respectively:
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.
