Search code examples
javascriptjqueryjquery-uijquery-ui-draggablejquery-ui-droppable

How to move only particular item using drag and drop JQuery?


I don't want to move all the item to the div. I wants to move only specific item form one div to another

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Sortable - Connect lists</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <style>
  #sortable1, #sortable2 {
    border: 1px solid #eee;
    width: 142px;
    min-height: 20px;
    list-style-type: none;
    margin: 0;
    padding: 5px 0 0 0;
    float: left;
    margin-right: 10px;
  }
  #sortable1 li, #sortable2 li {
    margin: 0 5px 5px 5px;
    padding: 5px;
    font-size: 1.2em;
    width: 120px;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#sortable1, #sortable2" ).sortable({
      connectWith: ".connectedSortable"
    }).disableSelection();
  } );
  </script>
</head>
<body>

<ul id="sortable1" class="connectedSortable">
  <li class="ui-state-default">StoreID</li>    // I want to move only this STOREID li element to another UL
  <li class="ui-state-default">ItemLookupCode</li>
  <li class="ui-state-default">ExtendedCost</li>
  <li class="ui-state-default">Quantity</li>
  <li class="ui-state-default">Price</li>
</ul>

<ul id="sortable2" class="connectedSortable">

</ul>


</body>
</html>

I want to move only the first element of UL StoreID to another UL nor all the elements. How to achieve it.

Thanks


Solution

  • This is best done using update or receive callbacks. The key is: ui.sender.sortable("cancel"); This will revert the change.

    Working Example: https://jsfiddle.net/Twisty/4f5yh3pa/

    HTML

    <ul id="sortable1" class="connectedSortable">
      <li class="ui-state-default acceptable">StoreID</li>
      <li class="ui-state-default">ItemLookupCode</li>
      <li class="ui-state-default">ExtendedCost</li>
      <li class="ui-state-default">Quantity</li>
      <li class="ui-state-default">Price</li>
    </ul>
    
    <ul id="sortable2" class="connectedSortable">
    </ul>
    

    CSS

    .connectedSortable {
      border: 1px solid #eee;
      width: 142px;
      min-height: 20px;
      list-style-type: none;
      margin: 0;
      padding: 5px 0 0 0;
      float: left;
      margin-right: 10px;
    }
    
    #sortable1 {
      background: #fff;
    }
    
    #sortable2 {
      background: #999;
    }
    
    .connectedSortable li {
      margin: 0 5px 5px 5px;
      padding: 5px;
      font-size: 1.2em;
      width: 120px;
    }
    

    JavaScript

    $(function() {
      $(".connectedSortable").sortable({
        connectWith: ".connectedSortable",
        update: function(e, ui) {
          if ($(e.target).attr("id") == "sortable2") {
            if (ui.item.hasClass("acceptable")) {
              console.log($(this).attr("id") + " accepted item: ", ui.item);
            } else {
              console.log($(this).attr("id") + " rejected item: ", ui.item);
              ui.sender.sortable("cancel");
            }
          }
        }
      }).disableSelection();
    });
    

    You can animate this if you want to make it look more like draggable.