Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
47
rated 0 times [  54] [ 7]  / answers: 1 / hits: 17043  / 10 Years ago, fri, march 21, 2014, 12:00:00

Alright so I have a nested sortable list, each item is therefore both a container and a sortable element.



The problem I am facing is that, whenever I add a new element, I want jQuery to refresh its internal state with the new item.
According to the documentation, one has to call the sortable method passing as parameter 'refresh', but still I can't make it work.



Sample code:
http://jsfiddle.net/X5sBm/



JavaScript:



$(document).ready(function() {
var list = $('#mycontainer ul').sortable({
connectWith: '#mycontainer ul',
placeholder: 'myplaceholder'
});

function addElement(text) {
$('#mycontainer > ul').append('<li>' + text + '<ul></ul></li>');
list.sortable('refresh');
}

addElement('yolo');
});


HTML:



<div id=mycontainer>
<ul>
<li>
Some text
<ul>
</ul>
</li>
<li>
Some text 2
<ul>
</ul>
</li>
</ul>
</div>


CSS:



#mycontainer > ul {
display: block;
}

#mycontainer > ul ul {
min-height: 10px;
padding-left: 20px;
}

.myplaceholder {
background-color: yellow;
}


Try to drag one pre existing item under the newly added one, you won't be able to do so even after the refresh.


More From » jquery

 Answers
112

I found a cheap fix:



I call sortable again on the same tag reinitialising the Sortable plugin like so:



$('#mycontainer ul').sortable({
connectWith: '#mycontainer ul',
placeholder: 'myplaceholder'
});


and it works.


[#71865] Thursday, March 20, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zariahdiamondz

Total Points: 649
Total Questions: 109
Total Answers: 88

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;