Using sortable() on AJAX-generated results

Using sortable() on AJAX-generated results

Hi all,

I have been playing around with the sortable() functionality and I have got it working fine on static DB results, however I have noticed that I can't get it to work on AJAX-generated DB results. I feel it has something to do with how I call the sortable() function within the $(document).ready(function() however I am not 100% sure. I have searched this forum and other forums to see if anyone else has this problem but I haven't found anything similar yet, so here's my attempt to describe my problem:

I have a MySQL database called 'db_objects' which contains 1 table:

  1. CREATE TABLE IF NOT EXISTS `tbl_objects` (
  2.   `obj_id` int(11) NOT NULL AUTO_INCREMENT,
  3.   `obj_name` varchar(50) DEFAULT NULL,
  4.   `obj_type` int(11) DEFAULT NULL,
  5.   PRIMARY KEY (`obj_id`)
  6. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

  7. INSERT INTO `tbl_objects` (`obj_id`, `obj_name`, `obj_type`) VALUES
  8. (1, 'Sam', 1),
  9. (2, 'John', 1),
  10. (3, 'Tom', 1),
  11. (4, 'Bob', 1),
  12. (5, 'Fluffy', 2),
  13. (6, 'Paws', 2),
  14. (7, 'Kitty', 2),
  15. (8, 'Tibbles', 2),
  16. (9, 'Mr. Meow', 2);

Now, I have a HTML page where I want to view the results on this table based on a drop-down box which filters by the obj_type column (1 = human names, 2 = feline names):

  1. <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><title>SORTABLE EXAMPLE</title>
  2. <link rel='stylesheet' href='http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css'>
  3. <script src='http://code.jquery.com/jquery-1.8.3.js'></script>
  4. <script src='http://code.jquery.com/ui/1.9.2/jquery-ui.js'></script>
  5. <style>
  6. div.div_container {
  7. background:#0F6;
  8. max-width:400px;
  9. padding: 10px 10px 10px 10px;
  10. }
  11. h2.h2_med_green { 
  12. display: inline;
  13. font-weight:normal;
  14. color:#090;
  15. font-size: 1.4em;
  16. }
  17. ul { 
  18. list-style: none;
  19. padding: 0px 0px 0px 0px;
  20. margin: 0px 0px 0px 0px;
  21. }
  22. li.li_sortable {
  23. cursor: hand; 
  24. cursor: pointer;
  25. border-bottom: 1px dotted #999999;
  26. background:#DDDDDD;
  27. height: 31px;
  28.   line-height: 31px;
  29. }
  30. </style>
  31. <script type='text/javascript'>
  32. $(document).ready(function() {
  33. //CALL SORTABLE FUNCTIONALITY
  34. $('#ul_sortable').sortable();
  35. //CALL AJAX RESULTS
  36. showAJAXResults(1);
  37. });
  38. </script>
  39. </head>
  40. <body>
  41. <div class="div_container">
  42.     Click and drag each round to re-order.<br />Object Type:&nbsp;
  43.     <select id="ajax_select_type">
  44.     <option value="1">Human</option>
  45.         <option value="2">Feline</option>
  46.     </select>
  47.     <div id="ajax_results"></div>
  48. </div>
  49. <script>
  50. $("#ajax_select_type").change( function() {
  51. //CALL AJAX DBR
  52. showAJAXResults($(this).val());
  53. });
  54. function showAJAXResults(obj_type){
  55. //HTTP REQUEST CODE
  56. if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();} // code for IE7+, Firefox, Chrome, Opera, Safari
  57. else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}// code for IE6, IE5  
  58. xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 && xmlhttp.status==200){document.getElementById("ajax_results").innerHTML=xmlhttp.responseText;}}
  59. //ASSEMBLE THE PAGE CALL VALUE
  60. var page_call_value = "ajax/ajax_page.php?t="+obj_type;
  61. //CALL THE AJAX PAGE
  62. xmlhttp.open("GET",page_call_value,true);
  63. xmlhttp.send();
  64. </script>
  65. </body>
  66. </html>

Finally, this page links to the following PHP page which is called via AJAX:

  1. <?php
  2. //DETERMINE INPUT VALUES
  3. $object_type=$_GET["t"];
  4. //CONNECT TO THE DATABASE
  5. $con = mysql_connect("localhost","<USERNAME>","<PASSWORD>");
  6. mysql_select_db("db_objects", $con);
  7. //GET SQL QUERY
  8. $q_db_result = "SELECT DISTINCT obj_id, obj_name, obj_type
  9. FROM tbl_objects
  10. WHERE obj_type = '".$object_type."'
  11. ORDER BY obj_name";

  12. //Execute the FULL_RESULT_QUERY QUERY
  13. $sql_results = mysql_query($q_db_result) or die(mysql_error());
  14. //TURN THE RESULTS RETRIEVED INTO AN ARRAY
  15. $results_array = array();
  16. while($row = mysql_fetch_assoc($sql_results)){
  17. $results_array[$row['obj_id']]['obj_name'] = $row['obj_name'];
  18. $results_array[$row['obj_id']]['obj_type'] = $row['obj_type'];
  19. }
  20. //ASSEMBLE THE RESULTS
  21. if(sizeof($results_array)>0){
  22. foreach($results_array as $obj_id => $result_details){
  23. //DECLARE VARIABLES
  24. $obj_name=$result_details['obj_name'];
  25. $obj_type=$result_details['obj_type'];
  26. $return_value .= "<li class='li_sortable' value='".$obj_id."'><h2 class='h2_med_green'>".$obj_name."</h2></li>\n";
  27. }
  28. //PUT THE UL TAGS AROUND THE HTML CODE
  29. $return_value = "<ul id='ul_sortable'>\n".$return_value."</ul>\n";
  30. //PUT THE RESULTS IN THE INNER CONTAINER DIV
  31. $return_value = "<div id='ajax_results_ic' class='ajax_results_ic_2'>".$return_value."</div>";
  32. }else{
  33. $return_value = "NO RESULTS RETURNED";
  34. }

  35. //DISPLAY HTML CODE
  36. echo $return_value;
  37. //CLOSE DB CONNECTION
  38. mysql_close($con);
  39. ?>


Now, if you can get this working you will notice that the sortable() functionality does not work. As I mentioned before, I think it has something to do with the fact that the DB results are generated on-the-fly by AJAX, so trying to declare/call the sortable() functionality in the $(document).ready(function() won't work, i.e.

  1. $(document).ready(function() {
  2. //CALL SORTABLE FUNCTIONALITY
  3. $('#ul_sortable').sortable();
  4. });

I'm not sure how to get around this - does anyone have any suggestions?

Thanks so much in advance!