【发布时间】:2012-08-02 09:07:38
【问题描述】:
这是一个类似的问题:Mysql PHP generated table: doesn't work with Tablesorter
但是,有一点不同:我直接在同一个文件中生成表,而不是外部文件,因此.load 不是一个选项。
我的代码:
<html>
<head>
<title>Tablesorter testing page</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.tablesorter.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#races").tablesorter();
});
</script>
</head>
<body>
<?php
$db = new mysqli("localhost", "user", "password", "database");
$query = "SELECT name, date FROM races";
$result = $db->query($query, MYSQLI_STORE_RESULT);
$o = '<table id="races"><thead><tr><th>Name</th><th>Date</th></tr></thead><tbody>';
while(list($name, $date) = $result->fetch_row()) {
$o .= '<tr><td>'.$name.'</td><td>'.$date.'</td></tr>';
}
$o .= '</tbody></table>';
echo $o;
?>
</body>
</html>
问题是表格没有格式化,好像对空表调用了Tablesorter?如果我对 html 表进行硬编码,Tablesorter 就可以正常工作。
那么,我该如何让它发挥作用呢?
编辑:下面是生成的 .html 代码
<html>
<head>
<title>Tablesorter testing page</title>
<link rel="stylesheet" href="style.css" type="text/css"/>
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.tablesorter.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$("#races").tablesorter();
});
</script>
</head>
<body>
<table id="races">
<thead><tr><th>Name</th><th>Date</th></tr></thead>
<tbody><tr><td>Race 1</td><td>2012-01-01</td></tr><tr><td>Race 2</td><td>2012-01-01</td></tr></tbody></table>
</body>
</html>
【问题讨论】:
-
无论您的内容是否由 PHP 生成,对 jquery 都没有影响。 PHP 在服务器端运行并将内容发送到客户端。 jquery/javascript 在客户端运行。
-
我完全意识到这一点,这就是为什么我提到硬编码示例差异......这很有趣。
-
你能发布它生成的HTML代码吗? (如果太长,可以缩短版)
-
是否有可能在您的表格呈现之前触发文档就绪事件?
-
这很有可能...我想知道,如何让表格在此之前生成(同步方式...)
标签: php jquery mysql tablesorter