jquery sortable('toArray') 仅当元素属于另一个数组时

jquery sortable('toArray') only if element belongs in another array

本文关键字:属于 元素 另一个 数组 sortable toArray jquery      更新时间:2023-09-26

我有一个页面,其中包含许多使用 bootstrap 3 渲染的div 框。我希望其中一些是可排序的。我想要可排序的每个div 都有一个特定的 data- 属性(例如:data-sortable="box-1",data-sortable="box-2" 等)

不过这样做:

$(".connectedSortable").sortable({
    placeholder: "sort-highlight",
    connectWith: ".connectedSortable",
    handle: ".box-header, .nav-tabs",
    forcePlaceholderSize: true,
    zIndex: 999999,
    stop: function(event, ui) {
     var columns = [];
        $(".ui-sortable").each(function(){
            columns.push($(this).sortable('toArray').join(','));
        });
    }
});

将在数组中添加一些其他div,因为一些静态div 有自己的迷你可排序项目,我在其他地方处理过。我想以某种方式将它们分开,并仅插入具有特定数据可排序标签的div。

有了这个:

$(".row").each(function(){
   var divsWithTag = $(this).find('[data-sortable]');
}

我可以得到我想要的所有div。有没有办法我可以在这里调整这条线

columns.push($(this).sortable('toArray').join(','));

只推送与divsWithTag 变量中的元素匹配的div?

你可以

使用 filter ,并考虑到 toArray 返回一个 id-s 数组的事实。下面,.attr()值被强制转换为布尔值:

finalColumns.push($(this).sortable('toArray').filter(function(id) {
    return $('#'+id).attr('data-sortable');
}).join(','));

或者使用 .is(如原始注释)代替 id 匹配/最初认为属性匹配集合只会被查找一次/

http://jsfiddle.net/h8h9u0k7/3/