Why would javascript fail when called dynamically (Ajax)?
-
08-07-2021 - |
Question
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"> </td><td align="right"><b>notes:</b></td><td colspan="4">' . $data['notes_to_picker'].'</td></tr>';
}
$i++;
}
$message.= '</table>';
echo $message;
?>
La solution
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]);
Autres conseils
You are passing data incorrectly use this
data: {"buildings":radBuild,"daterange":radDate,"users":JSON.stringify(chkUsers)}