Jquery sortable('serialize')

前端 未结 6 1660
遥遥无期
遥遥无期 2021-02-01 08:57

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:

va         


        
6条回答
  •  忘掉有多难
    2021-02-01 09:44

    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
    

提交回复
热议问题