1
所以我一直在閱讀tablesorter的文檔,但我現在想根據數據庫中的內容構建動態HTML表。我決定爲此使用getJson
方法。使用.getJson構建動態表
我的問題是,儘管表已建好,並且每行可以看到所有相關記錄,但是在循環遍歷getJson
方法返回的所有對象並構建表後,tablesorter不會在我的表上初始化。
這裏是我的代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test Test</title>
<!-- load tableSorter theme -->
<link href="./includes/tablesorter-master/css/theme.default.css" rel="stylesheet">
<!-- load jQuery and tableSorter scripts -->
<script type="text/javascript" src="./includes/jquery-2.1.0.js"></script>
<!-- <script src="http://code.jquery.com/jquery-1.11.0.js"></script> -->
<script type="text/javascript" src="./includes/tablesorter-master/js/jquery.tablesorter.js"></script>
<!-- load tableSorter widgets -->
<script type="text/javascript" src="./includes/tablesorter-master/js/jquery.tablesorter.widgets.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//loader();
$.getJSON("./get_recs.php",function(data){
$("#mytable").append("<thead><tr><th>Test1</th><th>Test2</th><th>Test3</th>" +
"<th>Test4</th><th>Test5</th><th>" +
"Test6</th><th>Test7</th><th>Test8</th>" +
"<th>Test9</th></tr></thead>");
$("#mytable").append("<tbody>");
for ($z = 0; $z < data.length; $z++)
{
//console.log(data[$z])
$("#mytable").append("<tr><td>" +
data[$z]['test1'] + "</td><td>" +
data[$z]['test2'] + "</td><td>" +
data[$z]['test3'] + "</td><td>" +
data[$z]['test4'] + "</td><td>" +
data[$z]['test5'] + "</td><td>" +
data[$z]['test6'] + "</td><td>"+
data[$z]['test7'] + "</td><td>" +
data[$z]['test8'] + "</td><td>" +
data[$z]['test9'] + "</td></tr>");
}
$("#mytable").append("</tbody>");
});
initialise();
});
function initialise()
{
$("#mytable").tablesorter({
theme : 'default',
widgets : ['zebra','columns'],
debug : true
});
}
</script>
</head>
<body>
<div id="topdiv">
<table id="mytable" class="tablesorter">
</table>
</div>
</body>
</html>
任何人只要有幫助呢?
你如何建立一個具體的結構JSON? –
好了,現在我已經手動編寫了PHP來構建JSON的這種特定結構,但現在,只有當我在瀏覽器中調用腳本時,返回的JSON文件纔會加載反斜槓。這有助於加載... –
看看[我的回答這個問題](http://stackoverflow.com/questions/21972629/tablesorter-pager-plugin-ajax-with-child-rows)...基本上你的JSON可以包含HTML標記。 – Mottie