Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get array of IDs after jQuery UI Sortable

I am using jQuery UI draggable and sortable functions and I am new to this. I have list of items in left (generated from database ) and I have boxes on right where user can drag and drop them. While/after dropping user can sort them.

Here is full preview : http://jsbin.com/oBeXiko/3

Problem: How can I get array of IDs of elements in each box after sorting?

I have tried .sortable("toArray") and sortable("serialize") but both return empty string.

Simple HTML

<div id="raspored">  
      <div class="left">
        <ul id="kanta">Delete</ul>    
        <ul id="lista_predmeta" class="droptrue">
          <li class="predmet ui-state-default" predmet-id="id_1">Item_1</li>
          <li class="predmet ui-state-default" predmet-id="id_2">Item_2</li>
          <li class="predmet ui-state-default" predmet-id="id_3">Item_3</li>
          <li class="predmet ui-state-default" predmet-id="id_4">Item_4</li>
        </ul>
      </div>
      <div class="right">     
        <ul class="raspored" razred="1">I</ul>  
        <ul class="raspored" razred="2">II</ul>  
        <ul class="raspored" razred="3">III</ul>  
        <ul class="raspored" razred="4">IV</ul>  
      </div>  
  </div> 

My jQuery functions

$(document).ready(function() {
     var url = "";
     var raz = 0;

    $( ".raspored" ).sortable({
        cursor: 'move',
        opacity: 0.65,
        stop: function ( event, ui){
            var data = $(this).sortable('toArray');
            console.log(data); // This should print array of IDs, but returns empty string/array
       }
    });

    $(".raspored").droppable({
         accept: ".predmet",   
         drop: function(event, ui){
            ui.draggable.removeClass("predmet");
            ui.draggable.addClass("cas");
            raz = $(this).attr("razred");
         }
    });


    $(".predmet").draggable({
        connectToSortable:".raspored",
        helper: "clone",
        revert: "invalid",

        stop: function(event, ui){
                var predmet = $(this).attr("predmet-id") ;
                console.log( "PredmetID = " + predmet + " | RazredID = " + raz);
        }
    });

    $("#kanta").droppable({
        accept: ".cas",
        drop: function(event, ui){
            ui.draggable.remove();
        }
    });

    $( ".raspored, .predmet, .cas, #kanta, #lista_predmeta" ).disableSelection();
  });
like image 821
Nutic Avatar asked Nov 18 '13 16:11

Nutic


2 Answers

When you call toArray, you can pass an options object with an attribute field. This attribute field defines what attribute is used in the toArray call. For example:

var data = $(this).sortable('toArray', { attribute: 'predmet-id' });

That will give you an array of the predmet-id attributes of the items. See the toArray documentation.

Note that the default attribute is id, which is why your array returned empty strings before - none of your elements have id attributes!

like image 177
Agop Avatar answered Oct 24 '22 04:10

Agop


use serialize that work for me. below are sample code that may help you.

<ul id="sortable">
<li id="ID_11" class="ui-state-default">sai</li>
<li id="ID_21" class="ui-state-default">2</li>
<li id="ID_3" class="ui-state-default">3</li>
<li id="ID_4" class="ui-state-default">4</li>
<li id="ID_5" class="ui-state-default">5</li>
<li id="ID_6" class="ui-state-default">6</li>
<li id="ID_7" class="ui-state-default">7</li>
<li id="ID_8" class="ui-state-default">8</li>
<li id="ID_9" class="ui-state-default">9</li>
<li id="ID_10" class="ui-state-default">10</li>
<li id="ID_11" class="ui-state-default">11</li>
<li id="ID_12"  class="ui-state-default">12</li>
</ul>
 <a href="javascript:void(0);" onClick="saveDisplayChanges();">Save</a>
 <span id="categorysavemessage"></span>

Javascript code are :

<script>
   $(function() {
   $( "#sortable" ).sortable();
});

function saveDisplayChanges()
{
  var order =  $("ul#sortable").sortable("serialize");
  var a = "hello";
  var b = "hi";
  $.post("update_displayorder.php?"+order+"&a=hello"+"&b=developer",{abc:a,xyz:b},function(theResponse){
  $("#categorysavemessage").html(theResponse);

});
}
</script>

Php script for fetch order data with other info update_displayorder.php script are :

<?php
echo "<pre>";
print_r($_REQUEST); 

$newOrder = $_POST['ID'];
$counter = 1;
foreach ($newOrder as $recordIDValue) {
   echo $counter .' '. $recordIDValue .'<br/>';
   $counter ++;
}
?>
like image 22
Vijay Lathiya Avatar answered Oct 24 '22 02:10

Vijay Lathiya