Solved

Reordering items in an ordered list using Jquery, Ajax, PHP & Mysql

Posted on 2012-03-16
3
1,356 Views
Last Modified: 2012-03-20
I'm currently developing a site using PHP/MySQL which is also using Jquery pretty extensively, and I need to add in functionality that allows the user to re-order items within an ordered list using drag and drop.  

When list items are dragged and dropped to a new position within the ordered list -- I'd like to trigger an ajax function that updates the order of those list items in the MySQL database.  My ordered list will have a structure similar to the following (where the dynamic record ID values will be appended each list items ID attributes):

<ul id="task_list_55">
     <li id="task_39"></li>
     <li id="task_40"></li>
     <li id="task_41"></li>
     <li id="task_42"></li>
</ul>

I've achieved the functionality that I've described using Prototype & Scriptaculous in the past -- though I'd much prefer to use JQuery to accomplish this if possible.  How would I accomplish something this using JQuery, PHP, and MySQL ?

One last note -- there will be multiple different ordered lists on the same page -- each with their own unique ID attributes assigned to them -- and each of those ordered lists will need to be sortable.  Please advise on how I would accomplish this.

Thanks!
- Yvan
0
Comment
Question by:egoselfaxis
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Ahmed Merghani earned 500 total points
ID: 37730190
I suggest to do such thing using jQuery UI. See this link:

http://jqueryui.com/demos/draggable/#sortable

After the ordering, you can handle the changes and trigger the AJax call within these events:
http://jqueryui.com/demos/draggable/#events
0
 

Author Comment

by:egoselfaxis
ID: 37738967
I'm already aware of jquery's ability to do drag n' drop using unordered lists.

I was actually looking for examples of the ajax function triggering (which somehow extracts the record ID values from each list item's ID attribute and passes them to the PHP script that does the re-ordering).  

Are you aware of any tutorials on this (or plugins) that might be helpful?

Thanks,
- yg
0
 

Author Comment

by:egoselfaxis
ID: 37743404
I ended up figuring this out myself:

$(function() {

	$( "ul.tasklist" ).sortable({	
		revert: true,
		stop: function(event, ui) { 
		
			document.getElementById("page").style.opacity = ".15";
			document.getElementById("page").style.filter = "alpha(opacity=15)";
			document.getElementById("loading").style.display = "block";		

			var ul_div_name = "#" + $(this).attr('ID');			
			
			var num_list_items = $(ul_div_name).children().length;
			
			var stage_ul_id = $(this).attr('ID');
			
			var stage_id = stage_ul_id.substring(stage_ul_id.indexOf('_')+6);	
			
			var list_of_task_ids = new Array();
			
			var num = 0;

			$(ul_div_name + ' li').each(function() {
			
				var id = $(this).attr('id').split('_')[1];
				
				list_of_task_ids[num] = id;	

				num++;	
				
			});				
			
			$.ajax({

				type : 'POST',
				url : '../user/packages.php?action=reorderTasks',
				dataType : 'json',
				
				data: {
					stage_id : stage_id,
					num_list_items : num_list_items,
					list_of_task_ids : list_of_task_ids,
				},
				 
				success: function(data) {
				
					document.getElementById("page").style.opacity = "1";
					document.getElementById("page").style.filter = "alpha(opacity=100)";
					document.getElementById("loading").style.display = "none";	
					
					alert(data.msg);		
					
					$('#package_edit').before('<div class="successtext"><input name="dismiss" type="button" class="deletebutton floatright" value="X" onclick="dismissWindow()" /><p><strong>The Tasks have been reordered successfully</strong></p></div>');
					
					$(".successtext").delay(1000).fadeOut(2000);

				},
				 
				error : function(XMLHttpRequest, textStatus, errorThrown) {
				 
					alert('ERROR!');
						
				}
				
			});	
		
		}	
		
	});	
	
});

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question