如何在ui-sortable中手动触发'update'

Jor*_*ing 3 jquery jquery-ui jquery-ui-sortable

我正在使用一个UI,可以在每个项目中按一个delete按钮进行排序.这是删除功能:

$('.delete_item').click(function(){
    $(this).closest('.grid_3_b').remove();
    initSortable();
    $(".sortable").sortable('refresh').trigger('update');
});
Run Code Online (Sandbox Code Playgroud)

div获取的去除,我想,但没有update发送到PHP ..所以我的脚本将无法保存订单和已删除项目的数据..

这是我的initSortable();功能:

function initSortable() {
    $( ".sortable" ).sortable({
        items: '.grid_3_b, .dropable',
        connectWith: ".sortable",
        placeholder: "placeholder",
        remove: function(event, ui) {
            if(!$('div', this).length) {
                $(this).next('.dropable').remove();
                $(this).remove();
            }
            initMenu();
        },
        receive: function(event, ui) {
            if( $(this).hasClass( "dropable" ) ) {
                if( $(this).hasClass( "gallery__item--active" ) ) {
                    $(this).before( "<div class=\"dropable gallery__item sortable\"></div>" );
                    $(this).after( "<div class=\"dropable gallery__item sortable\"></div>" );

                    initSortable();
                    $(".sortable").sortable('refresh').trigger('update');
                    initMenu();
                }
            }
        },
        update : function () {
            var neworder = new Array();
            $('.sortable').each(function() {
                var id  = $(this).attr("id");
                var pusharray = new Array();
                $('#' + id).children('div').each(function () {
                    var art = $(this).attr("data-art");
                    var pos = $(this).attr("data-pos");
                    pusharray.push( {data:{'art':art, 'pos':pos}} );
                });
                neworder.push({'id':id, 'articles':pusharray});
            });

            $.post("example.php",{'neworder': neworder},function(data){});
            initMenu();
        }
    }).disableSelection();
}

initSortable();
Run Code Online (Sandbox Code Playgroud)

此外,该remove函数通常在列为空时删除列,但在删除列中的最新项时不起作用.这是因为未调用更新触发器吗?

T J*_*T J 11

要在jquery-ui sortable中手动触发事件,而不是在options对象中指定处理程序,则需要在可排序初始化之后绑定事件处理程序.

例如,以下内容不起作用

$('ul').sortable({
  update: function () {
    console.log('update called');
  }
});
$('ul').trigger('sortupdate'); // doesn't work
Run Code Online (Sandbox Code Playgroud)

以下作品

$('ul').sortable();
$('ul').on('sortupdate',function(){
   console.log('update called');
});
$('ul').trigger('sortupdate'); // logs update called.
Run Code Online (Sandbox Code Playgroud)

演示

  • 您真棒,感谢您为回答这个老问题付出的努力! (2认同)