简体   繁体   English

JQueryUI可排序问题:序列化报告错误的顺序

[英]Problem with JQueryUI Sortable: serialize reporting the wrong order

I'm using sortable to implement a one-dimensional list of widgets. 我正在使用sortable来实现一维的小部件列表。 It's working alright, but when I call serialize to send the current order back to the server, the wrong order is generated. 它工作正常,但是当我调用serialize将当前订单发送回服务器时,会生成错误的订单。

Here's my HTML, note the ordering of widget IDs: 13, 10, 11: 这是我的HTML,请注意小部件ID的排序:13,10,11:

<div id="widget_columns">
    <ul id="column1" class="widget-column grid_8 alpha ui-sortable">
        <li id="widget_13" class="widget">
        (a widget!)
        </li>

        <li id="widget_10" class="widget">
        (a widget!)
        </li>

        <li id="widget_11" class="widget">
        (a widget!)
        </li>
    </ul>
</div>

The list is initialized with 该列表初始化为

    $(#widget_columns').sortable({
        connectWith: $(#widget_columns'),
        handle: settings.handleSelector,
        placeholder: 'widget-placeholder',
        forcePlaceholderSize: true,
        revert: 300,
        delay: 100,
        opacity: 0.8,
        containment: 'document',
        start: function (e, ui) {
            $(ui.helper).addClass('dragging');
        },
        stop: function (e, ui) {
            $(ui.item).css({ width: '' }).removeClass('dragging');
            $(settings.columns).sortable('enable');
        }
    });

However, when I then call 但是,当我打电话的时候

alert($('#widget_columns *').sortable('serialize'));

to find out the widget order, I get the correct IDs, but in the wrong order, 10, 11, 13: 找出小部件顺序,我得到正确的ID,但顺序错误,10,11,13:

widget[]=10&widget[]=11&widget[]=13

Any idea why this might be? 知道为什么会这样吗?

You are applying the sortable method on the parent div and not the ul I'm not sure why, also when calling serialize you are not calling it on the previous sortable object! 你正在父div上应用sortable方法而不是ul我不知道为什么,同样在调用serialize你没有在之前的可排序对象上调用它!

Here is a small working example : 这是一个小工作 示例

$('#column1').sortable({
    //connectWith: $('#widget_columns'),
    revert: 300,
    delay: 100,
    opacity: 0.8,
    containment: 'document',
    start: function(e, ui) {
        //$(ui.helper).addClass('dragging');
    },
    stop: function(e, ui) {
        //$(ui.item).css({ width: '' }).removeClass('dragging');
        //$(settings.columns).sortable('enable');
        alert($(this).sortable('serialize'))
    }
});

To answer my own question, it has something to do with the fact that my solution disable s the sortable whenever dragging ends, and enable s it when dragging starts, a concept I unthinkingly took from some tutorial. 要回答我自己的问题,它与我的解决方案在拖动结束时disable可排序的事实有关,并且在拖动开始时enable它,这是我从一些教程中不假思索的概念。 When I removed the code disabling and re-enabling the sortable ( $(settings.columns).sortable('disable'); ), the serialization worked fine. 当我删除代码禁用并重新启用sortable( $(settings.columns).sortable('disable'); )时,序列化工作正常。

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

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