随机化表格单元格 [英] Randomize table cells
本文介绍了随机化表格单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道它的声音有点连线,但有没有办法随机化表内容?正是细胞。
这个的目的 - 我有一个从几个xml生成的表,每个xml包含至少5个项目。在主表中我只能按原样订购,但我需要随机化项目。
I know its sounds bit wired, but is there any way to randomize table content? precisely cells. The purpose of this - I have a table generated from several xml's and each xml consists at least 5 items. In main table i can only order them as they are but i need to randomize the items.
例如 - jsfiddle.net/tiitremmel/qcSNz/1/
For example - jsfiddle.net/tiitremmel/qcSNz/1/
此表的结果可能看起来是随机的td's
and the result of this table could look randomized td's
推荐答案
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]);
}
}
当然,这不是最好的代码,这只是方法。但它的确有效。
在您的情况下,您需要使用
Definitely, this is not the best code, this is only approach. But it works. In your case you need to use
shuffle($("table tbody"));
这篇关于随机化表格单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文