0

I got this table and I need to sort columns by an attribute (kind_id) but not simply ascendant or descendant, I got an array of ids that the sorting should follow.

This is an example of column:

<tr id='music' class='unchecked' selezionato='no'  kind_id='203'>
    <td id='music' width='179px'>
       <div id='nome_music'>
           Rock
       </div>
    </td>
</tr>  

And this is an example of the array

["203", "10", "12", "15", "25", "46", "56", "61", "102", "104", "114", "116", "121", "138", "142", "145", "187", "189", "190", "204", "205", "208", "214", "220"] 

How could I do this?

4

2 回答 2

1

您可以使用像 underscore.js 这样的库并利用内置方法sortBy

underscore.js 排序方式

于 2013-07-22T17:57:42.890 回答
0

您可以使用排序功能。假设您的表格元素有一个 id ("myTable") 并且您的数组变量被称为arrayOfIndices

function sortById (row1, row2){
    return arrayOfIndices.indexOf($(row1).attr("kind_id")) > arrayOfIndices.indexOf($(row2).attr("kind_id")) ? 1 : -1; 
}

$("#myTable tr").sort(sortById).appendTo("#myTable");
于 2013-07-22T18:00:16.353 回答