0

Why does Jquery UI Sortable toArray method give back array like this (in Firebug):

ordering[] = 1
ordering[] = 2
ordering[] = 3

And when i use custom function to ignore hidden items, it gives back a string, like this:

ordering = 1,2,3

I've checked plenty of times, it seems there's no difference between original Sortable toArray function code:

toArray: function(o) {
    var items = this._getItemsAsjQuery(o && o.connected),
        ret = [];
    o = o || {};
    items.each(function() { ret.push($(o.item || this).attr(o.attribute || "id") || ""); });
    return ret;
}

and my custom function:

function(){ // Do not pass hidden clones
    var items = [];
    $('#fp_parameters_list').children().each(function(){
        if ($(this).is(':visible')) items.push($(this).attr('data-parameter-id'));
    });
    return items;
}

except that my doesn't counter hidden fields.

Thanks, if someone can help.

Alex
  • 731
  • 1
  • 7
  • 8
  • 1
    What are you trying to do? – zxqx Mar 28 '14 at 22:15
  • Maybe a duplicate of [jquery .is(“:visible”) not working in Chrome](http://stackoverflow.com/questions/8337186/jquery-isvisible-not-working-in-chrome)? – David Mann Mar 29 '14 at 07:18
  • I need to have order of sortable items sent by ajax, and i used toArray method before i needed to exclude hidden items from ordering. So i had to make custom function and it works fine, returning correct values, only problem is what's described in question: it returns it as a string, while toArray was returning array of ids. And it's strange as the functions are pretty much the same. – Alex Mar 29 '14 at 07:27

2 Answers2

1

Seems to work for me:

HTML:

<div id="fp_parameters_list">
    <div data-parameter-id="3"></div>
    <div data-parameter-id="2"></div>
    <div data-parameter-id="10" style="display:none"></div>
    <div data-parameter-id="1"></div>
</div>

JQuery:

function mySort(){ // Do not pass hidden clones
    var items = [];
    $('#fp_parameters_list').children().each(function(){
        if ($(this).is(':visible')) {         
            items.push($(this).attr('data-parameter-id'));
        }
    });
    return items;
}

console.log(mySort()[0]);

Here's the fiddle.

David Mann
  • 431
  • 2
  • 7
  • Thanks for fiddle. Yes, it is working, but when i assign this function result to ajax data variable, it is transformed into a string of ids separated by comma. – Alex Mar 29 '14 at 07:51
  • While trying to show what i need, i found a strange thing: the result is passed as an array if function is declared outside $.ajax function, and it becomes string if i put it inside $.ajax. You can check the fiddle and compare how 2 requests are sending ordering in their post data. – Alex Mar 29 '14 at 08:38
0

As answered in other topic, i had to invoke the function to get correct result. Here's the topic: Function result array inside $.ajax is converted to string

There's also one less code solution.

Community
  • 1
  • 1
Alex
  • 731
  • 1
  • 7
  • 8