My prepopulated answers are getting overwritten

More
1 month 3 weeks ago - 1 month 3 weeks ago #199329 by LarryMartell
I have some JS code that creates array questions and I pre-populate some rows with data the users cannot delete. Users can add more rows to the array questions. If a user takes a survey and submits it, and then goes back to the survey some of my pre-populate answers get overwritten with data the user has entered in other rows. This happens after my JS code has run in the load() function. Is there any way I can prevent lime from doing this? We are running version 2.73.1.

Thanks
Last edit: 1 month 3 weeks ago by LarryMartell.

Please Log in to join the conversation.

LimeSurvey Partners
More
1 month 2 weeks ago #199336 by tpartner
We cannot answer that without seeing a sample survey. We have no clue what your JS does.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Official LimeSurvey Partner - partnersurveys.com

Please Log in to join the conversation.

More
1 month 2 weeks ago #199403 by LarryMartell
Here is my code:
$(document).ready(function () {
 
    // Look for questions with an "arrayGroup" class
    // These denote question groups
 
    $.each($('.arrayGroup input'), function (index, groupInput) {
        var inputName = $(groupInput).attr('name').trim();
        var inputId = $(groupInput).attr('id').trim();
        var nameSegments = inputName.split('X');
        window._de_surveryId = nameSegments[0].trim();
        var groupId = nameSegments[1].trim();
        var questionId = nameSegments[2].trim();
        initialise(groupId, inputId);
    });
});
 
function initialise(groupId, inputId) {
 
    var url = 'https://api' + window.location.hostname + '/group/' + groupId + '/arrays';
    console.log('url=' + url);
    $.getJSON(url, function (data) {
        if (!window._de_data) window._de_data = {};
        window._de_data[groupId] = data;
        if (!window._de_maxRow) window._de_maxRow = {};
        if (!window._de_maxRow[groupId]) window._de_maxRow[groupId] = {};
        window._de_maxRow[groupId][inputId] = 0;
        var groupData = getGroupData(data, inputId);
        processGroupSegment(data, groupId, inputId);
        if (groupData.table_id == "SystemArch") {
            getMachineData(groupId, inputId);
        }
    }).fail(function (jqxhr, textStatus, error) {
        var err = textStatus + ", " + error;
        console.log("Request Failed: " + err);
    });
}
 
function getMachineData(groupId, inputId) {
    var machine_data_url = 'https://api' + window.location.hostname + '/machine/' + userToken;
    var nrows = 1;
    console.log('url=' + machine_data_url);
	$('.questionCell .mandatory.question-container').show()
 
    $.getJSON(machine_data_url, function (data) {
        processMachineData(data, groupId, inputId, userToken);
        for (var row = 1; row <= data.length; row++) {
            $('#' + getShowId(groupId, inputId, row) + ' input').val('true');
            var tr = getTr(groupId, inputId, row);
            tr.removeClass('hide');
            $('#table_SystemArch tr').eq(row).find('td').eq(10).find('button').prop('disabled', true)
            tr.find('td').each(function (i, el) {
               $(this).find('.question-container').show()
            });
        }
        $("#table_SystemArch tr").each(function(i, v) {
            if (i) {
                if ($(this).find("td").eq(2).find('.form-control.list-question-select').val() !== '') {
                    nrows++;
                }
            }
        });
        nrows--;
        window._de_maxRow[groupId][inputId] = nrows;
        $('#' + getShowId(groupId, inputId, nrows) + ' input').val('false');
        var tr = getTr(groupId, inputId, nrows);
        tr.addClass('hide');
	$('.questionCell .mandatory.question-container').show()
    }).fail(function (jqxhr, textStatus, error) {
        var err = textStatus + ", " + error;
        console.log("Request Failed: " + err);
    });
}
 
function processMachineData(data, groupId, inputId, app_code) {
    var component_map = {
       'Web Tier': 'A1',
       'Application Tier': 'A2',
       'Database': 'A3',
       'Messinging Layer': 'A4',
       'Load Balancer': 'A5',
       'Mobile Application': 'A6',
       'Other': 'A7'
    };
    var component_codes = {
       'Web Tier': 'WT',
       'Application Tier': 'AT',
       'Database': 'DT',
       'Messinging Layer': 'ML',
       'Load Balancer': 'LB',
       'Mobile Application': 'MA',
       'Other': 'OT'
    };
 
    var component;
    var current_component = '';
    var component_counter = 0;
 
    for (var row = 0; row < data.length; row++) {
        if (data[row].component in component_map) {
           component = component_map[data[row].component];
           component_code = component_codes[data[row].component];
        }
        else {
           component = component_map['Other'];
           component_code = component_codes['Other'];
        }
        if (component != current_component) {
            current_component = component;
            component_counter = 0;
        }
        else {
            var sw_name_field = $('#table_SystemArch tr').eq(row+1).find('td').eq(3).find('.form-control.text');
            if (sw_name_field.val() == '') {
                component_counter += 1;
            }
        }
 
        addArchRow(row+1, component, data[row].os, data[row].os_version, groupId, inputId, app_code, component_counter, component_code);
    }
}
 
function addArchRow(row, component, os, version, groupId, inputId, app_code, component_counter, component_code) {
    var sw_name_field = $('#table_SystemArch tr').eq(row).find('td').eq(3).find('.form-control.text');
    var version_field = $('#table_SystemArch tr').eq(row).find('td').eq(4).find('.form-control.text');
    var latency_field = $('#table_SystemArch tr').eq(row).find('td').eq(8).find('.form-control.list-question-select');
    var statefulness_field = $('#table_SystemArch tr').eq(row).find('td').eq(9).find('.form-control.list-question-select');
 
    if (sw_name_field.val() == '' || typeof sw_name_field.val() === 'undefined') {
        sw_name = app_code.substring(app_code.length - 4, app_code.length) + component_code + pad(parseInt(component_counter), 2);
        sw_name_field.val(sw_name); 
        sw_name_field.css('color', 'red');
    }
 
    if (component !== null && component !== '') {
        $('#table_SystemArch tr').eq(row).find('td').eq(2).find('.form-control.list-question-select').val(component);
        $('#table_SystemArch tr').eq(row).find('td').eq(2).find('.form-control.list-question-select').mousedown(function(e){
            e.preventDefault();
        });
    }
 
    if (os !== null && os !== '') {
        $('#table_SystemArch tr').eq(row).find('td').eq(5).find('.form-control.text').val(os);
        $('#table_SystemArch tr').eq(row).find('td').eq(5).find('.form-control.text').prop('readonly', true);
    }
 
    if (version !== null && version !== '') {
        $('#table_SystemArch tr').eq(row).find('td').eq(6).find('.form-control.text').val(version);
        $('#table_SystemArch tr').eq(row).find('td').eq(6).find('.form-control.text').prop('readonly', true);
    }
}

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now