Small Problem Serializing Sortables
Hey Guys, I am having a problem with my sortable list system. It is a little bit on the unconventional side because I am am trying to allow a user to sort lists but I don’t want them to be able to carry an element from one sub list to another. Everything is working great for me as far as drag and drop goes in all the major browsers as far as the sorting goes. The problem comes when I attempt to serialize my list. It attempts to serialize but it leaves out the item that was just moved when the results come back.
The idea is to allow them to grab a handle (Currently text that says move) ... move the item... drop it and have it update the db using ajax.
For example if I were to move the first item below the second in the list it serializes as:
id[]=2&id[]=3&id[]=4&id[]=5&id[]=6&id[]=7
leaving out
id[]=1
which should appear after id[]=2&
There is a demo of the code in action here:
http://ckeditor.ziplinestaging.com/sortable4.php and the code is included below:
- <style type="text/css">
- .sortable, .sortable2, .sortable3, .sortable4 { list-style-type: none; margin: 0; padding: 0; width: 100%; }
- .sortable2 { border-top: 1px solid #FFFFFF; }
- .sortable3 { border-top: 1px solid #FFFFFF; }
- .sortable4 { border-top: 1px solid #FFFFFF; }
- .sortable li, .sortable2 li, .sortable3 li, .sortable4 li { border-top: 1px solid #FFFFFF; border-bottom: 1px solid #FFFFFF; font-size: 12px; }
- .level1 { padding: 2px 2px 2px 0px;background-color: #333333; color: #FFFFFF; }
- .level2 { padding: 2px 2px 2px 10px; background-color: #CECECE; }
- .level3 { padding: 2px 2px 2px 20px; background-color: #EEEEEE; }
- .level4 { padding: 2px 2px 2px 30px; background-color: #CECECE; }
- </style>
- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"> </script>
- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"> </script>
- <script type="text/javascript">
- $(function() {
-
- //ON CLICK OF MOVE MAKE THE LIST ITEM DRAGGABLE
- $(".move").mousedown(function() {
- var parent = $(this).parent('li').parent('ul');
- parent.sortable({ axis: 'y' }).disableSelection();
- });
-
- //ON RELEASE MAKE LOCK IT IN PLACE AND UPDATE ORDER (LATER WILL BE SENT USING AJAX)
- $(".move").mouseup(function() {
- var parent = $(this).parent('li').parent('ul');
- var order = parent.sortable('serialize');
- parent.sortable('destroy');
- alert(order);
- });
- });
- </script>
- <ul class="sortable">
- <li class="level1" id="id_1"><span class="move">Move</span> Item 1</li>
- <li class="level1" id="id_2"><span class="move">Move</span> Item 2</li>
- <li class="level1" id="id_3"><span class="move">Move</span> Item 3</li>
- <li class="level1" id="id_4"><span class="move">Move</span> Item 4
- <ul class="sortable2">
- <li class="level2" id="id_8"><span class="move">Move</span> Item 1 <a href="http://www.msn.com">Sample Link</a></li>
- <li class="level2" id="id_9"><span class="move">Move</span> Item 2</li>
- <li class="level2" id="id_10"><span class="move">Move</span> Item 3</li>
- <li class="level2" id="id_11"><span class="move">Move</span> Item 4</li>
- <li class="level2" id="id_12"><span class="move">Move</span> Item 5
- <ul class="sortable3">
- <li class="level3" id="id_15"><span class="move">Move</span> Item 1</li>
- <li class="level3" id="id_16"><span class="move">Move</span> Item 2</li>
- <li class="level3" id="id_17"><span class="move">Move</span> Item 3</li>
- <li class="level3" id="id_18"><span class="move">Move</span> Item 4</li>
- <li class="level3" id="id_19"><span class="move">Move</span> Item 5</li>
- <li class="level3" id="id_20"><span class="move">Move</span> Item 6</li>
- <li class="level3" id="id_21"><span class="move">Move</span> Item 7</li>
- </ul>
- </li>
- <li class="level2" id="id_13"><span class="move">Move</span> Item 6</li>
- <li class="level2" id="id_14"><span class="move">Move</span> Item 7</li>
- </ul>
- </li>
- <li class="level1" id="id_5">Item 5</li>
- <li class="level1" id="id_6">Item 6</li>
- <li class="level1" id="id_7">Item 7
- </li>
- </ul>