Ajax dropdown php table sorting
I have a page which has a dropdown box. On selection a value is sent to a php script (Ajax), based on the value a html table is created and sent back to the responseText. The table is output to the HTML page. I want the table to have sortable columns, so I have used jQuery datatables for this, but it is not working.
I have cut and pasted the exact table into the html and ran the page, and then sorting works.
Please can anyone help / advise to fix this?
Note the table output from the php is outputted inbetween:
<a div id="txtHint"> <table id="example"> <div>
Here is the rest of the code on the HTML page:
<script type="text/javascript" src="/js/jquery-1.5.1.js"></script>
<script type="text/javascript" src="/js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('table#example').dataTable( {
"sPaginationType": "full_numbers"
} );
} );
</script>
<script type="text/javascript">
function selMetal(str,str2){
if (str==""){
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.statu开发者_如何学编程s==200){
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","sql.php?m="+str+"&s="+str2,true);
xmlhttp.send();
}
</script>
Where is #example defined? Are you recreating the html for the entire table in ajax?
If so, you are probably overwriting the #example dom element that jquery dataTable already manipulated. Try re-instantiating the data table after you set the table html, such as:
xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ document.getElementById("txtHint").innerHTML=xmlhttp.responseText; $('table#example').dataTable().fnDestroy(); // might need to destroy the old one first - not sure $('table#example').dataTable( { "sPaginationType": "full_numbers" } ); } }
精彩评论