Skip to content

Instantly share code, notes, and snippets.

@annuman97
Created July 24, 2023 09:31
Show Gist options
  • Save annuman97/c7f7e49068f52abfbe7693e2f79cf6fe to your computer and use it in GitHub Desktop.
Save annuman97/c7f7e49068f52abfbe7693e2f79cf6fe to your computer and use it in GitHub Desktop.
Randomize/shuffle the table rows each time after refresh the page
(function($){
//Shuffle all rows, while keeping the first column
//Requires: Shuffle
$.fn.shuffleRows = function(){
return this.each(function(){
var main = $(/table/i.test(this.tagName) ? this.tBodies[0] : this);
var firstElem = [], counter=0;
main.children().each(function(){
firstElem.push(this.firstChild);
});
main.shuffle();
main.children().each(function(){
this.insertBefore(firstElem[counter++], this.firstChild);
});
});
}
/* Shuffle is required */
$.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)
function runAll(){
$(document).on('click', 'tbody tr', function(e){
e.preventDefault();
var url = $(this).find('a.ninja_table_permalink').attr('href');
if(url != ""){
window.open(url, '_blank');
}
});
}
runAll();
$table.on('after.ft.paging', function () {
runAll();
});
$table.on("after.ft.filtering", function () {
runAll();
});
$(".footable").shuffleRows();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment