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

Question

Question

Forms - Duplicating Rows of a table into another table

asked on January 7, 2019 Show version history

Hi there,

I am looking to duplicate rows of 1 specific column of a table into another table.

The use case is this; In a previous step, drawings are added. In the next step, users are assigned to all drawing. In the current step I want to change, users must comment on each drawing.

I cannot use the same table as multiple users must comment on each drawing, so my idea was that on page load, I have javascript that looks at each row of the drawings table and clicks the "add" row buttton of the new table and the default "user" field is that of the current logged in user, which works fine if there is 2 drawings added but not anymore.

The issue I'm having is then filling out the drawing field from the first table, and copying it across to the new fields that are added when the "add" button is clicked by my javascript. I also need to find a way to prevent the drawing field being copied over to the new table if comments already exist from another user. 

I am trying to think if there's possibly another way that this could be achieved. Would there be a way from Workflow to create a new table with multiple rows for each user based on the number of rows from the initial table? I currently fill out the drawing number for the second table with workflow but it only adds the rows once and not duplicating the drawing numbers for each user

My javascript is below;

 

//for each row of column docNo
$('#q23 .docNo input[type=text]').each(function(value) {
       
   var cellText = $(this).val();
 //this correctly outputs all the drawing numbers in the DocNo Field
    alert(cellText);

//add the new rows in my new table
    $('#q3').click();
    if(!$('#q4 .engineerDrawing').val() ) {

    // it moves all the drawing numbers into new fields for some reason rather than appending to the blank as expected
     $('#q23 .docNo').clone().appendTo('#q4 .engineerDrawing');
       
  }
  	  
  });

 

0 0

Answer

SELECTED ANSWER
replied on January 7, 2019

Dear Alex,

 

you can do it in an easier way.

you can use the formula for the the second table fields as follows :

=index(Table_1.Column_1,ROW())   

table1.column_1 is the column the needs to be duplicated from the first table, this formula should be put to the second table field.

 

next you need to use the following code :

var i = $('.table1 tbody').find('tr').length;

for(var j = 0 ; j < i ; j++){
    $('.table2').find('.cf-table-add-row').trigger("click");
}

.table1 is the class of the first table.

.table2 is the class of the second table.

 

make sure that you put "0" as minimum rows for the second table.

i will be awaiting your feedback if this works,

regards,

Maher.

3 0
replied on January 7, 2019

Hi,

 

Unfortunately this adds about 4 rows too many and doesn't copy the value over based on the number of rows.  (so duplicating the number of rows.)

I think for now I will just make this a manual process and populate the dropdown with the values from the rows as their seems to be hidden rows that the javascript is picking up, I'm not entirely sure what the spare <tr>'s are there for. 

0 0
replied on January 7, 2019

dear Alex,

 

i have tried it and it is duplicating the values and adding the same number of rows.

can you send us a screen shot in order to make sure that we are on the same page.

i am sure that this will work.

 

Regards.

 

0 0
replied on January 7, 2019

Hi,

Thanks for your help. 

I have 2 rows in my first table, and on the page load there looks to be 6 in the second table for some reason. 

numberofRows.JPG
numberofRows.JPG (54.61 KB)
0 0
replied on January 7, 2019 Show version history

Alex,

Do not use 'tr' as your selector because Forms tables have other 'tr' elements like in the row headers so that's likely why your count is too high.

If you have the table on the second form (even if it is hidden) there's a much easier way to do what you want.

$('.table1 .propCount').val()

Forms actually adds a hidden input right after each table that tracks the number of rows; I have similar processes and this is the value I use.

Just remember if your second table starts with 1 row, you'll need to subtract 1 from the propCount value (i.e. if table1 has 3 rows and table2 starts with 1 row by default, then you only need to add 2 rows).

Additionally, you could add console.log('add row'); to the code where you add the rows so you can check the browser console and see how many times it is actually running. Depending on where you put it, there's a chance it could be firing more than once and creating the duplicates.

0 0
replied on January 7, 2019 Show version history

Thanks for the help guys, got there in the end. 

Got some help from a previous post to figure this one out..

https://answers.laserfiche.com/questions/87274/Forms-Table--Replicating-data-from-the-above-row#87277

$('#q23 .docNo input[type=text]').each(function(value) {
       $('#q3').click();
  	   var _drawNo = $(this).val();
       var _lastRow = $('li[attr=Engineer_Comments] tbody tr:last');
       _lastRow.find('td.engineerDrawing input.singleline').val(_drawNo);

  });

This looked at the number of rows for my first table; then added the same amount of rows into my second table and copies the value into my field. 

0 0
replied on March 21, 2019

Found another use case for Maher's solution, worked excellently! 

0 0

Replies

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

Sign in to reply to this post.