jQuery UI可排序,不使用自定义属性进行序列化 [英] jQuery UI sortable not serializing with custom attributes

查看:88
本文介绍了jQuery UI可排序,不使用自定义属性进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery UI与 data-id 属性进行排序。我知道您可以使用 sortable('serialize')使用 id =row_4之类的东西,这确实有效我需要这样做。

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'})给出一个空字符串

  • sortable('toArray'),{attribute:'data-id'})给出预期输出

  • 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.
    }
});

小提琴: http://jsfiddle.net/ogzw5pL2/

这笔交易是什么?我98%确定之前有效。

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

推荐答案

你需要 key 序列化,c>和,但您可以使用参数来覆盖默认行为并获得想要的结果。

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

在您的情况下,您可以设置属性表达式你想要它需要 data-id 并构建定义为键和适当的值。像这样:

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

            });

小提琴: http://jsfiddle.net/c2o3txry/

这篇关于jQuery UI可排序,不使用自定义属性进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆