- Posts: 26
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Randomize columns in a multiple response grid [Array (Numbers)]
- Gustavo_Kremer
- Topic Author
- Offline
- Junior Member
Less
More
3 years 9 months ago #200829
by Gustavo_Kremer
Randomize columns in a multiple response grid [Array (Numbers)] was created by Gustavo_Kremer
Hi, in the attached example, I have a grid/table with brands in its rows and attributes in its columns. Obviously, I can filter which rows are to be shown, and also rows can be shown in randomized order; but additionally, I need to randomize the columns order. Is there a way to do that using Javascript? Many thanks in advance!
Attachments:
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10635
- Thank you received: 3699
3 years 9 months ago - 3 years 9 months ago #200832
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Randomize columns in a multiple response grid [Array (Numbers)]
This script will randomize the column order in an array-numbers question in LS version 3.x:
Here is your sample survey back with that script incorporated:
Code:
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ randomArrayNumCols({QID}); }); // A function to randomize columns in an array-numbers question function randomArrayNumCols(qID){ var thisQuestion = $('#question'+qID); // Index the array columns $('table.subquestion-list tr', thisQuestion).each(function(i) { $('> *', this).each(function(i) { $(this).attr('data-index', i); }); }); // Create an array of the columns var columns = []; $('table.subquestion-list tr[id^="javatbd"]:eq(0) td.answer-item', thisQuestion).each(function(i) { columns.push($(this).attr('data-index')); }); // Shuffle the columns array columns = $.shuffle(columns); // Reposition the row elements $('table.subquestion-list tr', thisQuestion).each(function(i) { var thisRow = $(this); $(columns).each(function(i, val) { thisRow.append($('[data-index="'+val+'"]', thisRow)); thisRow.append($('.col-no-answer', thisRow)); }); }); } // A function to shuffle elements or contents of arrays (function($){ $.fn.shuffle = function() { return this.each(function(){ var items = $(this).children(); return (items.length) ? $(this).html($.shuffle(items)) : this; }); } $.shuffle = function(arr) { for( var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x ); return arr; } })(jQuery); </script>
Here is your sample survey back with that script incorporated:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 3 years 9 months ago by tpartner.
The following user(s) said Thank You: Gustavo_Kremer
The topic has been locked.
- Gustavo_Kremer
- Topic Author
- Offline
- Junior Member
Less
More
- Posts: 26
- Thank you received: 0
3 years 9 months ago #200838
by Gustavo_Kremer
Replied by Gustavo_Kremer on topic Randomize columns in a multiple response grid [Array (Numbers)]
Excellent!!! Many, many thanks Tony!
The topic has been locked.