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

Question

Question

Save as Draft and Database Lookup Question

asked on June 10, 2015

We have a mileage log form that has a from and a to drop down fields, which when selected does a database lookup to auto-calculate the mileage between the two locations.

If a user saves that form as a draft and goes back to it, on load does it perform all of the database lookups again or does it retain the values from the previous session?

0 0

Answer

SELECTED ANSWER
replied on June 24, 2015

So looks like the original blur won't work on newly added rows.

Try this one:

    $('.lookupCondition input').blur(autofill); 
    $('.lookupCondition select').blur(autofill);
    $('.cf-table-add-row').click(function(){
      $('.lookupCondition input').blur(autofill); 
      $('.lookupCondition select').blur(autofill);        
    });

 

0 0

Replies

replied on June 10, 2015

It will use the saved values;

that is, when a field is firstly filled with lookup return value, then changed by user input, after save and resume draft it would show the user changed value.

1 0
replied on June 11, 2015

Rui, thank you for your reply. The reason I asked is because on this mileage log form, if a user is continually adding entries to the table and saving their form as a draft, once they hit about 15 lines in the table, when they go back into the saved draft, it can sometimes take around 5-10 minutes for the form to load before they can do anything, which is making it hard to justify putting the form out for wide use. Do you have any suggestions on how we can fix that or what we can look at?

0 0
replied on June 11, 2015 Show version history

I tried with a table and lookup, but could not reproduce the long time loading issue. The lookup rule I used would match a dropdown field then fill a single line field.

Can you provide more information to help figure out the cause? What kinds of browser would have such issue? Does the issue exist if there is no lookup on the form? Is there custom JavaScript on the form?

And to be more accurate, when resume draft the field would be filled with user changed value, but lookup would still be performed on form load.

0 0
replied on June 12, 2015 Show version history

Below is a screenshot of the form in question. We initially do a lookup based on a hidden field that ahs the logged in users email address. That then populates the employee's information. The employee then goes in as often as needed and adds rows to the "Auto Calculate Mileage Log". The user puts in a date and then selects a "From" and "To" location from the dropdown lists provided. Once a From and To has been selected, it performs a database lookup and pulls back the value for the "miles" field. If they went roundtrip with that entry, they can select the roundtrip checkbox and it doubles the miles automatically.

All of the miles fields are added up and put in the "Total Miles" field at the bottom of the form. That value is then multiplied by .44 to determine the total reimbursement amount.

0 0
replied on June 14, 2015

Looks like there are custom javascript functions on this form. What about try collecting the JavaScript CPU profile from chrome developer tools? It can show how much time a JavaScript function has taken.

0 0
replied on June 15, 2015

I will give that a try and let you know. Thank you.

0 0
replied on June 16, 2015

I have collected the JavaScript CPU profile information., but How do I determine what function to look at? The names seem a little cryptic to me. See attached.

0 0
replied on June 17, 2015 Show version history

You can check the time on Developer Tools Profiles like this:

Without the original code I cannot figure out too much, while I found a function named "tableAutofill" and its total time is 6s while its self time is 0ms. So this function calls some other functions which is time consuming.

To get better effect you can split your script to smaller ones and give them names.

0 0
replied on June 17, 2015 Show version history

Here is all of the custom jQuery we have for this form:

$(document).ready(function () {
  
  //make fields with read-only class read-only
  $('.read-only input').attr('readonly',true);
  $('.read-only select').attr('disabled',true);
  
  //add domain in front of 4x4
  $('.name').on('change lookup', function () {
        if ($('.name input').val().indexOf('domain\\') == -1) {
            $('.name input').val('domain\\' + $('.name input').val());
        }
    });
  //end add domain in front of 4x4
  
  //Start Add row mile fields together
    $(document).ready(function () { 
    $('.cf-table-block').change(sumtotal); 
    function sumtotal() { 
        var sum = 0; 
        $('td.sum').each(function () { 
            var s = 0; 
            $(this).find('input').each(function () { 
                if ($(this).closest('td').siblings('td.roundTripCheck').find('input').is(':checked')) { 
                    s += parseNumber($(this).val() * 2) 
                } 
                else { 
                    s += parseNumber($(this).val()); 
                } 
            }) 
            $(this).find('.subtotal input').val(s); 
            sum += s; 
        }); 
        $('.total input').val((sum).toFixed(2)); 
    } 
    function parseNumber(n) { 
        var f = parseFloat(n); //Convert to float number. 
        return isNaN(f) ? 0 : f; //treat invalid input as 0; 
    } 
}); 
  //End Add row mile fields together
  
  //Start Calculate mileage reimbursement
     $('.cf-table-block').on('blur', 'input', sumreimbursement);
     function sumreimbursement() {
     var s = 0.44;
     $('.totalmiles input').each(function () {
     s *= parseNumber($(this).val());
     });
     $('.totalreimbursement input').val((s).toFixed(2));
     }
     function parseNumber(n) {
     var f = parseFloat(n); //Convert to float number.
     return isNaN(f) ? 0 : f; //treat invalid input as 0;
     }

  //End Calculate mileage reimbursement
  
  //Uncheck verify checkbox field
  	document.getElementById('Field51-0').checked = false;
  
  //End Uncheck verify checkbox field
  
  //Start Auto Fill
  	function tableAutofill() { 
      $('.cf-table-block tbody tr').each(function () { 
      $(this).find('button.autofill').trigger('click'); 
    }); 
    }
  
    function autofill() {  
    $('.autofill').trigger('click');
    }
  
    $(document).ready(function () { 
      $('.lookupCondition input').blur(autofill); 
      $('.cf-table-block').change(tableAutofill); 
    }); 
  //End Auto Fill
  
});

I can see at the end that the "tableAutofill" function that you mention. I believe this was code I was given from Laserfiche in Answers.

0 0
replied on June 17, 2015

In your code it will call function tableAutofill when anything gets changed in the table, while on saved draft load, all fields that have saved value will be considered as changed.

And in the code once a field gets changed, it will click the auto fill button for all rows. As a result, for a table with 15 rows, it will click on auto fill button for hundreds of times on saved draft load. Also each time you the click auto fill button, it will call lookup for 15 times (while this seems to be Forms bug as I think it should only call 1 time). 

0 0
replied on June 18, 2015

Rui, thank you for looking at it for me. That makes sense why it takes so long to load. Is there a way to change the code so that it only clicks the corresponding auto fill button when something in that row changes? Or is there a better way to correct the issue so it is not clicking the auto fill button hundreds of times?

0 0
replied on June 18, 2015

In your code there are both "table content on change" and "input field on blur/lose focus", so I think you can just remove the $('.cf-table-block').change(tableAutofill), then it would only click the auto fill button when someone focuses out the field.

Note that with $('.lookupCondition input') the event is registered on input element (single line field) with "lookupCondition" as css class. If you want to register to drop down field you can add the following:

$('.lookupCondition select').blur(autofill);

Additionally the autofill function need to be changed: now it would click all auto fill button on the page so it will also call lookup for hundreds of times. The suggestion is:

function autofill() {
  $(this).closest("tr").find('button.autofill').trigger('click');
}

 

0 0
replied on June 19, 2015

Thank you for the suggestions and examples. I implemented the code as you suggested and it would not work. To be more specific, the initial row would perform the lookup, but all subsequent rows would not. I narrowed it down to removing $('.cf-table-block').change(tableAutofill). Once I added it back, it started working again.

So this is the code I currently have now, thanks to your suggestions:

//Start Auto Fill
  	function tableAutofill() { 
      $('.cf-table-block tbody tr').each(function () { 
      $(this).find('button.autofill').trigger('click'); 
    }); 
    }
  
    function autofill() {  
    //$('.autofill').trigger('click');
      $(this).closest("tr").find('button.autofill').trigger('click');
    }
  
    $(document).ready(function () { 
      //$('.lookupCondition input').blur(autofill);
      $('.lookupCondition select').blur(autofill);
      $('.cf-table-block').change(tableAutofill); 
    }); 
  //End Auto Fill

Does that look correct? I have not yet tested it to see if it responds faster after saving as draft.

0 0
replied on June 19, 2015

I just did another JavaScript CPU profile with the changes I listed above and it is slightly better, but not much.

0 0
replied on June 22, 2015

I meant to comment out $('.cf-table-block').change(tableAutofill) and keep $('.lookupCondition input').blur(autofill);

0 0
replied on June 23, 2015

When I do that only first row does the lookup. All additional rows never perform the lookup:

0 0
replied on June 23, 2015

I didn't see the auto fill button in your screenshot. Did you hide them?

0 0
replied on June 24, 2015

Yes, I hide it using the following CSS code:

.autofill {display: none;}

 

0 0
SELECTED ANSWER
replied on June 24, 2015

So looks like the original blur won't work on newly added rows.

Try this one:

    $('.lookupCondition input').blur(autofill); 
    $('.lookupCondition select').blur(autofill);
    $('.cf-table-add-row').click(function(){
      $('.lookupCondition input').blur(autofill); 
      $('.lookupCondition select').blur(autofill);        
    });

 

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

Sign in to reply to this post.