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:
- CREATE TABLE IF NOT EXISTS `tbl_objects` (
- `obj_id` int(11) NOT NULL AUTO_INCREMENT,
- `obj_name` varchar(50) DEFAULT NULL,
- `obj_type` int(11) DEFAULT NULL,
- PRIMARY KEY (`obj_id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
- INSERT INTO `tbl_objects` (`obj_id`, `obj_name`, `obj_type`) VALUES
- (1, 'Sam', 1),
- (2, 'John', 1),
- (3, 'Tom', 1),
- (4, 'Bob', 1),
- (5, 'Fluffy', 2),
- (6, 'Paws', 2),
- (7, 'Kitty', 2),
- (8, 'Tibbles', 2),
- (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):
- <!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>
- <link rel='stylesheet' href='http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css'>
- <script src='http://code.jquery.com/jquery-1.8.3.js'></script>
- <script src='http://code.jquery.com/ui/1.9.2/jquery-ui.js'></script>
- <style>
- div.div_container {
- background:#0F6;
- max-width:400px;
- padding: 10px 10px 10px 10px;
- }
- h2.h2_med_green {
- display: inline;
- font-weight:normal;
- color:#090;
- font-size: 1.4em;
- }
- ul {
- list-style: none;
- padding: 0px 0px 0px 0px;
- margin: 0px 0px 0px 0px;
- }
- li.li_sortable {
- cursor: hand;
- cursor: pointer;
- border-bottom: 1px dotted #999999;
- background:#DDDDDD;
- height: 31px;
- line-height: 31px;
- }
- </style>
- <script type='text/javascript'>
- $(document).ready(function() {
- //CALL SORTABLE FUNCTIONALITY
- $('#ul_sortable').sortable();
- //CALL AJAX RESULTS
- showAJAXResults(1);
- });
- </script>
- </head>
- <body>
- <div class="div_container">
- Click and drag each round to re-order.<br />Object Type:
- <select id="ajax_select_type">
- <option value="1">Human</option>
- <option value="2">Feline</option>
- </select>
- <div id="ajax_results"></div>
- </div>
- <script>
- $("#ajax_select_type").change( function() {
- //CALL AJAX DBR
- showAJAXResults($(this).val());
- });
- function showAJAXResults(obj_type){
-
- //HTTP REQUEST CODE
- if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();} // code for IE7+, Firefox, Chrome, Opera, Safari
- else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}// code for IE6, IE5
- xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 && xmlhttp.status==200){document.getElementById("ajax_results").innerHTML=xmlhttp.responseText;}}
-
- //ASSEMBLE THE PAGE CALL VALUE
- var page_call_value = "ajax/ajax_page.php?t="+obj_type;
-
- //CALL THE AJAX PAGE
- xmlhttp.open("GET",page_call_value,true);
- xmlhttp.send();
- }
- </script>
- </body>
- </html>
Finally, this page links to the following PHP page which is called via AJAX:
- <?php
- //DETERMINE INPUT VALUES
- $object_type=$_GET["t"];
-
- //CONNECT TO THE DATABASE
- $con = mysql_connect("localhost","<USERNAME>","<PASSWORD>");
- mysql_select_db("db_objects", $con);
-
- //GET SQL QUERY
- $q_db_result = "SELECT DISTINCT obj_id, obj_name, obj_type
- FROM tbl_objects
- WHERE obj_type = '".$object_type."'
- ORDER BY obj_name";
- //Execute the FULL_RESULT_QUERY QUERY
- $sql_results = mysql_query($q_db_result) or die(mysql_error());
-
- //TURN THE RESULTS RETRIEVED INTO AN ARRAY
- $results_array = array();
- while($row = mysql_fetch_assoc($sql_results)){
- $results_array[$row['obj_id']]['obj_name'] = $row['obj_name'];
- $results_array[$row['obj_id']]['obj_type'] = $row['obj_type'];
- }
-
- //ASSEMBLE THE RESULTS
- if(sizeof($results_array)>0){
-
- foreach($results_array as $obj_id => $result_details){
- //DECLARE VARIABLES
- $obj_name=$result_details['obj_name'];
- $obj_type=$result_details['obj_type'];
-
- $return_value .= "<li class='li_sortable' value='".$obj_id."'><h2 class='h2_med_green'>".$obj_name."</h2></li>\n";
- }
-
- //PUT THE UL TAGS AROUND THE HTML CODE
- $return_value = "<ul id='ul_sortable'>\n".$return_value."</ul>\n";
-
- //PUT THE RESULTS IN THE INNER CONTAINER DIV
- $return_value = "<div id='ajax_results_ic' class='ajax_results_ic_2'>".$return_value."</div>";
-
- }else{
- $return_value = "NO RESULTS RETURNED";
- }
- //DISPLAY HTML CODE
- echo $return_value;
-
- //CLOSE DB CONNECTION
- mysql_close($con);
- ?>
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.
- $(document).ready(function() {
- //CALL SORTABLE FUNCTIONALITY
- $('#ul_sortable').sortable();
- });
I'm not sure how to get around this - does anyone have any suggestions?
Thanks so much in advance!