Shuffle options in a choice field

Forums Classic DFFS Shuffle options in a choice field

Viewing 0 reply threads
  • Author
    Posts
    • #29531
      Alexander Bautz
      Keymaster

      I got ta question on how to shuffle the options in a choice column. I found a code snippet for shuffling an array here: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array

      Include this snippet in your Custom JS:

      function shuffle(array) {
          var currentIndex = array.length, temporaryValue, randomIndex;
          // While there remain elements to shuffle...
          while (0 !== currentIndex) {
              // Pick a remaining element...
              randomIndex = Math.floor(Math.random() * currentIndex);
              currentIndex -= 1;
              // And swap it with the current element.
              temporaryValue = array[currentIndex];
              array[currentIndex] = array[randomIndex];
              array[randomIndex] = temporaryValue;
          }
          return array;
      }

      The examples below uses a field named “ChoiceColumn1” – replace this with the internal name of your field.

      Use this approach when NOT using the Misc tab option Multiple-choice columns (checkboxes and radio buttons) in multiple columns

      var table = jQuery("#dffs_ChoiceColumn1 table");
      var rows = jQuery(table).find("tr");
      var shuffled = shuffle(rows);
      jQuery(table).html(shuffled);

      Use this approach when using the Misc tab option Multiple-choice columns (checkboxes and radio buttons) in multiple columns

      var rows = jQuery("#dffs_ChoiceColumn1 .dffs_tr_multicolumn");
      jQuery.each(rows, function(i, row){
          var cells = jQuery(row).find("td");
          var shuffled = shuffle(cells);
          jQuery(row).html(shuffled);
      });

      If you use side-by-side for this field you must run the code on every click of the tab and must wrap the code in a function like this:

      function callMeFromTab(){
          // Add code snippet from above here
      }

      Then insert callMeFromTab in the Click function name input in the tab configuration.

      Alexander

      • This topic was modified 4 years ago by Alexander Bautz. Reason: Added code for SBS
Viewing 0 reply threads
  • You must be logged in to reply to this topic.