rniemeyer / knockout-sortable

A Knockout.js binding to connect observableArrays with jQuery UI sortable functionality
MIT License
547 stars 128 forks source link

Sorting is creating duplicate items #151

Open wellso opened 8 years ago

wellso commented 8 years ago

I'm trying to sortable working with dynamic templates, but after I re-order the items, duplicates keep being introduced. Could you shed any light on where I'm going wrong or if its a bug? Thanks.

http://jsfiddle.net/cqsv1fn3/

wellso commented 8 years ago

83 seems to fix the problem i was having

rniemeyer commented 8 years ago

I can't quite tell from your fiddle, as it doesn't seem runnable, but it is likely the same as this: https://github.com/rniemeyer/knockout-sortable/issues/150 . Make sure that you strip whitespace / text nodes from the beginning and the end inside your script tags. There is code to automatically strip this whitespace from templates, but it doesn't kick in on dynamic templates.

abradley2 commented 8 years ago

I had a similar issue, resolved by adding this as the afterMove hook function

this.afterMove = function( model, event, jqui ) {
  $(jqui.item[0]).remove();
};

http://jsfiddle.net/dnk43atp/

Learned the fix from this: https://github.com/rniemeyer/knockout-sortable/pull/118

pawel-zolty commented 5 years ago

2018 solution: The problem is that we have to use ul and li DOM objects to use knockout-sortable. It is not possible to use e.g. div. With my code:

<div data-bind="sortable: { data: availablePictures, beforeMove : beforeMove, afterMove: afterMove}, connectClass:false" class="..." id="picturesList">            
                <div> 
   content

I have objects copied and removed randomly.

When I change to:

<ul style="overflow:hidden; list-style-type:none" data-bind="sortable: { data: availablePictures, beforeMove : beforeMove, afterMove: afterMove}, connectClass:false" class="static-height-container" id="picturesList">
            <li style="float:left; display:block">
                <div>
content

it works properly