Recycle Data on Form Submit for Next Form

I was asked to save data entry time for users when many similar records are being typed in. So I wrote this method of basically saving certain data in global variables, then reloading the form and putting the data back in. Because of some complex fields, the putting back in part was not always easy. (This was for tasks relating to an audit, so some data could be the same for multiple records; the form has 4 levels of dependent dropdowns and 3 connected fields filtered by user role).

//  DATA-RECYCLING FOR A RECORD ADDITION FORM
$(document).on('knack-view-render.view_999999', function (event, view, data) {   

    setTimeout(function() {   // Wait to allow previous form to finish saving, if needed (our server is slow and this form does a lot of other stuff too)

            // set one field to Current User
            var userAttributes = window.Knack.getUserAttributes();
            var UserId = userAttributes.id;
            SetDropdownSelection('view_999999-field_1029', UserId);        
            
            // GET SAVED VALUES FROM LAST FORM SUBMISSION BY THIS USER, if any          
            // 1) preset TEXT fields (note the field ids do not include the view number like dropdowns do!!)
            $('#field_892').val(localStorage.getItem("TaskLot"));  
            $('#field_891').val(localStorage.getItem("TaskDesc"));                       

            // 2) preset DROPDOWN fields
            SetDropdownSelection('view_999999-field_936', localStorage.getItem("TaskProject"));        
            SetDropdownSelection('view_999999-field_899', localStorage.getItem("TaskEvent"));          
            SetDropdownSelection('view_999999-field_926', localStorage.getItem("TaskSalesManager"));   
            SetDropdownSelection('view_999999-field_927', localStorage.getItem("TaskDesigner"));    
  
            // 3) Trigger change of one dropdown only because another dropdown depends on it
            $('#view_999999-field_899').chosen().trigger("change");  

    }, 2000);

    // do some other stuff here...                    

    $("#view_999999 .kn-button").on("click", function() {      // intercept the Submit button

        // set local variables to save for next time      
        // get dropdown values like this
        localStorage.setItem("TaskProject", $('#view_999999-field_936').val());
        localStorage.setItem("TaskEvent", $('#view_999999-field_899').val());
        // these are not the normal user ids, they differ with each connected role 
        localStorage.setItem("TaskSalesManager", $('#view_999999-field_926').val());   
        localStorage.setItem("TaskDesigner", $('#view_999999-field_927').val());
        // get text field values like this instead
        var savedValues = {};
        $('#view_999999 input').each(function() {      
            savedValues[this.id] = $(this).val();
        });
        localStorage.setItem("TaskLot", savedValues.field_892);        
        localStorage.setItem("TaskDesc", savedValues.field_891);    

        // THEN Submit the form; form rule is set to automatically reload it
        $("#view_999999 form").submit();   

    });

});


function SetDropdownSelection (FieldID, Value) {
    FieldRef = "#"+FieldID;
    document.getElementById(FieldID).value = Value;     // Replace with your desired value 
    $(FieldRef).trigger("liszt:updated");               // Update the dropdown if using Chosen
    //$(FieldRef).chosen().trigger("change");           // Trigger change event (not needed for most, nothing is listening)
}


Hi @RobinB -

This looks like it might be a way to help with my own challenge of pre-filling form fields. One question. My deployment of KNACK is using the Next-Gen. I think they made some changes - specifically a “shift in JavaScript customization architecture. Classic applications use jQuery and traditional DOM manipulation patterns, while Next-Gen introduces a React-based system with vanilla (aka native) JavaScript patterns, different event handling, and a virtual DOM.”

I am not skilled in JavaScript. I can certainly replace views, etc. in your code, but will I need to re-write this (or have it re-written) for the Next-Gen React-based system?

Jacob

Jacob1, I don’t think this would be affectec by NextGen, but I have not switched our apps yet so cannot speculate 100%. But I’m 98% sure it would be fine. However, if you’re not versed in JS, this might be the easiest feature to implement first.