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

Question

Question

Duplicate table row with values to the end of table using Forms Layout Designer

asked on August 7, 2024

Hi,

My goal is to create copy and paste like functionality by selecting a checkbox in the table, click a button (thank you for adding button support to the Forms Layout Designer!) to copy selected rows, and add the rows with values to the end of the table.   I can get it to add the row(s) with LFForm.addRow but not add the values to the added rows.  Seems like it's possible, but I'm not sure where I'm going awry.

Table looks like this, the Total Cost field is calculated

And my js code looks like the following.  I've tried a couple iterations with and without adding the LFForms.addRow(), and using the arrays rather than field indexes to fully replace the fields.  As written the error "Error: Unable to find field. Please check your ID." shows.  Any ideas how to get the duplicated values  to go into the new row or if it's possible?

window.duplicateOrder1Rows=function() {
  var drow=LFForm.getFieldValues({fieldId: 229}); /*Duplicate checkbox, checked value ='V_1'*/
  var dqty=LFForm.getFieldValues({fieldId: 206});
  var ditmn=LFForm.getFieldValues({fieldId: 207});
  var ditmd=LFForm.getFieldValues({fieldId: 208});
  var duntc=LFForm.getFieldValues({fieldId: 209});
  var dshp=LFForm.getFieldValues({fieldId: 225});
  console.log(Array.isArray(drow));
  if (Array.isArray(drow)===false){
    drow=new Array(drow);
    }
  console.log(Array.isArray(dqty));
  if (Array.isArray(dqty)===false){
    dqty=[dqty];
    }
  dqty=dqty.map(Number);
  console.log("drow:",drow);
  console.log("dqty:",dqty);
  for (var di = 0; di < drow.length; di++) {
    console.log("drow[",di,"]", drow[di].value);
    console.log(drow[di].value[0]=='V_1');
      if (drow[di].value[0]=='V_1'){
        console.log("inside add row value")
        dqty.push(0);
        var didx=dqty.length-1;
        console.log(didx);
        LFForm.addRow({fieldId: 205}, 1);
        LFForm.setFieldValues({fieldId: 206, index:didx}, 0);
        LFForm.setFieldValues({fieldId: 207, index:didx}, ditmn[di]);
        LFForm.setFieldValues({fieldId: 208, index:didx}, ditmd[di]);
        LFForm.setFieldValues({fieldId: 209, index:didx}, duntc[di]);
        LFForm.setFieldValues({fieldId: 225, index:didx}, dshp[di]);  
      }
  }
};

I'm using on prem Laserfiche Forms 11 Update 5 with the Forms Layout Designer.

Possible Enhancements:  You can see in the code that there's a couple basic improvements I'd love to see, including table getFieldValues always being in an array, and numbers in number values always being numbers. 

Appreciate any help!

0 0

Answer

SELECTED ANSWER
replied on August 7, 2024 Show version history

I haven't tested it, but I think the issue is that the LFForm functions being called on lines 27-32 are happening asyncronously, so the values are possibly trying to be applied before the row is actually created.  Zachary St. Lous is the pro at this, I can barely wrap my brain around it, but I think you might be able to get it working by:

  1. Adding the async keyword to the beginning of line 1
  2. And adding the await keyword to the beginning of lines 27 to 32.

That will hopefully get the script to wait for the completed promises on the creation of the new row and added values before proceeding with the rest of the code.

I've probably completely explained that wrong about promises.  As I said, I can barely wrap my brain around it.

2 0
replied on August 7, 2024

@████████  That worked!  Thank you!!!

1 0
replied on August 7, 2024

I'm so glad to hear that!

0 0
replied on August 8, 2024 Show version history

As a rule of thumb, any LFForm API that updates the state of the form uses promises. This answer is correct, await is needed for lines 27 through 32.

 

To riddle your brain a little further, an unnecessary optimization but good to know is you can do lines 27-32 as follows

// first add the row
await LFForm.addRow({fieldId: 205}, 1);
// then set all values in parallel
await Promise.all([
  LFForm.setFieldValues({fieldId: 206, index:didx}, 0),
  LFForm.setFieldValues({fieldId: 207, index:didx}, ditmn[di]),
  LFForm.setFieldValues({fieldId: 208, index:didx}, ditmd[di]),
  LFForm.setFieldValues({fieldId: 209, index:didx}, duntc[di]),
  LFForm.setFieldValues({fieldId: 225, index:didx}, dshp[di]), 
]);

 

2 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.