Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call event after sorting in JQuery

Current Problem


In relearning jQuery/jQueryUI, I'm trying to call an event on an element after sorting it. This is probably relatively simple to do since jQuery has many ways to implement callbacks.

jQuery(document).ready(function($) {
  $('ul').addClass('list-unstyled');
  $('.sortable').sortable({
    revert: true,
    connectWith: ".sortable"
  });
});
ul {
  margin: .8rem 2rem !important;
}

li {
  margin: 0;
  padding: 0;
}

div>div {
  border: 1px solid #999;
  margin-bottom: 2px;
}

body {
  padding: 1rem;
}

.sortable {
  cursor: pointer;
}

.sortable>div:hover {
  background: #f0f0f0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js" integrity="sha256-Bxxp5LTCU2v12w2d0kxKb0vt5F4EgtrzcJKJSR3Xxio=" crossorigin="anonymous"></script>

<div class="sortable">
  <div>
    <ul class="1">
      <li>test 1</li>
      <li>test 2</li>
    </ul>
  </div>
  <div>
    <ul class="2">
      <li>test 3</li>
      <li>test 4</li>
    </ul>
  </div>
</div>
<div class="sortable">
  <div>
    <ul class="3">
      <li>test 7</li>
      <li>test 8</li>
    </ul>
  </div>
  <div>
    <ul class="4">
      <li>test 9</li>
      <li>test 0</li>
    </ul>
  </div>
</div>

So let's say I move the 3/4 list. I want to issue a callback (to change the background or the font size) after the element is finished moving.


Edit(s)


  • 20101019-1145EST: I think I need to be more clear. I'm trying to call the event on a particular element that is sorted. I guess I might be able to use the sort/change function, but shouldn't there be a conditional involved? This involves more than adding a class, the eventual function I plan to attach will perform some AJAX as well as some calculations.
like image 961
vol7ron Avatar asked Oct 19 '10 15:10

vol7ron


3 Answers

$(function() {   
    $('#sortable').sortable( { 
        revert    : true,
        connectWith  : ".sortable",
        stop     : function(event,ui){ 
              //write ur function here
        }
    });
});
like image 76
Mallikarjuna Sangisetty Avatar answered Sep 18 '22 16:09

Mallikarjuna Sangisetty


Use the stop event of the sortable plugin.

This event is triggered when sorting has stopped.

In your example

$('.sortable')
    .sortable({
       revert       : true,
       connectWith  : ".sortable",
       stop         : function(event,ui){ /* do whatever here */ }
    });
like image 30
Gabriele Petrioli Avatar answered Nov 12 '22 10:11

Gabriele Petrioli


A list of events for the jquery ui sortable plugin can be found here: http://jqueryui.com/demos/sortable/#events

like image 2
Jeremy Elbourn Avatar answered Nov 12 '22 08:11

Jeremy Elbourn