user1033882 user1033882 - 1 year ago 194
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?

Answer Source

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/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download