Solved

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

Posted on 2012-03-16
3
1,303 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now