Say I have this html:
<table>
<tr>
<th>Sort Order</th>
<th>Name</th>
</tr>
<tr class="item" id="item_1">
<td class="sortorder">1</td>
<td>ABC</td>
</tr>
<tr class="item" id="item_2">
<td class="sortorder">2</td>
<td>DEF</td>
</tr>
<tr class="item" id="item_3">
<td class="sortorder">3</td>
<td>XYZ</td>
</tr>
</table>
What I want is for a user to be able to sort the rows in the table (excluding the first row with the <th>s, so all rows with the class .item) by dragging and dropping them, and then update their sort orders.
E.g if user drags DEF to first position and ABC replaces it, I want DEF’s sort order to say 1, and ABC’s to say 2 instead.
How can this be done?
If needed, I can have all the items stored seperately in a javascript array or object so I can loop over them and update each item’s sort order whenever the user changes the sort order.
You can use jQuery UI’s sortable list. Its well supported across browsers and has many options and callbacks. They use a list in their example but you could just as easily use a table.
jQuery UI Sortable Documentation – http://jqueryui.com/demos/sortable/
Sortable tutorial/guide – http://www.petefreitag.com/item/736.cfm
Example: