Randomize table cells

I know its beats sounds are connected, but is there a way to randomize the contents of the table? exactly cells. The purpose of this is that I have a table created from several xml, and each xml consists of at least 5 elements. In the main table, I can only order them as they are, but I need to randomize the elements.

For example - jsfiddle.net/tiitremmel/qcSNz/1/

and the result of this table may look like a randomized td

+4
source share
2 answers
shuffle($("table")); function shuffle(tbl) { var arr = tbl.find("td"); for( var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x ); var tmp; var rows = tbl.find("tr").length var cols = tbl.find("tr:first td").length for (i = 0; i < rows; i++){ tmp = tbl.find("tr").eq(i); tmp.html() for (j = 0; j < cols; j++) tmp.append(arr[i*cols+j]); } } 

This is definitely not the best code, this is just an approach. But it does work. In your case you need to use

 shuffle($("table tbody")); 
+2
source

A shuffle plugin might help you. Here is the original post.

+1
source

Source: https://habr.com/ru/post/1342242/


All Articles