Reputation: 169
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?
Upvotes: 0
Views: 121
Reputation: 526
You can use the sort function. Assuming your table element has an id ("myTable") and that your array variable is called 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");
Upvotes: 0
Reputation: 666
You could use a library like underscore.js and utilize the built in method sortBy
.
Upvotes: 1