0

I have been a huge fan of http://www.kryogenix.org/code/browser/sorttable/ sort table javascript code. It works on so many levels.

That said, I recently come face to face with an issue trying to get the sorts to work when called via ajax. What I have is a div layer on my main page, and I am calling a dynamic query with ajax to dynamically populate the page. I was thinking that those results could then be sortable using the above javascript file. However, that is not the case and I don't quit understand why it will not work.

I have verified the code works on the page by placing the same query on the page and populating it when the page loads normally and the sorttable.js works like a charm.

If anybody could tell me why a client side language like javascript wouldn't work when called via ajax, I would appreciate it. I'm sure it is suppose to but I more then likely goofed something up.

For reference this is my jquery / ajax

        <script type="text/javascript">
                $(document).ready(function(){
                $(".small_radio").click(function() {

                //check radio buildings for selected value
                var radBuild = $('input:radio[name=buildings]:checked').val();

                //check radio daterange for selected value
                var radDate = $('input:radio[name=daterange]:checked').val();

                //create array for multiple possibilites from checkbox users
                var chkUsers = [];
                //loop through checkboxes appending values to array
                $('#checkall :checked').each(function() {
                   chkUsers.push($(this).val());
                 });

                 //send the request
                $.ajax({
                    url: "/inventory/pick-print-results.php",
                    type: "post",
                    data: "buildings=" + radBuild + "&daterange=" + radDate + "&users[]=" + chkUsers,
                    // callback for success
                     success: function(data, textStatus) {
                         $(".loadonly").hide();
                         $(".ajax_stuff").html(data);  //no data here
                          //alert(data); //Data here
                      }, //end success else...
                      //if failsauce throw error
                      error: function() {
                          alert('Not OKay');
                         } //end error failsauce
                      }); //ends .ajax function
                   }); //end #checkall. click function
                }); // ends ready function
            </script>

This is my php query data that is called via ajax..

<?php $message.='
            <input type="hidden" value="'.$big_chunk_sql.'" id="displayed_sql" name="displayed_sql">
            <input type="hidden" value="'.$row_count.'" id="amount" name="amount">
             <input type="hidden" value="print" id="print" name="print">
            <table border="0" width="100%" class="sortable">
            <th class="admin">Location</th>
            <th class="admin">Pick For</th>
            <th class="admin">Requested Date</th>
            <th class="admin">Part Number</th>
            <th class="admin">Quantity</th>
            <th class="admin">Received Date</th>
            <th class="admin">Action</th>';
            while($data=mysql_fetch_array($big_chunk_query)) {

                //Deal with operator Name Don Ford = D Ford
                list($user_first,$user_last)=explode(' ',$data['description']);
                $user_first=strtoupper(substr($user_first,0,1));
                $user_last=ucfirst($user_last);
                $operator_name=$user_first.' ' . $user_last;

               if ($i%2 !=0)
                 $rowColor = 'tr2center';
                  else
                 $rowColor = 'tr1center';
                    $pendingdate= trim($data['received_date']);
                    $newpendingdate = date('m-d-Y',strtotime($pendingdate));
                    $message.= '<tr class="'.$rowColor.'">
                    <td>'. $data['location'].'</td>
                    <td>'.$operator_name.'</td>
                    <td>'.date("m-j-y, g:i a", strtotime($data['date_requested'])) .'</td>
                    <td>'.$data['part_number'] . '</td>
                    <td>'. $data['qty_requested'] . '</td>
                    <td>'. $newpendingdate . '</td><td> 
                    <a href="picking.php?radiopart='.urlencode($data['org_transaction_id']) .'">Mark Picked</a></td></tr>';
                    if($data['notes_to_picker']!='') { 
                    $message.= '<tr class="'.$rowColor.'" align="center"><td colspan="2">&nbsp;</td><td align="right"><b>notes:</b></td><td colspan="4">' . $data['notes_to_picker'].'</td></tr>';
                    }
                    $i++;
                }
                $message.= '</table>';
                echo $message;
    ?>
6
  • What does "doesn't work" mean? Is it not calling the php page with the correct arguments? Is the php page failing somehow? Is it returning with incorrect results? Is something failing on the client side after it returns? 5 minutes spent with Firebug could answer all these questions in less time than it took you to type out the question. Commented Oct 18, 2012 at 17:48
  • By the way: blog.xcski.com/2012/10/08/how-to-debug Commented Oct 18, 2012 at 17:51
  • Hi Paul, by not working I mean the sorttable does not sort. (There is valid data returned) The sorting function normally works by clicking on a header row cell to sort asc / desc but will not when called this way. Commented Oct 18, 2012 at 17:51
  • Thanks Paul.. I understand the link, but typically expect the quality of answers to be respective of the quality of the question. If it was a turd, I apologize. Commented Oct 18, 2012 at 17:57
  • Where is the code that causes the sorting to happen? Everything you've posted so far appears to be working based on your comments. Commented Oct 18, 2012 at 17:58

2 Answers 2

1

Your case is what the plugin author would consider "advanced" if you read the documentation at the link you provided. http://www.kryogenix.org/code/browser/sorttable/#ajaxtables

in your success callback, you need sorttable.makeSortable($("#tableid")[0]);

Sign up to request clarification or add additional context in comments.

1 Comment

Son of ahh.. Thank you Kevin.
1

You are passing data incorrectly use this

data: {"buildings":radBuild,"daterange":radDate,"users":JSON.stringify(chkUsers)}

5 Comments

My experience is that data: handles arrays just fine without the JSON.stringify call.
@PaulTomblinjson make easy use of array server side.
data: {"buildings":radBuild,"daterange":radDate,"users":JSON.stringify(chkUsers)} Shows as invalid code in dreamweaver for this function..
Dreamweaver isn't always correct, and happens to be wrong in this case. In either case, changing from a param string to an object shouldn't make a difference.
Thanks Kevin I also tried to run the code.. it does not return any results. I would think there is a syntaxual difference in the 2 styles of code. Where each previous section was ended with a , (comma) in my code where the JSON code breaks that. Either way, I tried adding a comma myself to no avail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.