如何在ui-sortable中手动触发’update’
我正在使用一个UI可以在每个项目中排序一个delete
按钮。 这是删除function:
$('.delete_item').click(function(){ $(this).closest('.grid_3_b').remove(); initSortable(); $(".sortable").sortable('refresh').trigger('update'); });
我想删除div
get,但是没有发送给PHP的update
数据..所以我的脚本不会保存订单和删除的项目..
这是我的initSortable();
function:
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( "" ); $(this).after( "" ); 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();
此外, remove
函数通常会在列为空时删除列,但在删除列中的最新项时不起作用。这是因为未调用更新触发器吗?
要在jquery-ui sortable中手动触发事件,而不是在options对象中指定处理程序,则需要在可排序初始化之后绑定事件处理程序。
例如,以下内容不起作用
$('ul').sortable({ update: function () { console.log('update called'); } }); $('ul').trigger('sortupdate'); // doesn't work
以下工作
$('ul').sortable(); $('ul').on('sortupdate',function(){ console.log('update called'); }); $('ul').trigger('sortupdate'); // logs update called.
演示