1

I'm trying drag and drop with Knockout JS, but can't make it work. Any suggestions of why?

<td data-bind="event: {ondrop: $root.HandleDrop}">
burktelefon
  • 988
  • 2
  • 8
  • 27

1 Answers1

2

As mentioned in comments you have to be creating custom-binding handlers to achieve this

View Model:

(function() {
    var _dragged;
    ko.bindingHandlers.drag = {
        init: function(element, valueAccessor) {
            var dragElement = $(element);
            var dragOptions = {
                helper: 'clone',
                revert: true,
                revertDuration: 0,
                start: function() {
                    _dragged = ko.utils.unwrapObservable(valueAccessor().value);
                },
                cursor: 'default'
            };
            dragElement.draggable(dragOptions).disableSelection();
        }
    };

    ko.bindingHandlers.drop = {
        init: function(element, valueAccessor) {
            var dropElement = $(element);
            var dropOptions = {
                drop: function(event, ui) {
                    valueAccessor().value(_dragged);
                }
            };
            dropElement.droppable(dropOptions);
        }
    };
})();
var ViewModel = function () {
    var self = this;
    self.user = ko.observableArray([{name:'jhon'},{name:'charlie'},{name:'jack'}]);
    self.movedArray=ko.observableArray()
    self.latestone = ko.computed({
        read: function() {
            return self.movedArray().length ? self.movedArray()[0] : "";
        },
        write: function(value) {
            self.movedArray.unshift(value); // adds as first element in array 
        },
        owner: self
    });
};

$(function() {
    ko.applyBindings(new ViewModel());
});

View :

<ul id="dragbox" data-bind="foreach:user">
    <li data-bind="text:$data.name,drag:{value:$data.name}"></li>
</ul>

<hr/>
<div id="dropbox" data-bind="drop: {value: latestone}">
        <div data-bind="visible:(!!latestone())">
            <p >Thanks for adding <span data-bind="text:latestone"></span>!</p>
            <span>So far, you've added:</span>
            <ul data-bind="foreach: movedArray">
                <li data-bind="text: $data"></li>
            </ul>
        </div>
    </div>
<hr/>

Working fiddle here

Credits to wilsonhut i just forked his fiddle to make this .

super cool
  • 6,003
  • 2
  • 30
  • 63
  • In the documentation I mentioned above it says: "This can be used to bind to any event, such as keypress, mouseover or mouseout." Why would I have to create a custom binding handler? – burktelefon Feb 25 '15 at 12:57
  • True , can't agree more .its a personal `Choice` i feel its a better practice using binding handlers which gives more control on what you do (i.e complex logic) please do refer docs for it just in case . – super cool Feb 25 '15 at 13:07
  • Now I tried this: , where HandleUnwantedEvents does event.preventDefault for now. HandleDrop is unchanged, and I'm actually reaching it! – burktelefon Feb 25 '15 at 13:23
  • good to hear . this post can be helpful (events in jquery) http://stackoverflow.com/questions/6199890/jquery-droppable-receiving-events-during-drag-over-not-just-on-initial-drag-o . cheers – super cool Feb 25 '15 at 15:34