简体   繁体   English

JQuery嵌套可排序的chrome问题

[英]JQuery nested sortable chrome issues

I've been developing a file-tree like widget using Johnny's extended jquery sortable , which handles nested sortables better than standard JQuery. 我一直在使用Johnny的扩展jquery sortable开发一个类似文件树的小部件,它比标准的JQuery更好地处理嵌套的sortable。

I have an issue with reinitializing the tree with new data. 我有一个问题,用新数据重新初始化树。 There is no problem in firefox, but chrome acts up. 在Firefox中没有问题,但Chrome会起作用。 A minimal example can be seen here (or on jsfiddle here ) 这里可以看到一个最小的例子 (或者在这里的jsfiddle

In the onDrop callback, the tree is re-initialized. 在onDrop回调中,树被重新初始化。 For brevity, all that happens is a console.log , but in my actual example, data is being POSTed to the server via ajax, and the response has new data to update the tree with. 为简洁起见,所有发生的事情都是console.log ,但在我的实际示例中,数据通过ajax发布到服务器,并且响应具有用于更新树的新数据。

So, Firefox is happy with this solution, but in chrome, once I drag-and-drop once, and the tree is re-initialized, the next drag will fail with Uncaught TypeError: Cannot read property 'group' of undefined 所以,Firefox很满意这个解决方案,但是在chrome中,一旦我拖放一次,并且树被重新初始化,下一次拖动将失败, Uncaught TypeError: Cannot read property 'group' of undefined

  • ✓ Firefox ✓火狐
  • ✓ Internet explorer ✓Internetexplorer
  • ✕ Chrome ✕铬

If you will destroy the sortable every time before you init that element it will work: 如果您在每次启动该元素之前都会销毁sortable ,它将起作用:

function init(e) {
    // First of all - we destroy the sortable
    $('ul').sortable('destroy');

    var root = $('<ul></ul>')
    createNodes(root)
    e.html(root)
    root.sortable({
        group: 'foo',
        onDrop: function ($item, container, _super, event) {
            // These two lines are default behaviour of the plugin
            $item.removeClass(container.group.options.draggedClass).removeAttr("style");
            $("body").removeClass(container.group.options.bodyClass);

            console.log('Updating')
            init(e)
        }
    })
}

Working snippet : 工作片段

 function createNodes(e) { var foo = $('<li>Foo<ul></ul></li>'); var bar = $('<li>Bar<ul></ul></li>'); var baz = $('<li>Baz<ul></ul></li>'); bar.find('ul').append(baz); e.append(foo, bar); } function init(e) { // First of all - we destroy the sortable $('ul').sortable('destroy'); var root = $('<ul></ul>') createNodes(root) e.html(root) root.sortable({ group: 'foo', onDrop: function ($item, container, _super, event) { // These two lines are default behaviour of the plugin $item.removeClass(container.group.options.draggedClass).removeAttr("style"); $("body").removeClass(container.group.options.bodyClass); console.log('Updating') init(e) } }) } $(document).ready(function(){ init($('#myroot')) }) 
 <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//johnny.github.io/jquery-sortable/js/jquery-sortable.js"></script> <div id="myroot"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM