- Posts: 173
- Thank you received: 27
Ask the community, share ideas, and connect with other LimeSurvey users!
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
<script src="pathTo/jquery.easy-autocomplete.min.js"></script> <link href="pathTo/easy-autocomplete.min.css" rel="stylesheet" /> <script type="text/javascript" data-author="Tony Partner"> $(document).on('ready pjax:scriptcomplete',function(){ // Identify this question var thisQuestion = $('#question{QID}'); // Define the data var fullData = [ { 'region': 'Ontario', 'city': 'ONA1', 'dealer': 'ON11A1' }, { 'region': 'Ontario', 'city': 'ONA1', 'dealer': 'ON11A2' }, { 'region': 'Ontario', 'city': 'ONA1', 'dealer': 'ON11B1' }, { 'region': 'Ontario', 'city': 'ONA1', 'dealer': 'ON11B2' }, { 'region': 'Ontario', 'city': 'ONA2', 'dealer': 'ON12A1' }, { 'region': 'Ontario', 'city': 'ONA2', 'dealer': 'ON12A2' }, { 'region': 'Ontario', 'city': 'ONA2', 'dealer': 'ON12B1' }, { 'region': 'Ontario', 'city': 'ONA2', 'dealer': 'ON12B2' }, { 'region': 'Ontario', 'city': 'ONB1', 'dealer': 'ON21A1' }, { 'region': 'Ontario', 'city': 'ONB1', 'dealer': 'ON21A2' }, { 'region': 'Ontario', 'city': 'ONB1', 'dealer': 'ON21B1' }, { 'region': 'Ontario', 'city': 'ONB1', 'dealer': 'ON21B2' }, { 'region': 'Ontario', 'city': 'ONB2', 'dealer': 'ON22A1' }, { 'region': 'Ontario', 'city': 'ONB2', 'dealer': 'ON22A2' }, { 'region': 'Ontario', 'city': 'ONB2', 'dealer': 'ON22B1' }, { 'region': 'Ontario', 'city': 'ONB2', 'dealer': 'ON22B2' }, { 'region': 'Quebec', 'city': 'QCA1', 'dealer': 'QC11A1' }, { 'region': 'Quebec', 'city': 'QCA1', 'dealer': 'QC11A2' }, { 'region': 'Quebec', 'city': 'QCA1', 'dealer': 'QC11B1' }, { 'region': 'Quebec', 'city': 'QCA1', 'dealer': 'QC11B2' }, { 'region': 'Quebec', 'city': 'QCA2', 'dealer': 'QC12A1' }, { 'region': 'Quebec', 'city': 'QCA2', 'dealer': 'QC12A2' }, { 'region': 'Quebec', 'city': 'QCA2', 'dealer': 'QC12B1' }, { 'region': 'Quebec', 'city': 'QCA2', 'dealer': 'QC12B2' }, { 'region': 'Quebec', 'city': 'QCB1', 'dealer': 'QC21A1' }, { 'region': 'Quebec', 'city': 'QCB1', 'dealer': 'QC21A2' }, { 'region': 'Quebec', 'city': 'QCB1', 'dealer': 'QC21B1' }, { 'region': 'Quebec', 'city': 'QCB1', 'dealer': 'QC21B2' }, { 'region': 'Quebec', 'city': 'QCB2', 'dealer': 'QC22A1' }, { 'region': 'Quebec', 'city': 'QCB2', 'dealer': 'QC22A2' }, { 'region': 'Quebec', 'city': 'QCB2', 'dealer': 'QC22B1' }, { 'region': 'Quebec', 'city': 'QCB2', 'dealer': 'QC22B2' }, { 'region': 'Nova Scotia', 'city': 'NSA1', 'dealer': 'NS11A1' }, { 'region': 'Nova Scotia', 'city': 'NSA1', 'dealer': 'NS11A2' }, { 'region': 'Nova Scotia', 'city': 'NSA1', 'dealer': 'NS11B1' }, { 'region': 'Nova Scotia', 'city': 'NSA1', 'dealer': 'NS11B2' }, { 'region': 'Nova Scotia', 'city': 'NSA2', 'dealer': 'NS12A1' }, { 'region': 'Nova Scotia', 'city': 'NSA2', 'dealer': 'NS12A2' }, { 'region': 'Nova Scotia', 'city': 'NSA2', 'dealer': 'NS12B1' }, { 'region': 'Nova Scotia', 'city': 'NSA2', 'dealer': 'NS12B2' }, { 'region': 'Nova Scotia', 'city': 'NSB1', 'dealer': 'NS21A1' }, { 'region': 'Nova Scotia', 'city': 'NSB1', 'dealer': 'NS21A2' }, { 'region': 'Nova Scotia', 'city': 'NSB1', 'dealer': 'NS21B1' }, { 'region': 'Nova Scotia', 'city': 'NSB1', 'dealer': 'NS21B2' }, { 'region': 'Nova Scotia', 'city': 'NSB2', 'dealer': 'NS22A1' }, { 'region': 'Nova Scotia', 'city': 'NSB2', 'dealer': 'NS22A2' }, { 'region': 'Nova Scotia', 'city': 'NSB2', 'dealer': 'NS22B1' }, { 'region': 'Nova Scotia', 'city': 'NSB2', 'dealer': 'NS22B2' }, { 'region': 'Saskatchewan', 'city': 'SKA1', 'dealer': 'SK11A1' }, { 'region': 'Saskatchewan', 'city': 'SKA1', 'dealer': 'SK11A2' }, { 'region': 'Saskatchewan', 'city': 'SKA1', 'dealer': 'SK11B1' }, { 'region': 'Saskatchewan', 'city': 'SKA1', 'dealer': 'SK11B2' }, { 'region': 'Saskatchewan', 'city': 'SKA2', 'dealer': 'SK12A1' }, { 'region': 'Saskatchewan', 'city': 'SKA2', 'dealer': 'SK12A2' }, { 'region': 'Saskatchewan', 'city': 'SKA2', 'dealer': 'SK12B1' }, { 'region': 'Saskatchewan', 'city': 'SKA2', 'dealer': 'SK12B2' }, { 'region': 'Saskatchewan', 'city': 'SKB1', 'dealer': 'SK21A1' }, { 'region': 'Saskatchewan', 'city': 'SKB1', 'dealer': 'SK21A2' }, { 'region': 'Saskatchewan', 'city': 'SKB1', 'dealer': 'SK21B1' }, { 'region': 'Saskatchewan', 'city': 'SKB1', 'dealer': 'SK21B2' }, { 'region': 'Saskatchewan', 'city': 'SKB2', 'dealer': 'SK22A1' }, { 'region': 'Saskatchewan', 'city': 'SKB2', 'dealer': 'SK22A2' }, { 'region': 'Saskatchewan', 'city': 'SKB2', 'dealer': 'SK22B1' }, { 'region': 'Saskatchewan', 'city': 'SKB2', 'dealer': 'SK22B2' } ]; // Define some elements and vars var keys = ['region', 'city', 'dealer']; var input1 = $('.answer-item:eq(0) input:text.form-control', thisQuestion); var inputs = { 0: $('.answer-item:eq(0) input:text.form-control', thisQuestion), 1: $('.answer-item:eq(1) input:text.form-control', thisQuestion), 2: $('.answer-item:eq(2) input:text.form-control', thisQuestion) } var answers = { 0: $.trim($(inputs[0]).val()), 1: $.trim($(inputs[1]).val()), 2: $.trim($(inputs[2]).val()) } // Create an array of "region" values var regions = []; $.each(fullData, function(i, val) { if(!regions.includes(val.region)) { regions.push(val.region); } }); // Initiate autocomplete on the first input var aOptions1 = { data: regions, list: { maxNumberOfElements: 100, match: { enabled: true }, onChooseEvent: function() { //If new selection... if($.trim(inputs[0].val()) != answers[0]) { answers[0] = $.trim(inputs[0].val()); // Reset following items $('li.answer-item:gt(0) input:text', thisQuestion).val('').trigger('keyup').easyAutocomplete({ 'data': '' }); //Initiate autocomplete on next input childAutocomplete(0); } } } }; inputs[0].easyAutocomplete(aOptions1); function childAutocomplete(index) { if((index+1) < $('li.answer-item', thisQuestion).length) { var parentInput = inputs[index]; var nextInput = inputs[(index+1)]; var key1 = keys[index]; var key2 = keys[(index+1)]; // Define the new data var thisFullData = fullData.filter(function(obj) { return (obj[key1] == $.trim(parentInput.val())); }); var thisData = []; $.each(thisFullData, function(i, val) { if(!thisData.includes(val[key2])) { thisData.push(val[key2]); } }); //Initiate autocomplete on next input var aOptions = { data: thisData, list: { maxNumberOfElements: 100, match: { enabled: true }, onChooseEvent: function() { //If new selection... if($.trim($(nextInput).val()) != answers[(index+1)]) { answers[(index+1)] = $.trim($(nextInput).val()); // Reset following items $('li.answer-item:gt('+index+1+') input:text', thisQuestion).val('').trigger('keyup').easyAutocomplete({ 'data': '' }); //Initiate autocomplete on next input childAutocomplete(index+1); } } } }; nextInput.easyAutocomplete(aOptions); } } // Returning to page if($.trim(inputs[0].val()) != '') { childAutocomplete(0); } if($.trim(inputs[1].val()) != '') { childAutocomplete(1); } }); </script>
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.
Please Log in to join the conversation.