Skip to content
Advertisement

jQuery dynamic sortables with nested sortable items

Dynamic Sortable

Codepen Version

Description


I have a problem with a dynamic sortable list, which elements should also be sortable. The problem is, that each element in the list is dynamic, as it will be cloned from a template div and appended to document.

Issue


Currently the blocks (.pb-row) are sortable just as expected.
But the nested sortable for the dynamically added widgets (.builder-row-content) doesn`t work.

Only nested nodes in the first block will be draggable and are very buggy. Also i can drag them outside of the row.

Nodes from additionally added Blocks are not draggable at all.

Also i receive this message in the console

cannot call methods on sortable prior to initialization; attempted to call method ‘refresh’

Code


Html to run the sortable:

JavaScript

Attempt for jQuery sortable list:

JavaScript

Attempted solution which doesnt work:

JavaScript

Screenshot


Screenshot of implementation

Advertisement

Answer

You need to reinitialize sortable onto dynamically added elements, so:

Wrap your sortable events in a function

JavaScript

Then call the function in pbCreateNode.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement