Note that there are some explanatory texts on larger screens.

plurals
  1. POFind the approximate "closest" row element, based on a priority
    text
    copied!<p>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)</p> <p>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.</p> <pre><code>&lt;table&gt; &lt;tr id="5" priority="1"&gt; .. &lt;/tr&gt; &lt;tr id="2" priority="10"&gt; .. &lt;/tr&gt; &lt;tr id="7" priority="12"&gt; .. &lt;/tr&gt; &lt;tr id="8" priority="13"&gt; .. &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>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 <code>before()</code> or <code>after()</code> functionality.</p> <p>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?</p> <p><strong>EDIT</strong></p> <p>This works:</p> <pre><code>var the_row_I_need = $('table').filter(function() { return parseInt($(this).attr('priority')) &gt; parseInt(value) }).first(); the_row_I_need.before( row_to_move ); </code></pre> <p>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()?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload