I'm sorting a table using jQuery, roughly following the code found here. The code sketch is as follows:
$('.sort-table').click(function(e) {
// cache objects
$table = $('#sort-table'), // cache the target table DOM element
$rows = $('tbody > tr', $table); // cache rows from target table body
// sort items
$rows.sort(<my_predicate_function(a,b){...}>);
// assign to table - what is going on?
$rows.each(function(index, row){
$table.append(row); // <-- how come $table remains the same size?
});
});
While the code works fine, I'm puzzeled by the code that appends the rows back to the table, which simply iterates over the sorted rows, appending each at the end of $table
.
At no stage did we emtpy $table
from its previous children.
- Since
$table
was never emptied, how come$table
remains the same size? - Does
append()
also enforces uniquness in the target container?