user1033882 user1033882 - 3 months ago 16x
jQuery Question

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

I have a page with many div boxes rendered using bootstrap 3. I want some of them to be sortable. Each div that i want to be sortable has a specific data- attribute (ex: data-sortable="box-1", data-sortable="box-2" etc)

Doing this though:

placeholder: "sort-highlight",
connectWith: ".connectedSortable",
handle: ".box-header, .nav-tabs",
forcePlaceholderSize: true,
zIndex: 999999,
stop: function(event, ui) {
var columns = [];

will add some other divs inside the array, because some static divs have their own mini-sortable items that i deal with elsewhere.
I want to somehow separate those, and insert only the divs with the specific data-sortable tag.

With this:

var divsWithTag = $(this).find('[data-sortable]');

i can get all the divs that i want. Is there a way i can tweak this line here


to push only the divs that match elements inside divsWithTag variable?


You can use filter, and considering the fact that toArray returns an array of id-s. Below, the .attr() value is coerced to a boolean:

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

Or use .is (as in the original comment) instead of the id match /originally thought the attribute-match collection would only be looked up once/