2013-05-05 44 views
0

我已经看过很多jQuery排序解决方案,但还没有找到我在找什么。排序表

我试图理清其中有需要进行排序的行和列的表格:http://jsfiddle.net/rvezE/

<table> 
<tr> 
    <td>3</td> 
    <td>5</td> 
    <td>7</td> 
</tr> 
<tr> 
    <td>1</td> 
    <td>2</td> 
    <td>4</td> 
</tr> 
<tr> 
    <td>3</td> 
    <td>8</td> 
    <td>9</td> 
</tr> 
</table> 

我需要在表中从左至右,从上到下进行排序。但到目前为止,我发现的所有插件只能按行排序。

我已经尝试了一些简单的解决方案,将所有TD排除在外,整理并重新添加它们,但对于大量(大约100个),性能非常糟糕,对于平板电脑也需要相当快速。

任何人都遇到过这样的事情?

+0

排序对待每行作为一个列表,每个列表添加到一个数组,然后排序。 – GriffLab 2013-05-05 10:10:53

回答

2

试试这个:

/*remove rows from DOM, loop over each row within jQuery object to sort cells*/ 
var $rows=$('tr').detach().each(function(){ 
    /* sort cells of this row*/ 
    var cells=$(this).find('td').sort(function(a,b){ 
     return $(a).text() > $(b).text(); 
    }); 
    /* update sorted html of this row*/ 
    $(this).html(cells) 

}) 
/* now sort the rows*/ 
.sort(function(a,b){ 
    return $(a).find('td').first().text() > $(b).find('td').first().text(); 

}); 
/* update table with sorted rows html*/ 
$('table').html($rows); 

DEMO

假设你只想要第一列

+0

在JSfiddle中,它并没有真正提出正确的答案:)但是我会用它更多一点,谢谢。 – YesMan85 2013-05-05 18:50:56

+0

你是什么意思,它是不正确的答案? – charlietfl 2013-05-05 19:50:24

+0

我希望从左上角到右下角的1,2,3 ... 9的结果。 – YesMan85 2013-05-05 21:53:23