I have a jQuery UI Sortable list element that is populated dynamically from an Ajax request.
Currently, the workflow goes
- User clicks button, list is populated and sorted by specified settings.
- User clicks another button,
- Existing list
<li>
elements are erased byjQuery.empty()
call - New data values are inserted into new
<li>
list elements and appended to<ul>
list - Sortable list is refreshed via $("#sortable").sortable("refresh");
- Existing list
Given a Sortable list object $("#avail_list").sortable( ... );
, I want to have specific list elements' draggable
property disabled and opacity set to 0.5 based on existing values in other elements.
To do this, I have the following function:
var disabled = []; var appendString = "" if (avail.length > 0) { for (var i = 0; i < avail.length; i++) { //check if current list element exists in existing value list. True results //in grayed out and non-draggable element compareMatch = checkMatch(avail[i], compare); if (compareMatch) disabled.push(list + "open_" + avail[i].id); appendString += "<li id = "+ list + "open_" + avail[i].id + " class = 'avail_list_element'><img class = 'logo' src = /static/images/vendor_logo/" + avail[i].icon + " /></li>" } $("#avail_list").append(appendString); } $("#avail_list").sortable("refresh"); if (disabled.length > 0) disableDraggable(disabled); function disableDraggable(elements){ for (var i = 0; i < elements.length; i++) { console.log(elements[i]) $("#" + elements[i]).sortable("disable"); $("#" + elements[i]).fadeTo("fast", 0.5); } }
However, this results in an error
Error: cannot call methods on sortable prior to initialization; attempted to call method 'disable'
Since I called refresh
on the sortable list prior to disabling the elements, how can the sortable
object not be initialized?