4

I have found great solution (last answer):

jQuery Sortable - Select and Drag Multiple List Items

http://jsfiddle.net/hQnWG/480/

HTML:

<ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
</ul>
<ul>
    <li>Four</li>
    <li>Five</li>
    <li>Six</li>
</ul>

JavaScript (with jQuery and jQuery UI):

$("ul").on('click', 'li', function (e) {
    if (e.ctrlKey || e.metaKey) {
        $(this).toggleClass("selected");
    } else {
        $(this).addClass("selected").siblings().removeClass('selected');
    }
}).sortable({
    connectWith: "ul",
    delay: 150, //Needed to prevent accidental drag when trying to select
    revert: 0,
    helper: function (e, item) {
        var helper = $('<li/>');
        if (!item.hasClass('selected')) {
            item.addClass('selected').siblings().removeClass('selected');
        }
        var elements = item.parent().children('.selected').clone();
        item.data('multidrag', elements).siblings('.selected').remove();
        return helper.append(elements);
    },
    stop: function (e, info) {
        info.item.after(info.item.data('multidrag')).remove();
    }

});

It works great but when I'm dragging two or more elements JS console in Google Chrome shows this error:

Uncaught TypeError: Cannot call method 'insertBefore' of null

How do I fix it?

Community
  • 1
  • 1
tere
  • 41
  • 1
  • 2
  • No errors on Safari `6.0.3` and OS X `10.8.3`. Also I don't see `insertBefore()` in your code? What the problem is that a jQuery selector probably failed `var test = $('#myNoneExcistingElement');` and `console.log(test);` will print `null` or `undefined`. –  Mar 26 '13 at 12:20
  • No errors in `25.0` chrome – David Chase Mar 26 '13 at 12:26
  • @DavidChase because the `.insertBefore()` method is absent in the example ;) Or it is either used as substitute in another method, but can't confirm that for sure. –  Mar 26 '13 at 12:27
  • @Allendar i see that, i just wanted to share my test :) – David Chase Mar 26 '13 at 12:28
  • Errors stil show up (newest Chrome). Just select two elements from first list (one and two) and drag them **inside** only this list. After 2 or 3 actions u will get error. I count over 10 errors in a few seconds. – tere Mar 26 '13 at 14:04
  • Happens with me too. Everything seems to be working though, but this error is annoying... – waterplea Oct 07 '14 at 07:27

1 Answers1

4

I think its a bug in jquery-UI _rearrange function ... so i did a hack which overrides that method... Insert the below code after dom ready ...that will fix the issue

$.ui.sortable.prototype._rearrange = function (event, i, a, hardRefresh) {

                a ? a[0].appendChild(this.placeholder[0]) : (i.item[0].parentNode) ? i.item[0].parentNode.insertBefore(this.placeholder[0], (this.direction === "down" ? i.item[0] : i.item[0].nextSibling)) : i.item[0];
                this.counter = this.counter ? ++this.counter : 1;
                var counter = this.counter;

                this._delay(function () {
                    if (counter === this.counter) {
                        this.refreshPositions(!hardRefresh); //Precompute after each DOM insertion, NOT on mousemove
                    }
                });

            }

here is the fiddle

http://jsfiddle.net/r83zY/

Apo
  • 41
  • 6