Jquery sortable('serialize')

2020-05-19 02:52发布

Is it possible to get the serialized list of items from a UL in jquery by calling the serialize method directly instead of using a callback? The code snippet:

var sortableLinks = $("#category_links_list_3");
var linkOrderData = $(sortableLinks).sortable('serialize');

category_links_list_3 is the id of the UL

The DOM structure is:

<div class="hidden" id="inline_list_3">
    <ul class="category_links_list ui-sortable" id="category_links_list_3">
        <li class="link_title ui-state-default" id="category_link_8">Coconut Oil</li>
        <li class="link_title ui-state-default" id="category_link_9">Hempseed</li>
    </ul>
</div>

Thanks...

6条回答
你好瞎i
2楼-- · 2020-05-19 02:56

I finally got the answer! You need to make the UL sortable first before calling the serialize method on it:

var sortableLinks = $("#category_links_list_3");
$(sortableLinks).sortable();
var linkOrderData = $(sortableLinks).sortable('serialize');

This time linkOrderData contains category_link[]=8&category_link[]=9

查看更多
可以哭但决不认输i
3楼-- · 2020-05-19 03:05

I was able to get this function working using the split. If you have multiple underscores in your class you may need to adjust the index

function serializeList(container)
{
  var str = ''
  var n = 0
  var els = container.find('tr')
  for (var i = 0; i < els.length; ++i) {
    var el = els[i]

    var p = el.id.lastIndexOf('_')
    **var getIdNumber = el.id.split("_");**

    if (p != -1) {
      if (str != '') str = str + '&'
      str = str + el.id.substring(0, p) + '[]=' + **getIdNumber[1]**
      ++n
    }
  }
  return str
}
查看更多
等我变得足够好
4楼-- · 2020-05-19 03:06

If serialize returns an empty string, make sure the id attributes include an underscore. They must be in the form: "set_number" For example, a 3 element list with id attributes foo_1, foo_5, foo_2 will serialize to foo[]=1&foo[]=5&foo[]=2. You can use an underscore, equal sign or hyphen to separate the set and number. For example foo=1 or foo-1 or foo_1 all serialize to foo[]=1.

Above one is a example. that i used it. That is why I saw 2 you.

http://jqueryui.com/demos/sortable/#method-serialize

it migth be help you.

查看更多
迷人小祖宗
5楼-- · 2020-05-19 03:11

HTML

<div class="section">
    <div class="row">
        <div class="col s6 m6 l6" style="background-color:beige">
            <ul id="sortable1" class="connectedSortable" style="min-height:30px">
                <li class="ui-state-default" id="item_1" data-id="1">Item 1</li>
                <li class="ui-state-default" id="item_2" data-id="2">Item 2</li>
                <li class="ui-state-default" id="item_3" data-id="3">Item 3</li>
                <li class="ui-state-default" id="item_4" data-id="4">Item 4</li>
                <li class="ui-state-default" id="item_5" data-id="5">Item 5</li>
            </ul>
        </div>
        <div class="col s0 m1 l1">
            <div style="visibility:hidden">a</div>
        </div>
        <div class="col s5 m5 l5" style="background-color:aqua">
            <ul id="sortable2" class="connectedSortable justify-content-right" style="min-height:30px"></ul>
        </div>
    </div>
    <textarea id="json-output"></textarea>
</div>

JQuery

$(document).ready(function () {

            var updateOutput = function (e) {
                var list = e.length ? e : $(e.target),
                    output = list.data('output');

                if (window.JSON) {
                    output.val(window.JSON.stringify(list.sortable('serialize'))); //, null, 2));
                } else {
                    output.val('JSON browser support required for this demo.');
                }
            };

            $("#sortable1, #sortable2").sortable({
                connectWith: ".connectedSortable",
                update: function () {
                    updateOutput($('#sortable2').data('output', $('#json-output')));
                },
            }).disableSelection();

        });

Result

"item[]=1&item[]=4&item[]=2"

Hope this help!

查看更多
Rolldiameter
6楼-- · 2020-05-19 03:15

var formStr = $('#container').serialize()

Added: That will work for form elements. You could also roll your own serialize like so:

function serializeList(container)
{
  var str = ''
  var n = 0
  var els = container.find('li')
  for (var i = 0; i < els.length; ++i) {
    var el = els[i]
    var p = el.id.lastIndexOf('_')
    if (p != -1) {
      if (str != '') str = str + '&'
      str = str + el.id.substring(0, p) + '[]=' + (n + 1)
      ++n
    }
  }
  return str
}

alert(serializeList($('#container')))
查看更多
爷、活的狠高调
7楼-- · 2020-05-19 03:16

This post was very helpful. In case you're looking for additional help, here's how I got it to work (using haml-rails). Using the toArray function is slightly different. If using `serialize' you would have to set the attribute, again, as 'data-item="data-item_#{id}'.

Thank you, Internet, for knowing so many programming solutions.

:css
  #sortable { list-style-type: none; margin: 40 20; padding: 0; width: 500; }
  #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
  #sortable li span { position: absolute; margin-left: -1.3em; }

:javascript
  $(function() {

    $( "#sortable" ).sortable({
      update: function( event, ui ) {
        var data = $("#sortable").sortable('toArray', {attribute: "data-item"});  
        // return ids in order after update
        //alert(JSON.stringify(data));
        $.ajax({
          type: "PATCH",
          async: true,
          url: "/calendar/update_order.json",
          data: JSON.stringify(data),
          dataType: 'json',
          contentType: 'application/json',
          error: function(data) { return false; },
          success: function(data) { return false; }
        });
      }
    });
    $( "#sortable" ).disableSelection();

  });

#sort_tickets
  %ul#sortable
    - @tickets.each do |ticket|
      %li.ui-state-default(data-item="#{ticket.id}")<
        %span.ui-icon.ui-icon-arrowthick-2-n-s<
        = ticket.customer
查看更多
登录 后发表回答