Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Sortable: How do I prevent a specific list item from being moved?

I have this list:

<ul>
    <li class="line"><a href="#" class="drag">header (do not sort)</a></li>
    <li class="line"><a href="#" class="drag">line one</a></li>
    <li class="line"><a href="#" class="drag">line two</a></li>
</ul>

How can I prevent the first li from being moved?

like image 475
kmunky Avatar asked Jan 05 '10 13:01

kmunky


2 Answers

You can specify which items within the Sortable are actually sortable. This should do the trick—change the HTML as follows:

<ul>
    <li class="line" id="header"><a href="#" class="drag">header (do not sort)</a></li>
    <li class="line"><a href="#" class="drag">line one</a></li>
    <li class="line"><a href="#" class="drag">line two</a></li>
</ul>

Then when you set up the sortable:

$('ul').sortable({ items: 'li[id!=header]' });

Hope this helps!

like image 78
Mark Bell Avatar answered Nov 02 '22 15:11

Mark Bell


You can explicitly exclude items (aside by not including them):

$( ".sortable" ).sortable({
    cancel: ".disable-sort-item"
});
like image 6
Mladen Janjetovic Avatar answered Nov 02 '22 14:11

Mladen Janjetovic