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

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

我有一个页面,其中包含许多 div 使用 bootstrap 呈现的框 3. 我希望其中一些可以排序。我想要排序的每个 div 都有一个特定的数据属性(例如: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(','));
        });
    }
});

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

有了这个:

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

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

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

仅推送与 divsWithTag 变量内的元素匹配的 divs?

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

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

或者用.is(如原评论)代替id匹配/本来以为属性匹配集合只会查一次/

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