I have a problem since there is a jQuery closest() function, which is not what I mean. (Note, I kinda kept typing as I went asking this question, what I would love is suggestions on how to solve this the best way)
I have some objects in a table, ordered by a "priority" number. This priority is an arbitrary number between 1 - 1024 and not bound to the id of an object. The row "id" displayed in the following table is the id of an object, as it is in the database.
<table>
<tr id="5" priority="1"> .. </tr>
<tr id="2" priority="10"> .. </tr>
<tr id="7" priority="12"> .. </tr>
<tr id="8" priority="13"> .. </tr>
</table>
Now I can change the priority with an ajax request. At the moment, to display the resulting order the user would have to do a page reload (which kinda defeats the purpose of ajax). What I would like is to move the selected row to its new location, using before()
or after()
functionality.
So I want to move element 7 before element 2. So I set its priority to something between 1 and 9. Let's say I choose priority 5. Now I need to find the correct spot. As a simple solution: Start from the beginning of the list and move up till a "nice" spot is found. Which, looks like a sorting problem, but with only one insert? Is there some function or functionality in jQuery to do this in one insert?
EDIT
This works:
var the_row_I_need = $('table').filter(function() {
return parseInt($(this).attr('priority')) > parseInt(value)
}).first();
the_row_I_need.before( row_to_move );
But it still uses quite a bit of filtering, so I think it still goes over the entire list. Is there a better way than this or using sort()?