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

Discussion

Discussion

Checkboxes populated by SQL Look Ups

posted on June 24, 2020 Show version history

I would like to request the ability to have Checkboxes load in the values from a Database Look up.

0 0
replied on June 24, 2020

I was just working on this yesterday. I found a way where I can store the values of a checkbox field in a free text field as a string of 0's and 1's to represent checked or unchecked, then save that value to the DB. On a lookup, another JS script will iterate through those values and either check or uncheck the checkbox appropriately. My script is a little long because it accounts for checkbox fields that are part of a collection, and a checkbox field that isn't, but here it is:

 

    //#region Convert from checkboxes to string for selection storage in DB

    // adds the event listeners to every appropriate checkbox on the form
    function addListenersToCheckboxes() {
        $('.documents-checkboxes .choice input').change(event, populateBinaryFields);
    }
    // runs the function one time on form load
    addListenersToCheckboxes();
    // When they add a new property, runs the function to add event listeners
    $('#q66 .cf-collection-append').click(addListenersToCheckboxes);
    // function that the listeners execute. Finds its correct string values input, and runs the script
    // to convert the selected checkboxes to a sequence of 1's and 0's
    function populateBinaryFields(event) {
        // targets the input for all collections
        let checkboxValuesField = $(event.target).closest('.rpx').find('.checkbox-values input');
        // if it's not an iterable, hard-code its checkbox-values input field
        if (checkboxValuesField.length === 0) {
            checkboxValuesField = $('#Field89')
        }
        
        // runs through each checkbox of the target checkbox's collection, determines if it's checked or not, and
        // creates a string
        let valuesArray = [];
        $(event.target).closest('.documents-checkboxes').find('.choice input').each(function () {
            if ($(this).is(':checked')) {
                valuesArray.push('1')
            }
            else {
                valuesArray.push('0')
            }
        });
        //populates the value of the checkbox values field with the string value determined above
        checkboxValuesField.val(valuesArray.join(''));
    }
    //#endregion

    //#region Converts from the string to checkbox selections for population on the form
    // Does the opposite of the above function. Takes the string value from the lookup and checks/unchecks
    // the checkboxes based on the string.
    function populateCheckboxes() {
        $('.checkbox-values input').each(function() {
            const valuesArray = $(this).val().split('');
            let checkboxField = $( this ).closest('.rpx').find('.documents-checkboxes');
            if (checkboxField.length === 0) {
                checkboxField = $('#q87');
            }
            valuesArray.forEach(function (value, index) {
                const checkbox = $( checkboxField.find('input').eq(index) )
                if (value === '1') {
                    checkbox.prop('checked', true);
                }
                else {
                    checkbox.prop('checked', false);
                }
            })
        })
    };
    populateCheckboxes();
    //#endregion

 

2 0
replied on July 23, 2020

Do you have a screenshot of the fields that the code above is relating to?  I'm trying to work out what the different parts of the code does as I'm only interested in updating Checkboxes that have there related text fields updated from a SQL Lookup when another lookup field has been updated.

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

Sign in to reply to this post.