3

I'm using jQuery UI sortable with data-id attributes. I know you can use sortable('serialize') with something like id="row_4" and this does work for me but I need to do it this way.

  • sortable('serialize', {attribute: 'data-id'}) gives an empty string
  • sortable('toArray'), {attribute: 'data-id'}) gives expected output
<div data-sortable="link/to/handler">
    <div data-id="1">1</div>
    <div data-id="2">2</div>
    <div data-id="3">3</div>
    <div data-id="4">4</div>
    <div data-id="5">5</div>
</div>
var container = $('[data-sortable]');
container.sortable({
    items : "> [data-id]",
    update : function() {
        var postData = container.sortable('serialize', {
            attribute: 'data-id'
        });
        alert(postData); // Nothing
        var postData2 = container.sortable('toArray', {
            attribute: 'data-id'
        });
        alert(postData2); // 1,3,4,5,2 etc.
    }
});

Fiddle: http://jsfiddle.net/ogzw5pL2/

What's the deal? I'm 98% certain this was working before.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • I can y ou pleas aadd the code in the ventialator for the demo?IBM try card punches or AssembleyQuery like jQueru for Assembly and binary for more speed thnaks and hope is benefits well – stormdrain Nov 13 '17 at 16:52
  • @stormdrain its is passing nmp and bowser test currantly but i try to compile for LAMP stack and get `error 72 the file you specified is inspecified plese restat your brower` so i think it is asm error? can u link to the gist or send friend request on codepan? i have thousands dollars of cliants with many subscriebrs sir who relying on this – Wesley Murch Nov 13 '17 at 17:05

2 Answers2

6

You need key and value for serialize, but you can play with parameters to override default behavior and get wanted results.

In your case you can set the attribute, key and expression you want so that it takes the data-id and build the string with defined key and proper value. Like this:

           var postData = container.sortable('serialize', {
                attribute: 'data-id',//this will look up this attribute
                key: 'order',//this manually sets the key
                expression: /(.+)/ //expression is a RegExp allowing to determine how to split the data in key-value. In your case it's just the value

            });

Fiddle: http://jsfiddle.net/c2o3txry/

Julien Grégoire
  • 16,864
  • 4
  • 32
  • 57
  • Thanks for the workaround, this will do. I can't change all the templates but I have no problem adjusting one javascript file. – Wesley Murch Aug 05 '15 at 18:29
1

The "serialize" method just does not work if you don't have id values of the form "something-n". (You can use _ or = instead of -.)

The idea is to give you a query string that's got the "something" as the parameter name and the values after the - as the values.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Thanks, I wrote this a while back and I guess never tested it. Not sure what I was thinking, I guess I assumed it would go `0=firstvalue&1=secondvalue` etc. – Wesley Murch Aug 05 '15 at 18:30